| Property | Value |
|---|---|
| Package | Philips.CodeAnalysis.MsTestAnalyzers |
| Diagnostic ID | PH2017 |
| Category | MsTest |
| Analyzer | AvoidAttributeAnalyzer |
| CodeFix | No |
| Severity | Error |
| Enabled By Default | Yes |
The order that ClassInitialize methods execute is not deterministic and can create unexpected test results. Avoid state across tests. Moreover, it circumvents TestTimeouts.
Keep your test architecture simple. Remove the method. Rely on standard coding techniques to call the method at the beginning of your test.
Code that triggers a diagnostic:
[ClassInitialize()]
public void Initialize()
{ }
[TestMethod]
public void BadTestMethod()
{
Assert.AreEqual(1, 1);
}Fix the above code as follows:
[TestMethod]
public void GoodTestMethod()
{
Initialize();
Assert.AreEqual(1, 1);
}Microsoft's official MSTest analyzers provide equivalent functionality:
| Microsoft Rule | Description |
|---|---|
| MSTEST0010 | ClassInitialize should be valid |
Consider migrating to Microsoft's official MSTest analyzers which provide equivalent or better functionality with official support.
This analyzer does not offer any special configuration. The general ways of suppressing diagnostics apply.