Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out the next test method to execute in MS TestInitialize

I keep the test data for specific test method in folder named the same as function. I previously had the same function call in each [TestMethod], ClearAllAndLoadTestMethodData() which determined the method name via StackTrace. Now, I moved this function to [TestInitialize]. How can I find the name of the method that is about to be executed?

I thought TestContext provide this. I have access to it via [AssemblyInitialize()] and on first run its property Name is set to name of the testmethod. However, later this doesn't change (if I save the object in static field).

like image 364
majkinetor Avatar asked Aug 30 '12 09:08

majkinetor


1 Answers

The AssemblyInitialize method is executed only once before all your tests.

Use the TestContext inside the TestInitialize method:

[TestClass]
public class TestClass
{
    [TestInitialize]
    public void TestIntialize()
    {
        string testMethodName = TestContext.TestName;
    }

    [TestMethod]
    public void TestMethod()
    {
    }

    public TestContext TestContext { get; set; }
}
like image 144
chaliasos Avatar answered Sep 25 '22 15:09

chaliasos