您可以为 MSTest 运行配置设置临时环境变量吗?
我在 Visual Studio 2008 中使用 MSTest 和 C#。我有一个特定的环境变量,我想要一个路径修改,我只想在运行特定测试或更好的是运行配置中的所有测试期间进行路径修改。
我尝试使用测试运行配置安装脚本来执行此操作,但正如我所料,因为它是一个批处理文件,一旦退出,更改就会丢失,因此无法工作。
是否有其他方法可以设置在所有测试运行期间都有效的临时系统环境变量?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然对这个解决方案不满意,但我能够通过使用测试类的 ClassInitializeAttribute 来使用 MSTest 完成所需的工作,然后使用 Environment.SetEnvironmentVariable 进行我需要的更改,然后在装饰的方法中清理它与 ClassCleanup 属性。
由于缺乏更好的答案,这就是我如何为一组测试设置环境变量并在完成后清理它。不过,我更希望在代码之外处理此问题,并以某种方式成为测试配置的一部分。不管怎样问题都已经解决了。
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.
如果您相信您的测试套件不会在测试中“中止”,您可以使用 FixtureSetup 和 FixtureTeardown 方法来设置然后删除更改的环境变量。
从评论中编辑:我知道您来自哪里,但正如在我的编辑中一样,UT 框架设计用于创建单元测试。单元测试的概念规定它不应依赖于任何外部资源,包括环境变量。执行此操作的测试是集成测试,需要大量基础设施到位(并且通常比相同 LOC 的单元测试套件花费的时间长很多倍)。
要为依赖于环境变量的代码创建单元测试,请考虑拆分实际直接检查环境变量的代码行。并将其放入另一个类中的方法中,然后使用 RhinoMocks 或其他方式模拟该类,以提供用于测试的“虚拟”值,而无需检查(或更改)实际环境变量。
如果这确实是一个集成测试并且您确实需要设置环境变量(假设您正在更改路径,以便可以使用 Process.Start 调用您自己的 notepad.exe 而不是 Windows'),那么这就是 FixtureSetup 和 FixtureTeardown 方法/属性用于;执行固定的、可重复的环境的复杂设置,在该环境中测试应该成功,然后将环境重置为原来的方式,无论测试中发生了什么。通常,测试失败会引发异常并立即结束测试的执行,因此不保证测试方法本身末尾的代码能够运行。
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.