csharp-mstest

Get best practices for MSTest 3.x/4.x unit testing, including modern assertion APIs and data-driven tests

By github · 8,898 installs

npx skills add github/awesome-copilot --skill csharp-mstest

Source repository · Upstream listing

MSTest Best Practices (MSTest 3.x/4.x) Your goal is to help me write effective unit tests with modern MSTest, using current APIs and best practices. Project Setup Use a separate test project with naming convention [ProjectName].Tests Reference MSTest 3.x+ NuGet packages (includes analyzers) Consider using MSTest.Sdk for simplified project setup Run tests with dotnet test Test Class Structure Use [TestClass] attribute for test classes Seal test classes by default for performance and design clarity Use [TestMethod] for test methods (prefer over [DataTestMethod] ) Follow Arrange Act Assert (AAA) pattern Name tests using pattern MethodName Scenario ExpectedBehavior Test Lifecycle Prefer constructors over [TestInitialize] enables readonly fields and follows standard C patterns Use [TestCleanup] for cleanup that must run even if test fails Combine constructor with async [TestInitialize] when async setup is needed Execution Order 1. Assembly Initialization [AssemblyInitialize] (once per test assembly) 2. Class Initialization [ClassInitialize] (once per test class) 3. Test Initialization (for every test method): 1. Constructor 2. Set TestContext property 3. [TestInitialize] 4. Test Execution test method runs 5. Test Cleanup (for every test method): 1. [TestCleanup] 2. DisposeAsync (if implemented) 3. Dispose (if implemented) 6. Class Cleanup [ClassCleanup] (once per test class) 7. Assembly Cleanup [AssemblyCleanup] (once per test assembly) Modern Assertion APIs MSTest provides three assertion classes: Assert , StringAssert , and CollectionAssert . Assert Class Core Assertions Exception Testing (Prefer over [ExpectedException] ) Collection Assertions (Assert class) String Assertions (Assert class) Comparison Assertions Type Assertions Assert.That (MSTest 4.0+) StringAssert Class Note: Prefer Assert class equivalents when available (e.g., Assert.Contains("expected", actual) over StringAssert.Contains(actual, "expected") ). CollectionAssert Class Note: Prefer Assert class equivalents when available (e.g., Assert.Contains ). Data Driven Tests DataRow DynamicData The data source can return any of the following types: IEnumerable<(T1, T2, ...) (ValueTuple) preferred , provides type safety (MSTest 3.7+) IEnumerable<Tuple<T1, T2, ... provides type safety IEnumerable<TestDataRow provides type safety plus control over test metadata (display name, categories) IEnumerable<object[] least preferred , no type safety Note: When creating new test data methods, prefer ValueTuple or TestDataRow over IEnumerable<object[] . The object[] approach provides no compile time type checking and can lead to runtime errors from type mismatches. TestContext The TestContext class provides test run information, cancellation support, and output methods. See [TestContext documentation](https://learn.microsoft.com/dotnet/core/testing/unit testing mstest writing tests testcontext) for complete reference. Accessing TestContext Cancellation Token Always use TestContext.CancellationToken for cooperative cancellation with [Timeout] : Test Run Properties Output and Result Files Advanced Features Retry for Flaky Tests (MSTest 3.9+) Conditional Execution (MSTest 3.10+) Skip or run tests based on OS or CI environment: Parallelization Work Item Traceability (MSTest 3.8+) Link tests to work items for traceability in test reports: Work item associations appear in test results and can be used for: Tracing test coverage to requirements Linking bug fixes to regression tests Generating traceability reports in CI/CD pipelines Common Mistakes to Avoid Test Organization Group tests by feature or component Use [TestCategory("Category")] for filtering Use [TestProperty("Name", "Value")] for custom metadata (e.g., [TestProperty("Bug", "12345")] ) Use [Priority(1)] for critical tests Enable relevant MSTest analyzers (MSTEST0020 for constructor preference) Mocking and Isolation Use Moq or NSubstitute for mocking dependencies Use interfaces to facilitate mocking Mock dependencies to isolate units under test