VS Team Test: Multiple Test Initialize Methods in Test Class

.net, c#, unit-testing, visual-studio-2010

Solution

According to MSDN the `TestInitializeAttribute`:

- cannot be used more than once (AllowMultiple = false), and

- cannot be inherited to create your own `TestInitializeAttribute`.

So, my suggestion is to create the Test Initialize Methods without the `TestInitialize` attribute. Then in the unique `TestInitialize` method check which is the current executed `TestMethod` and call the appropriate initialize method:

[TestClass]
public class UnitTest
{
    public TestContext TestContext { get; set; }

    [TestInitialize]
    public void Initialize()
    {
        switch (TestContext.TestName)
        {
            case "TestMethod1":
                this.IntializeTestMethod1();
                break;
            case "TestMethod2":
                this.IntializeTestMethod2();
                break;
            default:
                break;
        }
    }

    [TestMethod]
    public void TestMethod1()
    {
    }

    [TestMethod]
    public void TestMethod2()
    {
    }

    public void IntializeTestMethod1()
    {
        //Initialize Test Method 1
    }

    public void IntializeTestMethod2()
    {
        //Initialize Test Method 2
    }
}

Problem

I have unit test project called “MyClassTest” in TeamTest. This project has three TestMethods. Each method needs its own test initialization steps. But when I apply TestInitializeAttribute to three initialization methods, it says the attribute should not be used more than once. Then what should be the attribute to be used to initialize each test method in Visual Studio Team Test? Reference: VS Team Test: .Net Unit Testing with Excel as Data Source: Adapter Failed How to create Startup and Cleanup script for Visual Studio Test Project? VS 2010 Load Tests Results with custom counters How to log unit test entry and leave in MSTest Can a unit test project load the target application's app.config file?

Original source

Related problems