I am using MSTest in Visual Studio 2008 with C#. I have a specific environment variable I would I would like to and a path modification I would like to do only during the run of either specific tests or better yet all test within a run configuration.
I tried using the test run configuration Setup script to do this but as I expected since it is a batch file the changes are lost once it exits, so that wont work.
Is there any other way to setup temporary system environment variables that will be valid during all tests being run?
Environment variables can be defined temporarily by booting to MS-DOS mode, defining variables with the SET command and then starting Windows. Variables defined in this way persist until the system is rebooted.
To set a custom environment variable, you must define it in the workflow file. The scope of a custom environment variable is limited to the element in which it is defined. You can define environment variables that are scoped for: The entire workflow, by using env at the top level of the workflow file.
Environment Variables are available to all processes and objects but a session variable is only available to that running process instance and values get updated on the go.
While am not happy with this solution, I was able to get what I needed done with MSTest by using the ClassInitializeAttribute for a test class, and then using Environment.SetEnvironmentVariable to make the changes I need, and then clean this up in the method decorated with theClassCleanupAttribute.
With lack of a better answer this was how I was able to get environment variables set for a group of tests and clean it up when I was done. However I would have prefered this to be handled outside of the CODE and be part of test configuration in some way. Regardless issues has been resolved.
If you trust that your test suite won't be "aborted" mid-test, you can use FixtureSetup and FixtureTeardown methods to set and then remove your changed environment variables.
EDIT FROM COMMENT: I see where you're coming from, but as in my edit, a UT framework is deisgned to be used to create unit tests. The concept of a unit test dictate that it should NOT depend on any outside resources, including environment variables. Tests that do this are integration tests, and require a lot of infrastructure to be in place (and usually take many times longer than a unit test suite of equal LOC).
To create a unit test for code that depends on an environment variable, consider splitting out the lines of code that actually examine the environment variables directly,. and put that into a method in another class, then mock that class using RhinoMocks or whatever to provide a "dummy" value for testing without examining (or changing) actual environment variables.
If this really is an integration test and you really need the environment variable set (say you're changing the path so you can use Process.Start to call your own notepad.exe instead of Windows'), that's what the FixtureSetup and FixtureTeardown methods/attributes are for; to perform complicated setup of a fixed, repeatable environment in which the tests should succeed, and then reset the environment to the way it was, regardless of what happened in the tests. Normally, a test failure throws an exception and ends that test's execution immediately, so code at the end of the test method itself is not guaranteed to run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With