writing-mstest-tests
Skill9 starsChanged 6 months agoMITView on GitHub
Best practices for writing new MSTest 3.x/4.x unit tests and implementing concrete fixes in existing MSTest code. Use when the user asks to write, create, implement, repair, or modernize tests (including fix-it prompts such as 'something seems off, fix issues'). Primary fit for direct code changes like correcting swapped Assert.AreEqual argument order, replacing outdated assertion patterns, and converting DynamicData from IEnumerable<object[]> to ValueTuple-based data sets. Covers modern assertions, data-driven tests, test lifecycle, MSTest.Sdk, sealed classes, Assert.Throws, DynamicData with ValueTuples, TestContext, and conditional execution. Do NOT use for broad test quality audits, flaky-test investigations, or test smell detection reports — use test-anti-patterns instead.
1 other project uses a version of this, 1 of them edited.
Adopted by
Most recently taken up by
- microsoft/testfxedited, first seen Oct 9, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 1
- microsoft/testfx★ 1.1k30% same
Wrote their own version · 0
None found.
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository29 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- cli-e2e-testingSkill
- crap-scoreSkill
- detect-static-dependenciesSkill
- dotnet-livecharts2Skill
- dotnet-modern-csharp-editorconfigSkill
- dotnet-test-frameworksSkill
- filter-syntaxSkill
- generate-testability-wrappersSkill
- migrate-static-to-wrapperSkill
- mtp-hot-reloadSkill
- platform-detectionSkill
- run-testsSkill
- test-anti-patternsSkill
- cli-e2e-testingSkill
- crap-scoreSkill
- detect-static-dependenciesSkill
- dotnet-livecharts2Skill
- dotnet-modern-csharp-editorconfigSkill
- dotnet-test-frameworksSkill
- filter-syntaxSkill
- generate-testability-wrappersSkill
- migrate-static-to-wrapperSkill
- mtp-hot-reloadSkill
- platform-detectionSkill
- run-testsSkill
- test-anti-patternsSkill
- writing-mstest-testsSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
15 rules. About 0 are common, 3 are distinctive, and 12 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
12
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 44
Check the test project for MSTest version and configuration:
- Very specific: line 46
If using MSTest.Sdk (<Sdk Name="MSTest.Sdk">): modern setup, all features available
- Very specific: line 47
If using MSTest metapackage: modern setup (MSTest 3.x+)
- Very specific: line 48
If using MSTest.TestFramework + MSTest.TestAdapter: check version for feature availability
- Very specific: line 61
When using MSTest.Sdk, put the version in global.json instead of the project file so all test projects get bumped together:
- Very specific: line 88
Use [TestClass] on the class and [TestMethod] on test methods
- Distinctive: line 89
Follow the Arrange-Act-Assert (AAA) pattern
- Very specific: line 91
Use separate test projects with naming convention [ProjectName].Tests
- Very specific: line 115
Use the correct assertion for each scenario.
- Very specific: line 207
Prefer ValueTuple return types over IEnumerable<object[]> for type safety:
- Very specific: line 227
When you need metadata per test case, use TestDataRow<T>:
- Very specific: line 241
Use [TestInitialize] only for async initialization, combined with the constructor for sync parts
- Very specific: line 242
Use [TestCleanup] for cleanup that must run even on failure
- Very specific: line 284
Always use TestContext.CancellationToken with [Timeout]:
- Distinctive: line 300
Use only for genuinely flaky external dependencies (network, file system), not to paper over race conditions or shared state issues.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Writing MSTest Tests
Help users write effective, modern unit tests with MSTest 3.x/4.x using current APIs and best practices.
When to Use
- User wants to write new MSTest unit tests
- User wants to improve or modernize existing MSTest tests by implementing concrete fixes
- User asks about MSTest assertion APIs, data-driven patterns, or test lifecycle
- User needs help fixing a specific MSTest test bug or failing assertion
- User asks to fix swapped
Assert.AreEqualargument order (expected first, actual second) - User asks to convert
DynamicDatafromIEnumerable<object[]>to ValueTuple-based data
When Not to Use
- User needs a test quality audit, anti-pattern detection, or flaky-test investigation (use
test-anti-patterns) - User needs to run or execute tests (use the
run-testsskill) - User needs to upgrade from MSTest v1/v2 to v3 (use
migrate-mstest-v1v2-to-v3) - User needs to upgrade from MSTest v3 to v4 (use
migrate-mstest-v3-to-v4) - User needs CI/CD pipeline configuration
- User is using xUnit, NUnit, or TUnit (not MSTest)
Inputs
| Input | Required | Description |
| Code under test | No | The production code to be tested |
| Existing test code | No | Current tests to fix, update, or modernize |
| Test scenario description | No | What behavior the user wants to test |
Workflow
Step 1: Determine project setup
Check the test project for MSTest version and configuration:
- If using
MSTest.Sdk(<Sdk Name="MSTest.Sdk">): modern setup, all features available - If using
MSTestmetapackage: modern setup (MSTest 3.x+) - If using
MSTest.TestFramework+MSTest.TestAdapter: check version for feature availability
Recommend MSTest.Sdk or the MSTest metapackage for new projects:
<!-- Option 1: MSTest SDK (simplest, recommended for new projects) -->
<Project Sdk="MSTest.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>
</Project>When using MSTest.Sdk, put the version in global.json instead of the project file so all test projects get bumped together:
{
"msbuild-sdks": {
"MSTest.Sdk": "3.8.2"
}
}<!-- Option 2: MSTest metapackage -->
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MSTest" Version="3.8.2" />
</ItemGroup>
</Project>Step 2: Write test classes following conventions
Apply these structural conventions:
- Seal test classes with
sealedfor performance and design clarity - Use
[TestClass]on the class and[TestMethod]on test methods - Follow the Arrange-Act-Assert (AAA) pattern
- Name tests using
MethodName_Scenario_ExpectedBehavior - Use separate test projects with naming convention
[ProjectName].Tests
[TestClass]
public sealed class OrderServiceTests
{
[TestMethod]
public void CalculateTotal_WithDiscount_ReturnsReducedPrice()
{
// Arrange
var service = new OrderService();
var order = new Order { Price = 100m, DiscountPercent = 10 };
// Act
var total = service.CalculateTotal(order);
// Assert
Assert.AreEqual(90m, total);
}
}Step 3: Use modern assertion APIs
Use the correct assertion for each scenario. Prefer Assert class methods over StringAssert or CollectionAssert where both exist.
Equality and null checks
Assert.AreEqual(expected, actual); // Value equality
Assert.AreSame(expected, actual); // Reference equality
Assert.IsNull(value);
Assert.IsNotNull(value);Exception testing -- use Assert.Throws instead of [ExpectedException]
// Synchronous
var ex = Assert.ThrowsExactly<ArgumentNullException>(() => service.Process(null));
Assert.AreEqual("input", ex.ParamName);
// Async
var ex = await Assert.ThrowsExactlyAsync<InvalidOperationException>(
async () => await service.ProcessAsync(null));Assert.Throws<T>matchesTor any derived typeAssert.ThrowsExactly<T>matches only the exact typeT
Collection assertions
Assert.Contains(expectedItem, collection);
Assert.DoesNotContain(unexpectedItem, collection);
var single = Assert.ContainsSingle(collection); // Returns the single element
Assert.HasCount(3, collection);
Assert.IsEmpty(collection);
Assert.IsNotEmpty(collection);Replace generic Assert.IsTrue with specialized assertions -- they give better failure messages:
| Instead of | Use |
Assert.IsTrue(list.Count > 0) | Assert.IsNotEmpty(list) |
Assert.IsTrue(list.Count() == 3) | Assert.HasCount(3, list) |
Assert.IsTrue(x != null) | Assert.IsNotNull(x) |
list.Single(predicate) + Assert.IsNotNull | Assert.ContainsSingle(list) |
Assert.IsTrue(list.Contains(item)) | Assert.Contains(item, list) |
String assertions
Assert.Contains("expected", actualString);
Assert.StartsWith("prefix", actualString);
Assert.EndsWith("suffix", actualString);
Assert.MatchesRegex(@"\d{3}-\d{4}", phoneNumber);Type assertions
// MSTest 3.x -- out parameter
Assert.IsInstanceOfType<MyHandler>(result, out var typed);
typed.Handle();
// MSTest 4.x -- returns directly
var typed = Assert.IsInstanceOfType<MyHandler>(result);Comparison assertions
Assert.IsGreaterThan(lowerBound, actual);
Assert.IsLessThan(upperBound, actual);
Assert.IsInRange(actual, low, high);Step 4: Use data-driven tests for multiple inputs
DataRow for inline values
[TestMethod]
[DataRow(1, 2, 3)]
[DataRow(0, 0, 0, DisplayName = "Zeros")]
[DataRow(-1, 1, 0)]
public void Add_ReturnsExpectedSum(int a, int b, int expected)
{
Assert.AreEqual(expected, Calculator.Add(a, b));
}DynamicData with ValueTuples (preferred for complex data)
Prefer ValueTuple return types over IEnumerable<object[]> for type safety:
[TestMethod]
[DynamicData(nameof(DiscountTestData))]
public void ApplyDiscount_ReturnsExpectedPrice(decimal price, int percent, decimal expected)
{
var result = PriceCalculator.ApplyDiscount(price, percent);
Assert.AreEqual(expected, result);
}
// ValueTuple -- preferred (MSTest 3.7+)
public static IEnumerable<(decimal price, int percent, decimal expected)> DiscountTestData =>
[
(100m, 10, 90m),
(200m, 25, 150m),
(50m, 0, 50m),
];When you need metadata per test case, use TestDataRow<T>:
public static IEnumerable<TestDataRow<(decimal price, int percent, decimal expected)>> DiscountTestDataWithMetadata =>
[
new((100m, 10, 90m)) { DisplayName = "10% discount" },
new((200m, 25, 150m)) { DisplayName = "25% discount" },
new((50m, 0, 50m)) { DisplayName = "No discount" },
];Step 5: Handle test lifecycle correctly
- Always initialize in the constructor -- this enables
readonlyfields and works correctly with nullability analyzers (fields are guaranteed non-null after construction) - Use
[TestInitialize]only for async initialization, combined with the constructor for sync parts - Use
[TestCleanup]for cleanup that must run even on failure - Inject
TestContextvia constructor (MSTest 3.6+)
[TestClass]
public sealed class RepositoryTests
{
private readonly TestContext _testContext;
private readonly FakeDatabase _db; // readonly -- guaranteed by constructor
public RepositoryTests(TestContext testContext)
{
_testContext = testContext;
_db = new FakeDatabase(); // sync init in ctor
}
[TestInitialize]
public async Task InitAsync()
{
// Use TestInitialize ONLY for async setup
await _db.SeedAsync();
}
[TestCleanup]
public void Cleanup() => _db.Reset();
}Execution order
[AssemblyInitialize]-- once per assembly[ClassInitialize]-- once per class- Per test:
- With
TestContextproperty injection: Constructor -> setTestContextproperty ->[TestInitialize] - With constructor injection of
TestContext: Constructor (receivesTestContext) ->[TestInitialize]
- With
- Test method
[TestCleanup]->DisposeAsync->Dispose-- per test[ClassCleanup]-- once per class[AssemblyCleanup]-- once per assembly
Step 6: Apply cancellation and timeout patterns
Always use TestContext.CancellationToken with [Timeout]:
[TestMethod]
[Timeout(5000)]
public async Task FetchData_ReturnsWithinTimeout()
{
var result = await _client.GetDataAsync(_testContext.CancellationToken);
Assert.IsNotNull(result);
}Step 7: Use advanced features where appropriate
Retry flaky tests (MSTest 3.9+)
Use only for genuinely flaky external dependencies (network, file system), not to paper over race conditions or shared state issues.
[TestMethod]
[Retry(3)]
public void ExternalService_EventuallyResponds() { }Conditional execution (MSTest 3.10+)
[TestMethod]
[OSCondition(OperatingSystems.Windows)]
public void WindowsRegistry_ReadsValue() { }
[TestMethod]
[CICondition(ConditionMode.Exclude)]
public void LocalOnly_InteractiveTest() { }Parallelization
[assembly: Parallelize(Workers = 4, Scope = ExecutionScope.MethodLevel)]
[TestClass]
[DoNotParallelize] // Opt out specific classes
public sealed class DatabaseIntegrationTests { }Validation
- Test classes are
sealed - Test methods follow
MethodName_Scenario_ExpectedBehaviornaming Assert.ThrowsExactly<T>used instead of[ExpectedException]- Specialized assertions used instead of
Assert.IsTrue(e.g.,Assert.IsNotNull,Assert.AreEqual) - DynamicData uses ValueTuple return types instead of
IEnumerable<object[]> - Sync initialization done in the constructor, not
[TestInitialize] TestContext.CancellationTokenpassed to async calls in tests with[Timeout]- Project builds with zero errors and all tests pass
Common Pitfalls
| Pitfall | Solution |
Assert.AreEqual(actual, expected) -- swapped arguments | Always put expected first: Assert.AreEqual(expected, actual). Failure messages show "Expected: X, Actual: Y" so wrong order makes messages confusing |
[ExpectedException] -- obsolete, cannot assert message | Use Assert.Throws<T> or Assert.ThrowsExactly<T> |
items.Single() -- unclear exception on failure | Use Assert.ContainsSingle(items) for better failure messages |
Hard cast (MyType)result -- unclear exception | Use Assert.IsInstanceOfType<MyType>(result) |
IEnumerable<object[]> for DynamicData | Use IEnumerable<(T1, T2, ...)> ValueTuples for type safety |
Sync setup in [TestInitialize] | Initialize in the constructor instead -- enables readonly fields and satisfies nullability analyzers |
CancellationToken.None in async tests | Use TestContext.CancellationToken for cooperative timeout |
public TestContext? TestContext { get; set; } | Drop the ? -- MSTest suppresses CS8618 for this property |
TestContext TestContext { get; set; } = null! | Remove = null! -- unnecessary, MSTest handles assignment |
| Non-sealed test classes | Seal test classes by default for performance |
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

