How to pass parameters to the dotnet test command while using NUnit or XUnit How to pass parameters to the dotnet test command while using NUnit or XUnit selenium selenium

How to pass parameters to the dotnet test command while using NUnit or XUnit


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.csvar 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.cmdSETLOCALSET 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'SETLOCALSET TestUser=%1SET TestPassword=%2SET TestUrl=%3DOTNET 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 environmentSET TestPassword=$(TestPassword)dotnet test $(Build.SourcesDirectory)\MyCompany.MyProduct.UITests\MyTest.csproj --configuration $(BuildConfiguration) --collect "Code Coverage" --logger trx --results-directory $(Agent.TempDirectory)

run Azure DevOps command task


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


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\")