我正在读一本关于单元测试的书,下面在书中被表示为错误代码:
public class CustomerTests
{
private Store _store;
[Fact]
public void Test1()
{
//act
_store = new Store(5);
...
}
[Fact]
public void Test2()
{
//act
_store = new Store(100);
...
}
}
As we know that when xUnit or other testing frameworks, a new instance of test class is created(constructor runs before each test in the class) for each test, so in my example, since I have two test methods, so there are two instances of CustomerTests
, since they are different object/instance, so _store
instance/property in different instance are therefore also independent, which means one test methods modifies its _store
, the other _store
in other instance is not affected. So they technically are not "shared state", why we need to avoid that?