2

I have a N number of MSTest test classes and methods in the same test assembly in the following manner (which makes use of the same static variable).

[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)
    }
}

Are these tests guaranteed to pass? My point being does MSTest execute test methods synchonously always allowing MyClass.StaticVariable to be initialized and incremented only once before being asserted? Can the following scenario take place?

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!)
Harindaka
  • 4,658
  • 8
  • 43
  • 62
  • I m not very experienced with MSTest, but I use NUnit quite a lot and my experience has been that NUnit sorts all the tests in an assembly (alphabetically) and goes through all of them Synchronously. I would guess the same goes to MSTest – Has AlTaiar Feb 03 '13 at 10:44
  • If that's the case I'd be a happy camper. I don't even need the tests to run in a certain order, just running synchronously would suffice. – Harindaka Feb 03 '13 at 10:48

1 Answers1

1

MSTest supports multi-threading, but you need to turn it on in the test settings file. By default all tests will run synchronously.

Also, if you want to reset a variable on each test run, there is a attribute you can place on a method and that method will be run before each test in that class:

[TestInitialize()]
public void TestInit()
{
    MyClass.StaticVariable = 0;
}
John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • Thanks John. Just wondering if one could pass parameters to TestInit. What if MyClass.StaticVariable was an object and we needed to do something like MyClass.StaticVariable = new StaticVariableClass(parameter) to reset. If parameter changes with each unit test method, would this still be achievable using TestInitialize? – Harindaka Feb 03 '13 at 14:56
  • @Harindaka If the parameter needs to change with each unit test, then that should be in the test itself and the TestInitialize method really doesn't apply. – John Koerner Feb 03 '13 at 15:18