Skip to content

TestCategory on class doesn't apply to inherited test methods #498

Closed

Description

Description

I have a test class that inherits test methods from a base test class, but I want to apply a TestCategory from the derived class in order to filter tests to run. The current implementation only looks at the TestCategory starting at the class that declares the test method, rather than the class being enumerated. But since TestCategory is inherited, I think it would make sense for it to include all categories starting at the class being enumerated.

I have a working implementation of this in a fork and am opening this issue to determine if the change is desired or if it's working as intended.

There's also a question about how assembly-level TestCategory attributes should work in this case. The code currently only considers the assembly containing the test method and not the assemblies containing the test class hierarchy. My fork changes it to only consider the assembly containing the test class being enumerated.

Steps to reproduce

[TestClass]
[TestCategory("base")]
public abstract class Base
{
    [TestMethod]
    public void TestSomeBaseInterfaceMethod() {}
}

[TestClass]
[TestCategory("derived")]
public class Derived : Base {}

Expected behavior

Derived.TestSomeBaseInterfaceMethod has both base and derived test categories:
image

Actual behavior

Derived.TestSomeBaseInterfaceMethod only has the base test category:
image

Environment

MSTest 1.3.2

Activity

  1. karanjitsingh commented on Nov 6, 2018

    @karanjitsingh
    Contributor

    Hey Paul Spangler (@spanglerco), thanks for pointing this out. Marking this as an enhancement and is up for grabs for a contribution.

    Just adding to the expected behavior a little, TestCategory should not override but add another category to the class. A test class can take multiple test categories, so for derived classes defining TestCategory should also show up when filtering for their base class category

    [TestClass]
    [TestCategory("base")]
    public abstract class Base
    {
        [TestMethod]
        public void TestSomeBaseInterfaceMethod() {}
    }
    
    // With test category
    [TestClass]
    [TestCategory("derived")]
    public class Derived : Base {}
    
    // Without test category
    [TestClass]
    public class Derived2 : Base {}

    Using the filter "base" should run both Derived and Derived2 classes whereas using filter "derived" should only run Derived class.

    Will be happy to take contribution with appropriate tests and expected behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedUp for grabs; can be claimed by commenting.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions