1

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?

Kjara
  • 2,504
  • 15
  • 42
  • 1
    make a third class and put the method that shouldn't run for TestClassChild there – Rand Random Apr 26 '21 at 08:50
  • 1
    What are you trying to achieve? You should be testing specifics with your tests, why should a derived class be made to test for another aspect. Are you trying to provide some context to the tests which differ per implementation? – Silvermind Apr 26 '21 at 08:52
  • btw typo in `TestClassChild` shouldn't it be override instead of virtual? – Rand Random Apr 26 '21 at 08:53
  • Doesnt having common methods in test cases (because of two or more child classes inheriting same base class) point to unnecesary/duplicated tests? The test would be executed (same test, same logic) N times, where N is the number of child classes – Cleptus Apr 26 '21 at 09:04
  • @Cleptus Since the actual testing methods differ in every class (note the override), there are no duplicates. – Kjara Apr 26 '21 at 09:07
  • @Kjara if the logic of the methods differ, then the naming of the methods should be more specific, eliminating the need of inheritance – Cleptus Apr 26 '21 at 09:12

0 Answers0