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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Achim
  • 15,415
  • 15
  • 80
  • 144
  • Have you tried using Environment.GetCommandLineArgs? Did that work? http://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs.aspx – Paddyslacker Sep 01 '10 at 19:38

4 Answers4

25

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().

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brian Low
  • 11,605
  • 4
  • 58
  • 63
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");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ankit Vijay
  • 3,752
  • 4
  • 30
  • 53
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Achim
  • 15,415
  • 15
  • 80
  • 144
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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Maarten Kieft
  • 6,806
  • 3
  • 29
  • 34
  • @Achim 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_dir – ReuseAutomator Nov 29 '16 at 00:29
  • @Marteen Kieft Can you help me with above issue I am facing – ReuseAutomator Nov 29 '16 at 00:32
  • @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 :) – Maarten Kieft Dec 02 '16 at 08:42
  • @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 http://stackoverflow.com/questions/40877478/loading-nunit-project-file-example-nunit-during-specflow-feature-execution – ReuseAutomator Dec 05 '16 at 01:42
  • @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. – ReuseAutomator Dec 05 '16 at 01:51
  • I have tried to do this, however it seems to ignore my App.Dev.config file and only runs off the App.Config settings – Tree55Topz Aug 31 '17 at 15:23
  • 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. – Maarten Kieft Aug 31 '17 at 19:33
  • 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 https://stackoverflow.com/questions/4256809/can-we-declare-variables-in-app-config-file for adding/using variables from app.config. – Chris Feb 08 '19 at 15:56
  • Why double "\" for two of them, "\\"? `` – Peter Mortensen Mar 17 '19 at 23:49
  • 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.) – Peter Mortensen Nov 05 '19 at 09:38