我以下列方式在同一个测试程序集中有 N 个 MSTest 测试类和方法(它使用相同的静态变量)。
[TestClass]
public class TestClass1
{
[TestMethod]
public void TestMethod1A()
{
MyClass.StaticVariable = 0;
MyClass.StaticVariable = MyClass.StaticVariable + 1;
Assert.AreEqual(1, MyClass.StaticVariable)
}
[TestMethod]
public void TestMethod1B()
{
MyClass.StaticVariable = 0;
MyClass.StaticVariable = MyClass.StaticVariable + 1;
Assert.AreEqual(1, MyClass.StaticVariable)
}
}
[TestClass]
public class TestClass2
{
[TestMethod]
public void TestMethod2A()
{
MyClass.StaticVariable = 0;
MyClass.StaticVariable = MyClass.StaticVariable + 1;
Assert.AreEqual(1, MyClass.StaticVariable)
}
[TestMethod]
public void TestMethod2B()
{
MyClass.StaticVariable = 0;
MyClass.StaticVariable = MyClass.StaticVariable + 1;
Assert.AreEqual(1, MyClass.StaticVariable)
}
}
这些测试能保证通过吗?我的观点是 MSTest 是否总是同步执行测试方法,只允许 MyClass.StaticVariable 在被断言之前被初始化和递增一次?会不会出现以下场景?
1. TestMethod1A makes MyClass.StaticVariable 0
2. TestMethod2B increments MyClass.StaticVariable by 1
3. TestMethod1A increments MyClass.StaticVariable by 1 (making the value equal to 2)
4. TestMethod1A asserts (Fail!)