I'm using MSTest (VS2008) and I need to persist a variable between tests. However the variable gets re-initialized between every test.
According to the third point mentioned in this answer,
MSTest always instantiates a new instance of the test class for each test method being executed.
Is there a straightforward way to keep the value of a variable between tests, or somehow suppress this behaviour?
Use a static member variable:
static int _test = 0;
[TestMethod]
public void __Test1()
{
_test += 1;
Assert.IsTrue(_test == 1);
}
[TestMethod]
public void __Test2()
{
_test += 1;
Assert.IsTrue(_test == 2);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With