Edit

MSTEST0069: Inherited [TestClass] is ignored by the MSTest source generator

Property Value
Rule ID MSTEST0069
Title Inherited [TestClass] is ignored by the MSTest source generator
Category Usage
Fix is breaking or non-breaking Non-breaking
Enabled by default Yes
Default severity Warning
Introduced in version 4.3.0
Is there a code fix No

Note

This analyzer ships in the MSTest.SourceGeneration package and is only loaded for projects that have opted into the MSTest reflection source generator, which is an experimental feature. It doesn't apply when the default reflection-based discovery is used.

Cause

A class is intended as an MSTest test class through a [TestClass] attribute that it inherits from a base class, rather than declaring [TestClass] directly.

Rule description

The MSTest source generator enumerates test classes using ForAttributeWithMetadataName, which doesn't follow inheritance. A class that relies on inheriting [TestClass] from a base type is silently skipped from source-generated discovery and won't be runnable when the source-generated provider is the active reflection backend.

[TestClass]
public class BaseTests
{
}

// Violation: relies on the inherited [TestClass] attribute.
public class DerivedTests : BaseTests
{
    [TestMethod]
    public void Test()
    {
    }
}

How to fix violations

Apply [TestClass] directly to the derived class so it's discovered when source-generated discovery is active.

[TestClass]
public class BaseTests
{
}

[TestClass]
public class DerivedTests : BaseTests
{
    [TestMethod]
    public void Test()
    {
    }
}

When to suppress warnings

Don't suppress warnings from this rule when source generation is enabled, because the affected test class won't be discovered or executed. You can suppress it if the class isn't actually meant to be a test class.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

#pragma warning disable MSTEST0069
// The code that's violating the rule is on this line.
#pragma warning restore MSTEST0069

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

[*.{cs,vb}]
dotnet_diagnostic.MSTEST0069.severity = none

For more information, see How to suppress code analysis warnings.