MSTEST0011: ClassCleanup method should have valid layout
Property | Value |
---|---|
Rule ID | MSTEST0011 |
Title | ClassCleanup method should have valid layout |
Category | Usage |
Fix is breaking or non-breaking | Non-breaking |
Enabled by default | Yes |
Default severity | Warning |
Introduced in version | 3.3.0 |
Is there a code fix | Yes |
Cause
A method marked with [ClassCleanup]
should have valid layout.
Rule description
Methods marked with [ClassCleanup]
should follow the following layout to be valid:
- it can't be declared on a generic class without the
InheritanceBehavior
mode is set - it should be
public
- it should be
static
- it should not be
async void
- it should not be a special method (finalizer, operator...).
- it should not be generic
- it should not take any parameter, or starting with MSTest 3.8, it can have a single
TestContext
parameter - return type should be
void
,Task
orValueTask
InheritanceBehavior.BeforeEachDerivedClass
attribute parameter should be specified if the class isabstract
.InheritanceBehavior.BeforeEachDerivedClass
attribute parameter should not be specified if the class issealed
.
The type declaring these methods should also respect the following rules:
- The type should be a
class
. - The
class
should bepublic
orinternal
(if the test project is using the[DiscoverInternals]
attribute). - The
class
shouldn't bestatic
. - If the
class
issealed
, it should be marked with[TestClass]
(or a derived attribute).
- the class should not be generic
How to fix violations
Ensure that the method matches the layout described above.
When to suppress warnings
Do not suppress a warning from this rule. If you ignore this rule, flagged instances will be either skipped or result in runtime error.
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.