Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the test name from a TestCleanup method?

I notice that a [TestCleanup] method cannot take a TestContext parameter. How then I am supposed to know which test is being cleanup up?

like image 494
Taylor Momsen Avatar asked May 24 '17 00:05

Taylor Momsen


1 Answers

You can have a public property named TestContext on your class and that will be set by MSTest, for example :

[TestClass]
public class UnitTest1
{
    public TestContext TestContext { get; set; }
    [TestMethod]
    public void TestMethod1()
    {
        var x = 2;
        var y = 1 + 1;
        Assert.AreEqual(x, y);
    }

    [TestMethod]
    public void TestMethod2()
    {
        Assert.AreEqual(true, true);
    }

    [TestCleanup]
    public void TestCleanup()
    {
        Debug.WriteLine(TestContext.TestName);
    }
}
like image 136
John Koerner Avatar answered Nov 20 '22 20:11

John Koerner