locked
TestInitilize inheritance problem in Data driven unit tests RRS feed

  • Question

  • Hi,

    We recently have started using the data driven unit tests in our development in Visual Studio 2010.

    Before using the data driven option, we had a base class with methods marked with [TestInitilize] and [TestCleanup], and many other classes derived from the base class with the actual tests marked with [TestMethod]. And it worked as expected - for every test that was running the execution flow was:

    1. [TestInitialize] (defined in base class)
    2. [TestMethod] (defined in main test class)
    3. [TestCleanup] (defined in base class)

    The problem started when we added a data source for the tests. Here is the simple example that shows our problem:

    	[TestClass]
    public class RootUnitTest
    {
    public TestContext TestContext { getset; }

    [TestInitialize]
    public void RootInit()
    {
    TestContext.WriteLine(TestContext.TestName + ": RootInit");
    }

    [TestCleanup]
    public void RootCleanup()
    {
    TestContext.WriteLine(TestContext.TestName + ": RootCleanup");
    }
    }

    [TestClass]
    public class MainUnitTest : RootUnitTest
    {
    [TestInitialize]
    public void MainInit()
    {
    TestContext.WriteLine(TestContext.TestName + ": MainInit");
    }

    [TestCleanup]
    public void MainCleanup()
    {
    TestContext.WriteLine(TestContext.TestName + ": MainCleanup");
    }

    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV""|DataDirectory|\\Data.csv""Data#csv"DataAccessMethod.Sequential), DeploymentItem("DataDrivenUT\\Data.csv"), TestMethod]
    public void Test1()
    {
    int data = Convert.ToInt32(TestContext.DataRow["Data"]);
    TestContext.WriteLine(TestContext.TestName + ": Running test for data " + data);
    }
    }

    The result of running Test1 (with data source file that has 3 rows of data) is :

    Test1: RootInit
    Test1: MainInit
    Test1: Running test for data 0
    Test1: MainCleanup
    Test1: RootCleanup
    Test1: MainInit
    Test1: Running test for data 1
    Test1: MainCleanup
    Test1: MainInit
    Test1: Running test for data 2
    Test1: MainCleanup

    So, as you can see the [TestInitialize] and [TestCleanup] methods from the base class (RootUnitTest) are called only for the first iteration of the test, and for all the other iterations - only the methods of the same class (MainUnitTest) are called. This is strange, because we expected the following:

    Test1: RootInit
    Test1: MainInit
    Test1: Running test for data 0
    Test1: MainCleanup
    Test1: RootCleanup
    Test1: RootInit
    Test1: MainInit
    Test1: Running test for data 1
    Test1: MainCleanup
    Test1: RootCleanup
    Test1: RootInit
    Test1: MainInit
    Test1: Running test for data 2
    Test1: MainCleanup
    Test1: RootCleanup

    Is it a bug in the mstest framework or is it an intended behaviour?

    One way of solving this is to remove the attributes from the RootInit and RootCleanup and call these methods explicitly from the MainInit and MainCleanup ...
    Any other suggestions?

    Thanks.

    Wednesday, February 9, 2011 11:58 AM

All replies

  • Totally a bug.

    MSTest team - please use your own product on real and different projects...

    One of the options is to get connected with test managers on new teams, and either persuade them to use MSTest (but no one likes when they are forced to use tool), OR build something side by side and show your approach to them. Prove them that the can develop and debug faster, that their overnight tests will not blow up, that they get useful logging etc.

    You should know of all deployments within MS and solicit feedback too.

    MS FTE Alumni

    Friday, June 10, 2011 9:01 PM
  • I also have this problem.  It would be nice if at some point in the last year they would have fixed this but at least now I can quit trying to figure out why this is not working.
    Thursday, March 29, 2012 1:59 PM
  • Hi Tanya

    Unfortunately the Visual Studio 2010 has problem with inheritance. I think that you should read follow link to more information

    inheritance problem

    Sincerely you M.Bagheri


    M.Bagheri

    Wednesday, February 27, 2013 6:20 AM
  • Is this fixed in VS2012 ? If not, are there any workaround (except calling init and cleanup for every test class) ?
    Friday, July 5, 2013 3:54 AM
  • Hi, Tanya

    I tried run the unit test by your code in  Visual Studio 2012  and the test result is true.

    Test1: RootInit
    Test1: MainInit
    Test1: Running test for data 0
    Test1: MainCleanup
    Test1: RootCleanup
    Test1: RootInit
    Test1: MainInit
    Test1: Running test for data 1
    Test1: MainCleanup
    Test1: RootCleanup
    Test1: RootInit
    Test1: MainInit
    Test1: Running test for data 2
    Test1: MainCleanup
    Test1: RootCleanup

    But run in visual stdio 2010, the result is same to you, so I think something different between them.

    Thanks

    Best Regards.

    Tuesday, July 9, 2013 2:37 PM