Inheritence and UnitTests

Hello.
I have a question about writing UnitTests.
We use csUnit with VS2003, and now going to use VS2005 with internal unit test tool.
We wrote testing code with abstact test classes which realize something like these:

public abstact class MyTestBase
{
public MyTestBase(string sConfigFileName)
{
// initialize test enviroment
}
[Test]
public void Test1()
{}
}

[TestFixture]
public class MyTestOnMSSQL: MyTestBase
{
public MyTestOnMSSQL() : base("config-mssql.xml")
{}
}

[TestFixture]
public class MyTestOnOracle: MyTestBase
{
public MyTestOnOracle() : base("config-oracle.xml")
{}
}

A analogue for TestFixtureAttribute in Microsoft.VisualStudio.TestTools.UnitTesting is TestClassAttribute, for TestAttribute is TestMethod.

We try rewrite these tests on VS2005 TS:


public abstact class MyTestBase
{
public MyTestBase(string sConfigFileName)
{
// initialize test enviroment
}
[TestMethod]
public void Test1()
{}
}

[TestClass]
public class MyTestOnMSSQL: MyTestBase
{
public MyTestOnMSSQL() : base("config-mssql.xml")
{}
}

[TestClass]
public class MyTestOnOracle: MyTestBase
{
public MyTestOnOracle() : base("config-oracle.xml")
{}
}

BUT.. this doesn't work :(
VS requires TestMethodAttribute to be only in class with TestClassAttribute and doesn't understand abstract class with test methods :(

How we should rewrite out test in VS2005 without doubling of test methods



Answer this question

Inheritence and UnitTests

  • VTaneja

    Here is another problem closely related to the reported issue.
    It is the same scenario, but with a little twist - the abstract base class is generic.

    public abstract class GenericBaseFixture<T>
    {
    [TestMethod()]
    public void TestInGenericBaseClass()
    {
    // ...
    }
    }

    [TestClass()]
    public class InheritedFromGenericTestFixture :
    GenericBaseFixture<int>
    {
    [TestMethod()]
    public void TestInClassDerivedFromGeneric1()
    {
    // ...
    }
    }

    [TestClass()]
    public class InheritedFromGenericTestFixture2 :
    GenericBaseFixture<int>
    {
    [TestMethod()]
    public void TestInClassDerivedFromGeneric2()
    {
    // ...
    }
    }

    I expect 4 tests to run here - TestInGenericBaseClass (x2), TestInClassDerivedFromGeneric1 and TestInClassDerivedFromGeneric2.
    Because of the reported limitation VS2005 refuses to run the test in the base class.
    The strange thing is that VS2008 silently skips it and runs only the tests in the derived classes.
    TestDriven.NET has no such issues and runs all 4 tests.


  • Danielyaakove

    Hi Guillermo,

    As for me, this test case works only for classes in the same project. If you separate these classes into two projects within one solution, it doesn't work anymore. Surprisingly Cleanup and Initialize methods of base class are called for any test method in Derived class. But test methods from base class are not visible in Test View. I hope you have some good news regarding this Problem, because i'm very upset now and have to return to old good NUnit, which supports such things for ages


  • angelok2

    Hi, Guillermo,

    I just checked my code again. It works Smile . So I ought to excuse. Sorry for misunderstanding.

    But I also check the issue paha wrote about. Really inheritence of test methods works only inside one project and doesn't work among two projects.



  • SpyderZEX

    You are correct about our current limitation. It is being addressed for the next version and we are investigating the feasibility of making it available earlier is possible.

    Right now you can do the brute force approach listed above at worst.

    At best, for this particular example, you could have just the base class and use the runconfig support to have a deployment file of 'config.xml'. You could then have 2 run configs - one for each database. This would force you to have a test run per config - also not ideal.



  • SQL Wizard

    Hello, Joe.
    Could you explain what "next version" you mean Is this Orcas or something else


    Surely brute force approach isn't acceptable for us. We use such test branching not only by type of DBMS, but other (e.g. by type of transport protocol to out application server)
    Approach with runconfig is more interesting. Thank you.


    I must say what test branching are using not only for testing in different environment configurations, but in case when realizations of tests depend on environment type (e.g. type of DBMS) – in this cases we use overriding of virtual methods in derived classes:

    class abstract MyTestBase
    {
    [Test]
    public virtual void Test1()
    {
    // some base realization of test
    }
    }

    class MyTestMSSQL: MyTestBase
    {
    public override void Test1()
    {
    // here is specific realization for MSSQL
    }
    }

    class MyTestOracle: MyTestBase
    {
    // here we use base realization,
    // so there are not overriding here

    }



  • SamGentile

    By next version I am indeed referring to the next major VS release. We would like to get this out sooner, but this is very much in the air at the moment.

    I'm not sure what you want me to tell you. The functionality isn't there right now. There are work around for everything - but yes - they are quite unwieldy and may be unacceptable for you.

    The simplest work around, assuming work done in inherited classes is minimal, is to configure it via a deployment item and for now collaps the logic into the base class. It is not elegent, nor object oriented.



  • Marcus Perry

    Hi Sergei,

    This actually is part of VS2008. I just verified it with your code and the test method does show up in the Test List Editor.

    Can you verify that this is not working in your installation Also, are you using Side by Side installation(VS2005 and VS2008 in the same machine)

    thanks,

    Guillermo Serrato

    VS Team Test



  • Steve Guidos

    Well, "next major VS relese" is out - VS2008. And what we can see - nothing's changed!

    public abstract class UnitTestBase

    {

    [TestMethod]

    public void TestMethod1()

    {}

    }

    [TestClass]

    public class UnitTestImp : UnitTestBase

    {}

    }

    And there're no any tests in Test List Editor.

    You haven't implemented this so necessary feature for two years. I'm upset.



  • Inheritence and UnitTests