我在 Visual Studio 2008 中使用 MSTEST。如何让某个测试类中的每个单元测试方法都像运行的第一个测试一样,以便在运行每个测试之前重置所有全局状态?我不想使用 TestInitialize、ClassInitialize、AssemblyInitialize 等显式清理世界。例如:
[TestClass]
public class MyClassTests
{
[TestMethod]
public void Test1()
{
// The "Instance" property creates a new instance of "SomeSingleton"
// if it hasn't been created before.
var i1 = SomeSingleton.Instance;
...
}
[TestMethod]
public void Test2()
{
// When I select "Test1" and "Test2" to run, I'd like Test2
// to have a new AppDomain feel so that the static variable inside
// of "SomeSingleton" is reset (it was previously set in Test1) on
// the call to ".Instance"
var i2 = SomeSingleton.Instance;
// some code
}
尽管在该主题上出现了类似的问题,但它只是澄清了测试不是并行运行的。我意识到测试是连续运行的,但似乎没有办法为每个方法显式强制一个新的 AppDomain (或等同于清除所有状态的东西)。
理想情况下,我只想为我的单元测试的一小部分指定这种行为,这样我就不必为不关心全局状态的测试(绝大多数我的测试)。