Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TestInitialize and TestCleanup not running before and after each DataRow of a DataTestMethod

Below you can see some code written with Mstest in a Window Phone Unit Test App.
I have a normal TestMethod called TestMethod1, and a DataTestMethod called TestMethod2 which has three DataRows:

[TestClass]
public class UnitTest1
{
    [TestInitialize]
    public void Setup()
    {
        Debug.WriteLine("TestInitialize");
    }

    [TestMethod]
    public void TestMethod1()
    {
        Debug.WriteLine("TestMethod1");
    }

    [DataTestMethod]
    [DataRow("a")]
    [DataRow("b")]
    [DataRow("c")]
    public void TestMethod2(string param)
    {
        Debug.WriteLine("TestMethod2 param=" + param);
    }

    [TestCleanup]
    public void TearDown()
    {
        Debug.WriteLine("TestCleanup");
    }
}

If I run the tests in debug mode (Ctrl+R,Ctrl+T in Visual Studio) I see this in the output panel:

TestInitialize
TestMethod1
TestCleanup
TestInitialize
TestMethod2 param=c
TestMethod2 param=a
TestMethod2 param=b
TestCleanup

As you can see, TestInitialize was executed only twice: once before TestMethod1 and once before TestMethod2 with param c.
It's the same for TestCleanup, which was executed once after TestMethod1 and once at the very end.

I would expect the TestInitialize and TestCleanup to be executed before and after each test, no matter if it's a TestMethod or a DataTestMethod. Otherwise the execution of one test can influence the next one.

I expected it to be like this:

TestInitialize
TestMethod1
TestCleanup
TestInitialize
TestMethod2 param=c
TestCleanup
TestInitialize
TestMethod2 param=a
TestCleanup
TestInitialize
TestMethod2 param=b
TestCleanup

I did not find anyone else with the same problem, what am I doing wrong?

like image 810
stralsi Avatar asked Mar 22 '13 21:03

stralsi


People also ask

Does TestInitialize run for each test?

TestInitialize and TestCleanup are ran before and after each test, this is to ensure that no tests are coupled. If you want to run methods before and after ALL tests, decorate relevant methods with the ClassInitialize and ClassCleanup attributes. Save this answer.

What is ClassInitialize?

ClassInitialize runs only on the initialization of the class where the attribute is declared. In other words it won't run for every class. Just for the class that contains the ClassInitialize method. If you want a method that will run once before all tests or classes' initialization use the AssemblyInitialize .

What is aaa unit testing?

Arrange/Act/Assert (AAA) is a pattern for arranging and formatting code in Unit Test methods. It is a best practice to author your tests in more natural and convenient way.

What is AssemblyInitialize?

The method decorated by [AssemblyInitialize] is called once before running the tests of the assembly. The method decorated by [AssemblyCleanup] is called after all tests of the assembly are executed. These methods can be located in any class as long as the class is decorated by [TestClass] .


1 Answers

I am kind of new to this but I think that ff you mark the [DataTestMethod] with the [TestMethod] attribute as well it should run the Initialize and Cleanup for each Test Method.

[TestMethod]
[DataTestMethod]
[DataRow("a")]
[DataRow("b")]
[DataRow("c")]
public void TestMethod2(string param)
{
    Debug.WriteLine("TestMethod2 param=" + param);
}

Update: Microsoft says: TestCleanupAttribute "will be run after methods marked with the TestMethodAttribute ..."

When I test this it does work. Please provide further details when you say it doesn't work.

If you wish initializers that run only once per Test Class you can use the attribute TestClass Attribute. See this post.

// Use ClassInitialize to run code before running the first test in the class
[ClassInitialize()]
public static void MyClassInitialize(TestContext testContext) { }

// Use ClassCleanup to run code after all tests in a class have run
[ClassCleanup()]
public static void MyClassCleanup() { }
like image 77
CaptainBli Avatar answered Oct 13 '22 07:10

CaptainBli