Pass parameters via the command line to NUnit
Asked Answered
N

4

32

Is it somehow possible to pass values to NUnit tests via the 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 the command line. File App.config is not an option, because I want to run the tests for different URLs via a batch file.

Niersteiner answered 6/7, 2010 at 18:35 Comment(1)
Have you tried using Environment.GetCommandLineArgs? Did that work? msdn.microsoft.com/en-us/library/…Conics
S
8

NUnit 3 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");
Shelburne answered 31/12, 2018 at 5:50 Comment(0)
P
26

Use an environment variable to pass the information.

Use set from the command-line or <setenv> from NAnt. Then read the value using Environment.GetEnvironmentVariable().

Pittman answered 22/5, 2011 at 23:56 Comment(0)
S
8

NUnit 3 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");
Shelburne answered 31/12, 2018 at 5:50 Comment(0)
N
2

There seems to be no solution at the moment. The best option is to use NUnit project files, modify settings there and pass the solution file to the runner.

Niersteiner answered 5/10, 2010 at 7:59 Comment(0)
L
1

I had a similar issue. The answer of Achim put me on the right track, and 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 configuration and assembly files) are relative to the location of the NUnit file. Also the App.config, App.Dev.config, etc. file are just .NET configuration files.

Next when you want to run it for a certain configuration you execute it like this:

nunit3-console.exe example.nunit /config:test

More information about the format of the NUnit file is in NUnit Project XML Format.

More information about command-line arguments is in http://www.nunit.org/index.php?p=consoleCommandLine&r=2.2.5

Landgrave answered 23/11, 2016 at 12:12 Comment(10)
@Niersteiner Tried doing the above method by adding configs in nunit file, but throws error message - Unable to locate Fixture. \nunit-console-x86.exe $env_config /config:CI /run:$feature $dll_dir /result=$result_dirParonomasia
@Marteen Kieft Can you help me with above issue I am facingParonomasia
@ReuseAutomator: It seems that it something inside your project and nothing specific to this config setup. You can actually without this config setup directly run you tests by executing: nunit3-console.exe mytest.dll You problably get the same error, so you might want to check: Does your test class has a testfixture attribute on it Start your class with Public (so public mytestclass { ..} instead of only class (without the public). If you are still facing it, create a question here and point me to it :)Landgrave
@Marteen Kieft I can run the tests directly by passing the dll which is working fine, but my requirement is to specify the example.nunit config file as need to pass environment variable to be used across tests.I have raised a seperate question #40877978Paronomasia
@Marteen Kieft Created a new nunit project just doing some basic test. Still see the Unable to locate Fixture issue when we pass custom nunit file to load in console. Class access is Public with Test Fixture attribute.Paronomasia
I have tried to do this, however it seems to ignore my App.Dev.config file and only runs off the App.Config settingsVip
A few things to try: Change something in the dev.config so you know for sure which config is used. Place the app.config on a wrong location and see if it still runs or throwing an error that it doesnt find the file.Landgrave
I tried this but the configfile parameter seems to expect a app.config file. Might have something to do with the warning at the end of nunit-console's help info, I guess, which says you need NUnitProjectLoader and/or VSProjectLoader. But passing in an app.config file was good enough for me so I stopped there. Also, see #4257309 for adding/using variables from app.config.Textual
Why double "\" for two of them, "\\"? <assembly path="bin\Debug\\example.dll"/>Manno
The last link is (effectively) broken: "Legacy Documentation. The help files have moved to a new location. The page you are looking for was not found". (And the redirect link does not work.)Manno

© 2022 - 2024 — McMap. All rights reserved.