npx skills add ...
npx skills add github/awesome-copilot --skill csharp-mstest
Get best practices for MSTest 3.x/4.x unit testing, including modern assertion APIs and data-driven tests
npx skills add github/awesome-copilot --skill csharp-mstest
Your goal is to help me write effective unit tests with modern MSTest, using current APIs and best practices.
[ProjectName].Testsdotnet test[TestClass] attribute for test classes[TestMethod] for test methods (prefer over [DataTestMethod])MethodName_Scenario_ExpectedBehavior[TestInitialize] - enables readonly fields and follows standard C# patterns[TestCleanup] for cleanup that must run even if test fails[TestInitialize] when async setup is needed[AssemblyInitialize] (once per test assembly)[ClassInitialize] (once per test class)TestContext property[TestInitialize][TestCleanup]DisposeAsync (if implemented)Dispose (if implemented)[ClassCleanup] (once per test class)[AssemblyCleanup] (once per test assembly)MSTest provides three assertion classes: Assert, StringAssert, and CollectionAssert.
[ExpectedException])Note: Prefer
Assertclass equivalents when available (e.g.,Assert.Contains("expected", actual)overStringAssert.Contains(actual, "expected")).
Note: Prefer
Assertclass equivalents when available (e.g.,Assert.Contains).
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 safetyIEnumerable<TestDataRow> - provides type safety plus control over test metadata (display name, categories)IEnumerable<object[]> - least preferred, no type safetyNote: When creating new test data methods, prefer
ValueTupleorTestDataRowoverIEnumerable<object[]>. Theobject[]approach provides no compile-time type checking and can lead to runtime errors from type mismatches.
The TestContext class provides test run information, cancellation support, and output methods.
See TestContext documentation for complete reference.
Always use TestContext.CancellationToken for cooperative cancellation with [Timeout]:
Skip or run tests based on OS or CI environment:
Link tests to work items for traceability in test reports:
Work item associations appear in test results and can be used for:
[TestCategory("Category")] for filtering[TestProperty("Name", "Value")] for custom metadata (e.g., [TestProperty("Bug", "12345")])[Priority(1)] for critical tests