Is it somehow possible to pass values to NUnit tests via command line? My tests use a certain URL. I have different instances of my code at different URLs and would like to specify the URL via command line. App.config is not an option, because I want to run the tests for different URLs via a batch file.
Environment variable.
Use set
from the command-line or <setenv>
from nant. Then read the value using Environment.GetEnvironmentVariable()
.
There seems to be no solution at the moment. Best option is to use NUnit project files, modify settings there and pass the solution file to the runner.
I had similar issue, The answer of Achim put me on the right track, for other readers
Create a file like example.nunit like this:
<NUnitProject>
<Settings activeconfig="local"/>
<Config name="local" configfile="App.config">
<assembly path="bin\Debug\example.dll"/>
</Config>
<Config name="dev" configfile="App.Dev.config">
<assembly path="bin\Debug\\example.dll"/>
</Config>
<Config name="test" configfile="App.Test.config">
<assembly path="bin\Debug\\example.dll"/>
</Config>
</NUnitProject>
All the file / paths (of the config and assembly files) are relative to the location of the nunit file. Also the App.config, App.Dev.config, etc. are just .net config files.
Next when you wanne run it for a certain config you execute it like this
nunit3-console.exe example.nunit /config:test
More info about the format of the nunit file https://github.com/nunit/docs/wiki/NUnit-Project-XML-Format
More info about command line arguments http://www.nunit.org/index.php?p=consoleCommandLine&r=2.2.5
NUnit3
now allows passing parameters. Here is the usage
nunit3-console [inputfiles] --params:Key=Value
From the documentation
--params|p=PARAMETER
A test PARAMETER specified in the form NAME=VALUE for consumption by tests. Multiple parameters may be specified, separated by semicolons or by repeating the --params option multiple times. Case-sensitive.
Here's how you can access the parameter through code:
var value= TestContext.Parameters.Get("Key", "DefaultValue");
来源:https://stackoverflow.com/questions/3189149/pass-parameters-via-command-line-to-nunit