Search code examples
c#inheritancemstest

MSTest can't discover inherited TestMethod in a child Class



I'm using Visual Studio 2015,
My probleme is that MSTest cannot discover the TestMethods that are already defined in base class, when I click "Run Tests" in vs, only the test that are defined in child class are executed.

exemple

[TestClass]
public class A 
{
  [TestMethod]
  public void Test1(){....}
}

[TestClass]
public class B : A
{
  [TestMethod]
  // MsTest only discover this method to execute!
  public void Test2(){....}
}

I want both method to get executed when I click "Run Tests" when I'm in class B, how to solve this? Thanks


Solution

  • You could mark it as virtual and then override it and re-supply the TestMethod attribute on the override and have the method call through to the base implementation.

    [TestClass]
    public class A 
    {
      [TestMethod]
      public virtual void Test1(){....}
    }
    
    [TestClass]
    public class B : A
    {
      [TestMethod]
      public override void Test1()
      {
        base.Test1();
      }
    
      [TestMethod]
      public void Test2(){....}
    }