writing-mstest-tests

Write and modernize MSTest unit tests using MSTest 3.x/4.x assertion APIs.

Updated Jul 12, 2026
One-click install
npx skills add https://github.com/Patrick-Rex/DotNetTechSamples --skill writing-mstest-tests-patrick-rex
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: writing-mstest-tests
Source: https://github.com/Patrick-Rex/DotNetTechSamples/tree/main/.agents/plugins/dotnet-test/skills/writing-mstest-tests
Command: npx skills add https://github.com/Patrick-Rex/DotNetTechSamples --skill writing-mstest-tests-patrick-rex

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing MSTest unit tests often leads to outdated patterns like [ExpectedException], generic Assert.IsTrue checks, swapped Assert.AreEqual arguments, and MSTESTxxxx analyzer warnings. This Skill guides you to write, fix, and modernize MSTest tests using current MSTest 3.x/4.x APIs and conventions. ## Core Features & Use Cases - Modern assertion APIs: Replace Assert.IsTrue with specific assertions like Assert.IsEmpty, Assert.HasCount, Assert.Contains, Assert.IsInstanceOfType, and Assert.ThrowsExactly for clearer failure messages. - Data-driven and lifecycle patterns: Use DataRow, DynamicData with ValueTuples, constructor-based initialization, TestContext injection, cancellation tokens, retries, conditional execution, and parallelization. - Analyzer diagnostics fixes: Look up common MSTESTxxxx rules (e.g., MSTEST0006, MSTEST0017, MSTEST0037) and apply the idiomatic fix instead of suppressing warnings. - Use Case: You inherit a test project full of Assert.IsTrue(list.Count > 0) and [ExpectedException] attributes. Use this Skill to convert them to Assert.IsNotEmpty and Assert.ThrowsExactly<T>, then resolve the remaining analyzer warnings. ## Quick Start Ask the assistant to write MSTest unit tests for your C# class or to modernize an existing MSTest test file using current assertion APIs.

Frequently Asked Questions about writing-mstest-tests

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write MSTest unit tests in C#?

Create a sealed class marked with [TestClass] and methods marked with [TestMethod], following the Arrange-Act-Assert pattern. Use MSTest.Sdk or the MSTest metapackage, and name tests as MethodName_Scenario_ExpectedBehavior.

What should I use instead of Assert.IsTrue in MSTest?

Use specific assertions that produce better failure messages: Assert.IsNotEmpty for non-empty collections, Assert.HasCount for exact counts, Assert.IsNull for null checks, and Assert.Contains for membership. These replace generic boolean assertions.

How do I test exceptions in MSTest without ExpectedException?

Use Assert.ThrowsExactly<T> for synchronous code or Assert.ThrowsExactlyAsync<T> for async code. Assert.Throws<T> matches derived types, while ThrowsExactly matches only the exact exception type, and both return the exception for further assertions.

Does MSTest support data-driven tests with DynamicData?

Yes, MSTest 3.7+ supports DynamicData with ValueTuple return types for type-safe test data, preferred over IEnumerable<object[]>. You can also use TestDataRow<T> to attach metadata like DisplayName per test case.

How do I fix MSTESTxxxx analyzer warnings?

MSTest analyzers ship with the MSTest metapackage and MSTest.Sdk, and most rules have automated code fixes in Visual Studio. Common fixes include replacing ExpectedException (MSTEST0006) and correcting swapped AreEqual arguments (MSTEST0017).

When should I not use this MSTest writing approach?

Do not use it for test quality audits, running tests, or version migrations between MSTest v1/v2/v3/v4, which are separate skills. It also does not apply to xUnit, NUnit, TUnit, or non-.NET languages.