0

I have set of selenium tests developed with VS 2017,Nunit (Project type - class library using .net framework 452. In my OneTimeSetUp, I am reading app.config that define few things such web URL, database connection string, Web Login user ID/password etc.

example:

<appSettings>
    <add key="Browser" value="IExplorer" />
    <add key="User" value="xxx" />
    <add key="Password" value="xxx" />
    <add key="BaseURL" value="http://ccc.com" />
    <add key="DefaultImplicitDriverWait" value="15" />
    <add key="TestRailIntegrationValue" value="False" />
    <add key="GenerateCustomReport" value="False" />
  </appSettings>
  <connectionStrings>
    <add name="DB1" connectionString="Data Source=db_name;Initial Catalog=db_instance;User Id=userid;Password=pwd;" />

  </connectionStrings> 

Then to run tests, I use nunit command line -- example

cd "C:\path-to-repo location"
packages\NUnit.ConsoleRunner.3.7.0\tools\nunit3-console pathToDll --where "cat == Demo" 

cmd /k 

Now this works fine as long I target for one env. Now when I need to switch tests to different env, before running tests from command line or from VS 2017, I need to change the app.config bu pointing to different env variables.

So is there a way, i can pass these values as parameter in the command line that then update the app.config before executing any test?

If there any alternate solution/suggestion, I am happy to get all suggestion.

deb
  • 145
  • 1
  • 2
  • 11

3 Answers3

1

NUnit doesn't make use of or modify in any way your config file. All it does is make sure it's available to the tests.

This issue asks for the addition of a command-line option to change the config file when running NUnit. You may want to add your voice to the issue.

Meanwhile, the option available through NUNit is to specify the individual parameters using the --params option, for example:

--params "Browser=IExplorer"

If you are willing to read a config file yourself, you could even specify its name using --params.

Charlie
  • 12,928
  • 1
  • 27
  • 31
0

Please, consider theese 2 approaches:

1) Look How to modify my App.exe.config keys at runtime? You can use this approach. First set some env vars in your CI server, read them and change your app.config somewhere in [OneTimSetUp] method or whatever attribute you have in your test framework.

2) Look at this plugin https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.SlowCheetah-XMLTransforms and here http://www.c-sharpcorner.com/article/transform-config-using-slow-cheetah/ I used it and really like it. On CI in your job you just need change target from release to whatever you create and it works really fine. And another advantage - you can easily switch to any app.config in Visual Studio and run/debug test on any env just choosing your config from dropdown.

P.S. In both cases you don't need to change anything in Nunit console command

SerhiiBond
  • 247
  • 3
  • 8
  • 1
    Thanks SerhiiBond Perfect .. I had previously looked at SlowCheetah but never occur to my mind. Your 2 idea just worked.. this what i did - I created multiple app.config files based on my requirements - then point my nUnit command like to the dll under that folder .. – deb Jan 12 '18 at 01:22
0

In Nunit 3 they added a --configfile option. See this small description.

Also here is some more information on the params flag:

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

Shannon McRae
  • 231
  • 3
  • 10