4

Is there a way to access configuration parameters within MSBuild unit tests?

I have teamcity configured to run lengthy test set every couple of hours. Every now and then I want to run test with specific parameter (string restricting sample test set). Instead of checking in change to unit test I would like to click "Run..." in team city and set a value to some parameter.

Is this possible?

Marek
  • 2,419
  • 6
  • 34
  • 38

1 Answers1

5

One possible solution is to set an environment variable from a build parameter in teamcity and then having the test look for this environment variable in order to determine what to run.

If you choose build parameters from the configuration steps menu and add a new build paramater and then choose the type as EnvironmentVariable (.env) you can set the to some value like 'all' for when you want to run all the tests and some other value when you want to run a restricted set.

You might be able to create a template which does not have a value set for this build parameter, then have one instance of the template which sets the 'all' value, for the automated test run. Then have another build based on this template which doesn't set a value for this, which you use for manual runs. I believe that if you have not set a value for the parameter then TeamCity will prompt you for it when you try and run the build (but I'm not certain about that)

Once you have defined a variable called env.file_path_contains in teamcity then you can access its value in your tests using this code:

System.Environment.GetEnvironmentVariable("file_path_contains")
Sam Holder
  • 32,535
  • 13
  • 101
  • 181
  • Yeap I thinkg this is what I am looking for. Just for completeness: If I define EnvironmentVariable: "env.File_Path_Contains" I can access in my test as System.Environment.GetEnvironmentVariable("File_Path_Contains") or System.Environment.GetEnvironmentVariable("env.File_Path_Contains") – Marek Jul 29 '14 at 10:58
  • @Marek, thanks I have updated the answer with the code you used to access the environment. – Sam Holder Jul 29 '14 at 11:25
  • System.Environment.GetEnvironmentVariable("file_path_contains") works perfectly well, no need to add 'env', Thanks for this! – reachfreedom Dec 21 '20 at 23:31