Search code examples
c#.netunit-testingtestingtdd

Does .NET create instance per Test Method?


My question is about the way .NET running test methods. Let's say that I have a test class like this:

[TestClass]
public class MyTestClass
{
    [TestMethod]
    public void Test1()
        {
            ...
        }

    [TestMethod]
    public void Test2()
        {
            ...
        }
}

Question; does .NET create an instance per each of these test methods? Or it will create just one instance of MyTestClass and will run all the test methods over the same instance?


Solution

  • "MSTest instantiates each test method’s class separately during the execution process, with each instantiation occurring on a separate thread"

    https://blogs.msdn.microsoft.com/nnaderi/2007/02/16/that-pesky-mstest-execution-ordering/