Språk
Rediger

MSTEST0085: '[TestClass]' should not be applied to abstract classes

Property Value
Rule ID MSTEST0085
Title [TestClass] should not be applied to abstract classes
Category Usage
Fix is breaking or non-breaking Non-breaking
Enabled by default Yes
Default severity Info
Introduced in version 4.5.0 (preview)
Is there a code fix No

Cause

An abstract class declares TestClassAttribute or an attribute that derives from it.

Rule description

MSTest can't instantiate or discover an abstract class as a test class, so [TestClass] has no effect on the abstract type. Test methods declared in an abstract base class are discovered through concrete derived classes. Each concrete class must declare its own [TestClass] attribute because test-class attributes aren't inherited.

[TestClass] // Violation
public abstract class SharedTests
{
    [TestMethod] public void CommonTest() { }
}

How to fix violations

Remove [TestClass] from the abstract base class, and apply it to each concrete derived test class.

public abstract class SharedTests { }

[TestClass]
public sealed class ConcreteTests : SharedTests { }

When to suppress warnings

Don't suppress this rule. The attribute on the abstract class doesn't make the class discoverable, and concrete derived classes still require [TestClass].

Suppress a warning

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

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

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

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

See also