Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 427
Mohammad Nikravesh Avatar asked Sep 15 '25 05:09

Mohammad Nikravesh


1 Answers

"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/

like image 198
Jocke Avatar answered Sep 17 '25 19:09

Jocke