简体   繁体   English

在 MSTest 中每个测试用例之后清除所有 static 数据

[英]Clear all static data after each test case in MSTest

I wan to write tests for my library with MSTest.我想用 MSTest 为我的库编写测试。 After TestCase1 runs, it does somethings, set MyData (private static field of my library's internal class) like: TestCase1 运行后,它会做一些事情,设置 MyData(我的库内部类的私有 static 字段),如:

internal MyClass{private static int MyData;}

When TestCase2 runs, it will re-use that data.当 TestCase2 运行时,它将重新使用该数据。

I have written a Reset method and call it in TestInitialize method content like:我已经编写了一个 Reset 方法并在 TestInitialize 方法内容中调用它,例如:

var type = MyAssembly.GetType("MyClass");
var privateType = new PrivateType(type);
privateType.SetStaticField("MyData", 0);

As you see, the method will reset the static field to 0 but it need to know the name of attribute and class ("MyClass" and "MyData").如您所见,该方法会将 static 字段重置为 0,但它需要知道属性名称和 class(“MyClass”和“MyData”)。

Now, I test with my obfuscated dll, the test will fail because the name will be changed.现在,我用我的混淆 dll 进行测试,测试将失败,因为名称将被更改。 Besides, in side my library, i reference to third party libraries, it may store data in static fields also.此外,在我的库中,我引用了第三方库,它也可以将数据存储在 static 字段中。 It will make the TestCase2 re-used data that was set by TestCase1 and TestCase2 will always fail.它将使TestCase2 重用由TestCase1 设置的数据,而TestCase2 将始终失败。

How can i reset all static fields at TestInitialize?如何在 TestInitialize 中重置所有 static 字段?

** UPDATE: ** 更新:

I can see a solution that i can run each test case on a separate AppDomain, it may slow but i may try.我可以看到一个解决方案,我可以在单独的 AppDomain 上运行每个测试用例,它可能会变慢,但我可以尝试。 The issue is i don't know how to run each test case in a separated AppDomain correctly?问题是我不知道如何在单独的 AppDomain 中正确运行每个测试用例?

I found NO better solution after days of reading documents and examples.经过几天阅读文档和示例后,我发现没有更好的解决方案。 I have discussed with some colleagues and i think it should be an issue of my tested library's design, not an issue of my test design.我和一些同事讨论过,我认为这应该是我测试库设计的问题,而不是我的测试设计问题。

Saving data in library static field and change it will keep that data coupled with the running process or instance.将数据保存在库 static 字段中并更改它将使该数据与正在运行的进程或实例保持耦合。 In real life, if user run 2 (or more) processes (instances) of application that use the library, that static data will easily be changed in an unpredictable way and a huge disaster comes.在现实生活中,如果用户运行 2 个(或更多)使用该库的应用程序的进程(实例),那么 static 数据将很容易以不可预知的方式更改,并带来巨大的灾难。 So, solving my test issue by clearing static data is just a "let the code go" trick and that tricky solution hides the real issue inside the code - more dangerous.因此,通过清除 static 数据来解决我的测试问题只是一个“让代码运行”的技巧,而这个棘手的解决方案将真正的问题隐藏在代码中——更危险。

Saving data in a static field is really BAD idea . 将数据保存在 static 字段中确实是个坏主意 DON'T do this if that data is NOT STATIC.如果该数据不是 STATIC,请不要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用MSTest中的属性数据运行测试用例? - How to run test case with data from property in MSTest? 如何在每个 class 之后运行 ClassCleanup (MSTest) 并进行测试? - How to run ClassCleanup (MSTest) after each class with test? 在Visual Studio单元测试(MSTest)中对项目中的所有测试进行每次测试之前,是否可以执行初始化代码? - Is there a way to execute initializer code before each test in Visual Studio Unit Testing (MSTest) for all tests in a project? MsTest - 在程序集中的每个测试之前执行方法 - MsTest - executing method before each test in an assembly 测试删除后的MSTest空目录 - MSTest empty directory after test deleted 如何在所有测试之前执行一行代码,这是MSTest中的数据设置代码 - How to execute a line of code which is a data setup code in MSTest before all test 我可以强制MSTest为每次测试运行使用新进程吗? - Can I force MSTest to use a new process for each test run? 在MSTest中,如何指定某些测试方法不能彼此并行运行? - In MSTest, how can I specify that certain test methods cannot be run in parallel with each other? 使用mstest,我可以针对我支持的每种语言运行单元测试套件吗? - With mstest, can I run my unit test suite against each of my supported languages? 如何清除列表框中的所有数据? - How to clear all data in a listBox?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM