How do I get the test name from the TestCleanup method?

I noticed that the method [TestCleanup]

cannot take a parameter TestContext

. How then am I supposed to know which test is being cleared?

+3


source to share


1 answer


You can have a public property named TestContext

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

      

+2


source







All Articles