Pass parameters via command line to NUnit

北城余情 提交于 2019-11-28 21:09:27

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");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!