2

I created a runsettings file which looks like this

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
  <TestRunParameters>
    <Parameter name ="environment" value="PROD" />
  </TestRunParameters>
</RunSettings>

And then in my TestSetup portion (using LeanFT for UI tests) I specify that the target environment is contained under a paramater called environment

string env= TestContext.Parameters["environment"];

This doesnt seem to work, and I am not getting any particular error messages. Is this the right way to do this, or is there an easier way to just use Environment and something I pass into the command line.

Tree55Topz
  • 1,102
  • 4
  • 20
  • 51

2 Answers2

4

You should be more specific than "via the commandline" since there are a number of ways that folks run NUnit tests from the command-line.

If you are using the nunit3-console.exe runner, you pass run parameters to the framework using the --params option, for example:

nunit3-console my.test.dll --params "environment=PROD"

The .runsettings file is an artifact used by Visual Studio and recognized by the NUnit VS adapter, but not by NUnit itself.

You can use that from the command-line as well, using vstest.console.exe. If that's what you are using, you want the /Settings option in order to specify the file.

Two answers for the price of one! But if you are using neither nunit-console nor vstest.console you'll have to ask again. ;-)

Charlie
  • 12,928
  • 1
  • 27
  • 31
  • Thank you for the response! I am using the nunit console, using the command that essentially looks like [path\to\nunit-console] [dll].. If I specify --params "environment=PROD", what does the code look like to actually take that param and do something if there is no runsettings file that can be used for NUnit? – Tree55Topz Nov 03 '17 at 19:53
  • You access the value using `TestContext.Parameters` just as you described in your question. This is a framework feature. Each runner lets you set the parameter (assuming it supports it) in its own way. The original implementation was through a command-line option and the `.runsettings` file approach was added to hook it up to the adapter. – Charlie Nov 04 '17 at 23:18
  • Thank you! This actually works great, very clean and easy to use. My only criticism on NUnit is the documentation. This is such a trivial issue, but i struggled to find examples (actual examples in code) from the NUnit documentation. – Tree55Topz Nov 06 '17 at 15:19
1

Within a test could you use the following to write all your settings

   foreach (var name in TestContext.Parameters.Names)
   {
       Console.WriteLine("Parameter: {0} = {1}", name, TestContext.Parameters.Get(name))
   }
dove
  • 20,469
  • 14
  • 82
  • 108
  • I am not sure how this helps.. What I want is just to toggle this string env when I run the tests on the command line – Tree55Topz Nov 03 '17 at 15:29