csharp-mstest
Has your agent apply best practices in unit testing with MSTest. It teaches modern assertion APIs and data-driven tests.
What it does
This guide teaches how to write efficient code tests using the MSTest tool. It shows best practices for organizing your test code, checking results, and handling errors.
When to use it
- ✓When creating tests to verify if your code works correctly.
- ✓When you need to ensure a specific part of your program behaves as expected.
- ✓When learning to use the MSTest tool for unit testing.
Example of what to ask your AI
“How can I test if the addition function returns the correct value for two positive numbers?”
Want more? Open the sections below.
How to use
The easiest way: copy the message below and paste it into Claude, ChatGPT, Cursor or any AI. It reads the skill and follows the instructions.
Skill link
Ready-to-paste message
How it works
The guide organizes test code into classes and methods, following a clear pattern for writing and verifying results. It uses specific commands to compare values, check for nulls, or verify if an exception was thrown.
Installation
Easiest way: copy the skill link and paste it into Claude, ChatGPT or Cursor asking it to follow the instructions.
In the terminal (Claude Code, Codex, Cursor), in your project folder:
npx skills add github/awesome-copilot --skill csharp-mstestOr download just the file:
curl -L https://raw.githubusercontent.com/github/awesome-copilot/main/skills/csharp-mstest/SKILL.md -o SKILL.mdTechnical content (original SKILL.md)
Original text by the author, straight from GitHub.
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
[TestClass]
public sealed class CalculatorTests
{
[TestMethod]
public void Add_TwoPositiveNumbers_ReturnsSum()
{
// Arrange
var calculator = new Calculator();
// Act
var result = calculator.Add(2, 3);
// Assert
Assert.AreEqual(5, result);
}
}
Test Lifecycle
- Prefer constructors over
[TestInitialize]- enablesreadonlyfields 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
[TestClass]
public sealed class ServiceTests
{
private readonly MyService _service; // readonly enabled by constructor
public ServiceTests()
{
_service = new MyService();
}
[TestInitialize]
public async Task InitAsync()
{
// Use for async initialization only
await _service.WarmupAsync();
}
[TestCleanup]
public void Cleanup() => _service.Reset();
}
Execution Order
- Assembly Initialization -
[AssemblyInitialize](once per test assembly) - Class Initialization -
[ClassInitialize](once per test class) - Test Initialization (for every test method):
- Constructor
- Set
TestContextproperty [TestInitialize]
- Test Execution - test method runs
- Test Cleanup (for every test method):
[TestCleanup]DisposeAsync(if implemented)Dispose(if implemented)
- Class Cleanup -
[ClassCleanup](once per test class) - Assembly Cleanup -
[AssemblyCleanup](once per test assembly)
Modern Assertion APIs
MSTest provides three assertion classes: Assert, StringAssert, and CollectionAssert.
Assert Class - Core Assertions
// Equality
Assert.AreEqual(expected, actual);
Assert.AreNotEqual(notExpected, actual);
Assert.AreSame(expectedObject, actualObject); // Reference equality
Assert.AreNotSame(notExpectedObject, actualObject);
// Null checks
Assert.IsNull(value);
Assert.IsNotNull(value);
// Boolean
Assert.IsTrue(condition);
Assert.IsFalse(condition);
// Fail/Inconclusive
Assert.Fail("Test failed due to...");
Assert.Inconclusive("Test cannot be completed because...");
Exception Testing (Prefer over [ExpectedException])
// Assert.Throws - matches TException or derived types
var ex = Assert.Throws<ArgumentException>(() => Method(null));
Assert.AreEqual("Value cannot be null.", ex.Message);
// Assert.ThrowsExactly - matches exact type only
var ex = Assert.ThrowsExactly<InvalidOperationException>(() => Method());
// Async versions
var ex = await Assert.ThrowsAsync<HttpRequestException>(async () => await client.GetAsync(url));
var ex = await Assert.ThrowsExactlyAsync<InvalidOperationException>(async () => await Method());
Collection Assertions (Assert class)
Assert.Contains(expectedItem, collection);
Assert.DoesNotContain(unexpectedItem, collection);
Assert.ContainsSingle(collection); // exactly one element
Assert.HasCount(5, collection);
Assert.IsEmpty(collection);
Assert.IsNotEmpty(collection);
String Assertions (Assert class)
Assert.Contains("expected", actualString);
Assert.StartsWith("prefix", actualString);
Assert.EndsWith("suffix", actualString);
Assert.DoesNotStartWith("prefix", actualString);
Assert.DoesNotEndWith("suffix", actualString);
Assert.MatchesRegex(@"\d{3}-\d{4}", phoneNumber);
Assert.DoesNotMatchRegex(@"\d+", textOnly);
Comparison Assertions
Assert.IsGreaterThan(lowerBound, actual);
Assert.IsGreaterThanOrEqualTo(lowerBound, actual);
Assert.IsLessThan(upperBound, actual);
Assert.IsLessThanOrEqualTo(upperBound, actual);
…
FAQ
What is a unit test?
It's a small piece of code that checks if another part of your code (usually a function or method) is working as it should.
Why use a separate test project?
It helps keep your main code clean and organized by separating the code that runs the program from the code that checks if it works.
What is the Arrange-Act-Assert pattern?
It's a common way to structure tests: 1. Arrange: Set up everything you need. 2. Act: Run the code you want to test. 3. Assert: Check if the result is what you expect.
Details
Original description by the author
Get best practices for MSTest 3.x/4.x unit testing, including modern assertion APIs and data-driven tests
repo: github/awesome-copilot
Popularity
- GitHub stars
- 40k
- Ranking position
- #5096
- License
- MIT
- Updated
- 10/15/2026
Comments
- …
More skills from this repository
acquire-codebase-knowledge
Helps your agent understand and document an existing code project. It's useful for onboarding new people or getting an overview of the architecture.
github/awesome-copilot
acreadiness-assess
Enables your agent to assess a project's readiness for AI and generate a report. It's useful for understanding how well a project is prepared to use AI tools.
github/awesome-copilot
acreadiness-generate-instructions
Helps your agent create instructions for AI tools based on an assessment. It's useful for improving how AI interacts with code and closing identified gaps.
github/awesome-copilot
acreadiness-policy
Enables your agent to help define or apply policies for AI project assessment. It's useful for standardizing how projects are evaluated and ensuring compliance.
github/awesome-copilot
Similar skills
cpp-testing
Makes your agent write and improve tests for C++ code. It helps set up testing tools and fix errors.
affaan-m/ECC
csharp-testing
Helps your agent create tests for C# and .NET code. It uses best practices to ensure tests are efficient and reliable.
affaan-m/ECC
django-tdd
Enables your agent to apply testing strategies for Django with pytest-django and TDD. It's useful for writing robust tests and ensuring code quality, especially for DRF APIs.
affaan-m/ECC