Skip to content

Latest commit

 

History

History
60 lines (44 loc) · 1.88 KB

File metadata and controls

60 lines (44 loc) · 1.88 KB

PH2017: Avoid ClassInitialize

Property Value
Package Philips.CodeAnalysis.MsTestAnalyzers
Diagnostic ID PH2017
Category MsTest
Analyzer AvoidAttributeAnalyzer
CodeFix No
Severity Error
Enabled By Default Yes

Introduction

The order that ClassInitialize methods execute is not deterministic and can create unexpected test results. Avoid state across tests. Moreover, it circumvents TestTimeouts.

How to solve

Keep your test architecture simple. Remove the method. Rely on standard coding techniques to call the method at the beginning of your test.

Example

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 Equivalent

Microsoft's official MSTest analyzers provide equivalent functionality:

Microsoft Rule Description
MSTEST0010 ClassInitialize should be valid

Migration

Consider migrating to Microsoft's official MSTest analyzers which provide equivalent or better functionality with official support.

Configuration

This analyzer does not offer any special configuration. The general ways of suppressing diagnostics apply.