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

5

22

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.

Dermatology answered 6/1, 2019 at 17:30 Comment(1)
As of 3.13+, the nunit console runner supports .NET Core (see https://mcmap.net/q/587300/-does-net-core-work-with-the-nunit-console-runner)Umberto
M
29

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)

run Azure DevOps command task

Mra answered 24/8, 2019 at 13:41 Comment(1)
Remember to use lowercase "set" on UbuntuAuscultation
S
12

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
Sands answered 6/1, 2019 at 18:54 Comment(2)
Thanks very much, I'll keep it like this for the moment. All other pages i read also mentioned that its not currently possible. However, it may be possible in xUnit, maybe its worth to have a look at this.Dermatology
learn.microsoft.com/en-us/visualstudio/test/…Gayn
U
10

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

Undertaker answered 18/12, 2020 at 8:46 Comment(2)
HI, When I execute the cmd file in a powershell in the folder location, getting below error : Could not execute because the specified command or file was not found. Possible reasons for this include: * You misspelled a built-in dotnet command. * You intended to execute a .NET program, but dotnet-TEST does not exist. * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.Bobker
From github.com/Microsoft/vstest-docs/blob/main/docs/… # cmd dotnet test -- TestRunParameters.Parameter(name=\"myParam\", value=\"value\") # powershell dotnet test --% -- TestRunParameters.Parameter(name=\"myParam\", value=\"value\") # bash dotnet test -- TestRunParameters.Parameter(name=\"myParam\",\ value=\"value\")Sherly
G
6

You can also pass environment variables to dotnet test via the -e/--environment parameter.

dotnet test TestProject.csproj --environment USERNAME="Bob" --environment PASSWORD="password"

After that you can retrieve those values using Environment.GetEnvironmentVariable like mentioned in the other answers.

var username = Environment.GetEnvironmentVariable("USERNAME");
var password = Environment.GetEnvironmentVariable("PASSWORD");

Reference: https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-test#synopsis

Germinate answered 15/9, 2023 at 16:23 Comment(0)
T
-1

we can directly pass environment variables through for example: param1=vale param2=value dotnet test Get value in c#:

 var user = Environment.GetEnvironmentVariable("param1");
Trimurti answered 30/6, 2023 at 11:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.