csharp-mstest

Guides writing MSTest 3.x/4.x unit tests with modern assertions and data-driven patterns.

3|Updated Aug 8, 2026
One-click install
npx skills add https://github.com/Jose-Polanco-Oxte/Echos-Live-Music-Visualizer --skill csharp-mstest-jose-polanco-oxte
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: csharp-mstest
Source: https://github.com/Jose-Polanco-Oxte/Echos-Live-Music-Visualizer/tree/main/.agents/skills/csharp/sub-skills/csharp-mstest
Command: npx skills add https://github.com/Jose-Polanco-Oxte/Echos-Live-Music-Visualizer --skill csharp-mstest-jose-polanco-oxte

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing C# unit tests with outdated MSTest patterns leads to unclear failure messages, obsolete APIs like [ExpectedException], and missed modern features such as retries, conditional execution, and type-safe data-driven tests. ## Core Features & Use Cases - Modern Assertion APIs: Covers Assert.Throws, Assert.ContainsSingle, Assert.IsInstanceOfType, collection/string/comparison assertions, and the MSTest 4.0 Assert.That API. - Data-Driven Tests: Explains DataRow, DynamicData with ValueTuple and TestDataRow for type-safe parameterized tests with custom display names and metadata. - Lifecycle & Advanced Features: Details test execution order, TestContext usage with cancellation tokens, parallelization, retry for flaky tests, OS/CI conditional execution, and work item traceability. - Use Case: When adding tests to a .NET service, apply the Arrange-Act-Assert pattern with sealed test classes, constructor-based setup, and Assert.ThrowsExactly to produce maintainable tests with clear failure diagnostics. ## Quick Start Help me write MSTest unit tests for my C# Calculator class using modern assertion APIs and data-driven test cases.

Frequently Asked Questions about csharp-mstest

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

FAQPage Schema
How do I write data-driven tests in MSTest?

Use [DataRow] attributes for inline values or [DynamicData] pointing to a property returning IEnumerable of ValueTuple or TestDataRow. ValueTuple is preferred since MSTest 3.7 because it provides compile-time type safety, unlike IEnumerable<object[]>.

How to assert exceptions in MSTest without ExpectedException?

Use Assert.Throws<TException> to verify an exception type or its derived types, or Assert.ThrowsExactly<TException> for exact type matching. Async variants Assert.ThrowsAsync and Assert.ThrowsExactlyAsync handle async methods, and the returned exception allows message verification.

What is the difference between MSTest 3.x and 4.x assertions?

MSTest 4.x changes Assert.IsInstanceOfType<T> to return the typed result directly instead of using an out parameter, and adds Assert.That for expression-based assertions with auto-captured failure messages. Most other assertion APIs remain consistent across versions.

Does MSTest support retrying flaky tests?

Yes, MSTest 3.9 and later supports the [Retry(n)] attribute on test methods to automatically re-run failing tests up to n times. This is intended for genuinely flaky tests such as those involving timing or external resources.

Should I use TestInitialize or a constructor in MSTest?

Prefer constructors over [TestInitialize] because they enable readonly fields and follow standard C# patterns. Reserve [TestInitialize] for async setup work, and use [TestCleanup] for cleanup that must run even when a test fails.

How do I run MSTest tests only on specific operating systems?

MSTest 3.10+ provides [OSCondition] to include or exclude tests by operating system, and [CICondition] to run tests only in CI environments or only locally. Combine flags like OperatingSystems.Linux | OperatingSystems.MacOS for multiple targets.