I'm developing some end-to-end tests using C# with .NET Core, Selenium and NUnit.
Now i want to write a login testcase. My tests are started from console simply by using the dotnet test
command.
I simply want to pass username and password to this command and get them in my tests. I can not use NUnit-Console since it doesn't support .NET Core at the moment.
Whats the suggested way to solve this problem? I would prefer to not store the settings in a file but to directly input them into the console.
If you want to avoid a runsettings file, you can use this workaround. One of the recommended ways of passing parameters, is through environment variables. So in your C# nunit (or xunit) file, you can do something like:
// in mytest.cs
var user = Environment.GetEnvironmentVariable("TestUser");
var password = Environment.GetEnvironmentVariable("TestPassword");
var url = Environment.GetEnvironmentVariable("TestUrl");
If you do not want to definitively set your environment variables, remember you can always set them temporarily for just your session process. One way of doing this, is by creating a simple cmd file
#launchtests.cmd
SETLOCAL
SET TestUser='pete001'
SET TestPassword='secret'
SET TestUrl='http://testserver.local/login'
DOTNET TEST mytest.csproj
And now the fun part. You can parameterize every aspect of this. So you can change it to:
#run wity launchtests.cmd pete001 secret 'http://testserver.local/login'
SETLOCAL
SET TestUser=%1
SET TestPassword=%2
SET TestUrl=%3
DOTNET TEST mytest.csproj
Or if you want to launch the test from an Azure DevOps (fka VSTS or TFS) pipeline, you can simply use the $(...) notation to inline variables, even if they're marked secret and/or come from Azure KeyVault.
#In Azure DevOps, variables not marked as secret are already added to the environment
SET TestPassword=$(TestPassword)
dotnet test $(Build.SourcesDirectory)\MyCompany.MyProduct.UITests\MyTest.csproj --configuration $(BuildConfiguration) --collect "Code Coverage" --logger trx --results-directory $(Agent.TempDirectory)
This documentation suggests that it should now be possible to pass in arguments on the command line, instead of within a runsettings file.
https://github.com/Microsoft/vstest-docs/blob/master/docs/RunSettingsArguments.md
dotnet test -- MSTest.MapInconclusiveToFailed=True MSTest.DeploymentEnabled=False
Note the space after --
.
Edit 1
What worked for me was a combination of adding a runsettings file, and then overriding the param I wanted to, using this syntax:
dotnet test -- TestRunParameters.Parameter(name=\"myParam\", value=\"value\")
Unfortunately, the only way to pass settings from dotnet test
into NUnit is to use a .runsettings
file. There's no way for NUnit to create custom command line arguments for the dotnet test
tool - although we'd love there to be!
Take a look at the sample .runsettings
file here. The specific bit you'll need:
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<!-- Parameters used by tests at runtime -->
<TestRunParameters>
<Parameter name="webAppUrl" value="http://localhost" />
<Parameter name="webAppUserName" value="Admin" />
<Parameter name="webAppPassword" value="Password" />
</TestRunParameters>
</RunSettings>
You should just be able to then pass this file into dotnet test
with the -s
flag.
dotnet test myProj.csproj -s mySettings.runsettings
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With