hide test method in child test class

167 views Asked by At

I have two test classes where one inherits from the other, like this:

[TestClass]
public class TestClassBase
{
    // does some actual testing (load file, call methods, do Asserts and such)
    protected virtual DoTest(string fileName) { ... }

    [TestMethod]
    public void TestFileFoo() { DoTest("fooFile"); }

    [TestMethod]
    public void TestFileBar() { DoTest("barFile"); }

    ...
}

[TestClass]
public class TestClassChild : TestClassBase
{
    // is just a little bit different from base method
    protected override DoTest(string fileName) { ... }
}

I am doing it this way because the TestClassBase has around 100 tests that test a certain aspect, and now I added a new functionality that I want to test in the same 100 files in TestClassChild. Copy-pasting (i.e. using two classes not connected via inheritance) is no option from maintainability perspective. Some tests are marked with [Ignore] (they are not ready yet) which is fine with inheritance because the new functionality is a special case of the old one, i.e. if the old one does not work as expected, it does not make sense to test the new one.

But there is a little difference that doesn't work well with inheritance: There are some tests (in fact just one) that should be done in TestClassBase but not in TestClassChild. Is there a way to achieve that?

Own thoughts:

  • I thought I could make that test just private instead of public, but this does not get along with [TestMethod] (which needs public).

  • Making it virtual and overriding it with an empty method would kind of work since the empty method will always pass, but it would confuse others who look at my code. We do not want tests to appear in the test list of TestClassChild which are not real tests.

  • It would be nice to have [TestMethod] conditional in some way, e.g. so that it is only "active" if a certain variable has a certain vaule. Then I could set the variable accordingly in the two test classes.

  • Maybe the conditionality could be achieved with precompiler directives? Defining symbols with #define and put an #if/#endif around the [TestMethod] line? But how to define the symbol differently in the two classes?

0

There are 0 answers