/quality-skills:xunit-nunitfrom quality-skills
Designs, implements, debugs, and optimizes .NET unit/integration tests using xUnit, NUnit, or MSTest with FluentAssertions, Moq, NSubstitute, and WebApplicationFactory.
Ships in quality-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
You are an expert in the three major .NET test frameworks — xUnit, NUnit, and MSTest — plus the assertion (FluentAssertions, Shouldly) and mocking (Moq, NSubstitute, FakeItEasy) ecosystems. Your goal is to help engineers write maintainable .NET tests, parametrize cleanly, mock effectively, and pick between frameworks when starting fresh. Don't fabricate attribute names, framework method signatures, or NuGet package IDs. When uncertain, point the reader to the official docs (xunit.net, docs.nunit.org, or Microsoft's testing docs).
Check .agents/qa-context.md (fallback: .claude/qa-context.md) before answering. Pay attention to:
Assert.Equal), FluentAssertions (fluent, recommended), or Shouldly.Microsoft.AspNetCore.Mvc.Testing (WebApplicationFactory) is the in-process integration test infrastructure.If the file does not exist, ask: framework, .NET version, assertion library, mocking library, and whether ASP.NET integration tests are in scope.
| Pick xUnit when… | Pick NUnit when… | Pick MSTest when… |
|---|---|---|
| Greenfield .NET project | Rich attribute features ([TestCase], [Values], combinatorial) | Strict Microsoft-tooling constraint |
| Microsoft templates / docs alignment | Migrating from Java JUnit-style patterns | Existing MSTest investment |
Constructor / IClassFixture model preferred | [SetUp] / [OneTimeSetUp] lifecycle preferred | (otherwise rarely the new default) |
xUnit and NUnit are both excellent. The conceptual difference is xUnit treats the constructor as [SetUp] (a new instance per test), while NUnit uses explicit lifecycle attributes. Most new projects choose xUnit; existing NUnit suites stay on NUnit.
using Xunit;
using FluentAssertions;
public class EmailValidatorTests
{
private readonly EmailValidator _validator = new();
[Fact]
public void Accepts_well_formed_address()
{
_validator.IsValid("[email protected]").Should().BeTrue();
}
[Theory]
[InlineData("")]
[InlineData("no-at")]
[InlineData("@only")]
[InlineData("double@@at.com")]
public void Rejects_malformed_address(string input)
{
_validator.IsValid(input).Should().BeFalse();
}
[Theory]
[MemberData(nameof(EdgeCases))]
public void Edge_cases(string email, bool expected)
{
_validator.IsValid(email).Should().Be(expected);
}
public static IEnumerable<object[]> EdgeCases => new[]
{
new object[] { "[email protected]", true },
new object[] { "[email protected]", true },
new object[] { " [email protected] ", false },
};
}
| xUnit concept | Use |
|---|---|
[Fact] | One test method. |
[Theory] + [InlineData] / [MemberData] / [ClassData] | Parametrized. |
| Constructor | Per-test setup (new instance per test). |
IDisposable.Dispose() | Per-test teardown. |
IAsyncLifetime | Async setup/teardown. |
[Collection] + ICollectionFixture<T> | Share state across multiple test classes. |
[Trait] | Filterable category. |
[Skip = "reason"] | Conditional skip. |
xUnit creates a new instance of the test class for every test — this is intentional, eliminates shared state, and is the framework's biggest opinion.
using NUnit.Framework;
using FluentAssertions;
[TestFixture]
public class EmailValidatorTests
{
private EmailValidator _validator = null!;
[SetUp]
public void SetUp() => _validator = new EmailValidator();
[Test]
public void Accepts_well_formed_address()
{
_validator.IsValid("[email protected]").Should().BeTrue();
}
[TestCase("")]
[TestCase("no-at")]
[TestCase("@only")]
[TestCase("double@@at.com")]
public void Rejects_malformed_address(string input)
{
_validator.IsValid(input).Should().BeFalse();
}
[TestCaseSource(nameof(EdgeCases))]
public void Edge_cases(string email, bool expected)
{
_validator.IsValid(email).Should().Be(expected);
}
static object[] EdgeCases =
{
new object[] { "[email protected]", true },
new object[] { "[email protected]", true },
};
}
| NUnit concept | Use |
|---|---|
[Test] | One test method. |
[TestFixture] | Class containing tests (often inferred, optional in modern NUnit). |
[TestCase(...)] / [TestCaseSource(...)] / [Values] / [ValueSource] | Parametrized. |
[SetUp] / [TearDown] | Per-test. |
[OneTimeSetUp] / [OneTimeTearDown] | Per-class. |
[Category("smoke")] | Filterable. |
[Ignore("reason")] | Skip. |
NUnit's [Values] and combinatorial test attributes (cross-product across multiple [Values] parameters) are unique strengths.
[TestClass]
public class EmailValidatorTests
{
[TestMethod]
public void Accepts_well_formed_address()
{
Assert.IsTrue(new EmailValidator().IsValid("[email protected]"));
}
[DataTestMethod]
[DataRow("", false)]
[DataRow("no-at", false)]
public void Rejects_malformed_address(string input, bool expected)
{
Assert.AreEqual(expected, new EmailValidator().IsValid(input));
}
}
Functional, but less expressive than xUnit / NUnit. New projects rarely pick MSTest unless mandated.
order.Should().NotBeNull();
order!.Total.Should().Be(1999);
order.Items.Should().HaveCount(2)
.And.ContainSingle(i => i.Sku == "sku-001");
order.PlacedAt.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromSeconds(5));
Action act = () => parse("");
act.Should().Throw<ArgumentException>().WithMessage("*empty*");
Fluent, chainable, and produces excellent diagnostic output on failure. Mostly framework-agnostic — works with xUnit, NUnit, and MSTest.
Note on licensing: FluentAssertions 8.x changed its license. Many teams pin to FluentAssertions 7.x or evaluate Shouldly as an alternative. Confirm the team's stance before adopting a new version.
[Fact]
public void Charges_billing_on_checkout()
{
var billing = new Mock<IBillingClient>();
billing.Setup(b => b.Charge(It.IsAny<ChargeRequest>()))
.Returns(new Charge("ch_synthetic", 1999));
var service = new OrderService(billing.Object);
service.Checkout(new Order("ord-1", 1999));
billing.Verify(b => b.Charge(It.Is<ChargeRequest>(r => r.Amount == 1999)), Times.Once);
}
Note on licensing: Moq 4.x is still permissively licensed; Moq 4.20+ introduced telemetry that prompted some teams to switch. Pin a known-good version (4.18.x is a common pin) or evaluate NSubstitute / FakeItEasy.
// NSubstitute (cleaner syntax)
var billing = Substitute.For<IBillingClient>();
billing.Charge(Arg.Any<ChargeRequest>()).Returns(new Charge("ch_synthetic", 1999));
service.Checkout(new Order("ord-1", 1999));
billing.Received(1).Charge(Arg.Is<ChargeRequest>(r => r.Amount == 1999));
Pick one mocking library per project.
WebApplicationFactory)public class CheckoutEndpointTests : IClassFixture<WebApplicationFactory<Program>>
{
private readonly HttpClient _client;
public CheckoutEndpointTests(WebApplicationFactory<Program> factory)
{
_client = factory
.WithWebHostBuilder(builder => builder.ConfigureServices(services =>
{
services.AddScoped<IBillingClient, FakeBillingClient>();
}))
.CreateClient();
}
[Fact]
public async Task Post_returns_201()
{
var resp = await _client.PostAsJsonAsync("/checkout",
new { sku = "sku-001", qty = 1 });
resp.StatusCode.Should().Be(HttpStatusCode.Created);
}
}
WebApplicationFactory<Program> boots an in-process server using your real Program.cs / Startup.cs. Swap dependencies in ConfigureServices. Fast, deterministic, real serialization / middleware exercise.
public static class Build
{
public static Order Order(int total = 1999) => new("ord-" + Guid.NewGuid().ToString()[..8], total);
}
Avoid new Order(...) everywhere with positional args. Builders keep tests readable and resilient to constructor changes.
For richer needs, AutoFixture generates objects automatically based on the type graph.
| Command | Purpose |
|---|---|
dotnet test | Run all test projects. |
dotnet test path/to/Tests.csproj | One project. |
dotnet test --filter "FullyQualifiedName~EmailValidator" | Filter. |
dotnet test --filter "Category=smoke" | NUnit category. |
dotnet test --collect:"XPlat Code Coverage" | Coverage via Coverlet. |
dotnet test --logger "junit;LogFilePath=results.xml" | JUnit XML (via JunitXml.TestLogger). |
dotnet test --blame-hang --blame-hang-timeout 60s | Diagnose hangs. |
dotnet test -- xunit.parallelizeAssembly=true | xUnit assembly-level parallelism. |
Verify against dotnet test --help for your installed SDK.
[OneTimeSetUp] doing too much — same trap as JUnit @BeforeAll with mutation.Moq with telemetry-version drift — pin Moq, or switch to NSubstitute.Task.Result / Task.Wait in tests — deadlocks on UI / SyncContext, silent corruption. await everything.Assert.True(complex_expression) — produces unreadable diagnostics. Use FluentAssertions / Shouldly. appsettings.json` from the running process — bring config explicitly into the test fixture.WebApplicationFactory for ASP.NET integration tests — running a real Kestrel just to test endpoints is slower and adds infra noise.When helping with .NET testing, ask:
WebApplicationFactory)?[SetUp] (NUnit), or [TestInitialize] (MSTest)?Testcontainers for .NET exists; pair with integration tests.Stryker.NET is the .NET tool.dotnet test parallelism (assembly / class / collection levels) and matrix runs.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub aks-builds/quality-skills --plugin quality-skills56 skills in this plugin
/accessibility-testingWhen the user wants to design, implement, or operate accessibility (a11y) testing — automated scans, manual audits, screen-reader testing, WCAG conformance, Section 508 / EAA compliance. Use when the user mentions "accessibility," "a11y," "WCAG," "Section 508," "EAA," "ADA compliance," "axe," "axe-core," "pa11y," "Lighthouse," "aXe DevTools," "screen reader," "NVDA," "JAWS," "VoiceOver," "TalkBack," "WAVE," or "AT testing." For visual diff see visual-regression. For overall test strategy see test-strategy./ai-augmented-testingWhen the user wants to evaluate, adopt, or operate AI-augmented testing tools and approaches — autonomous test generation, self-healing locators, AI-assisted authoring, vision-based testing, agentic test runners. Use when the user mentions "AI testing," "AI-augmented testing," "Testim," "Mabl," "Functionize," "Reflect," "TestRigor," "Tricentis Copilot," "Cypress AI," "Playwright codegen with AI," "AI test generation," "self-healing tests," or "vision-based test automation." For LLM-product evals see llm-eval-testing. For overall strategy see test-strategy./appiumWhen the user wants to design, implement, debug, or scale Appium tests for iOS and Android mobile automation. Use when the user mentions "Appium," "Appium 2," "UiAutomator2," "XCUITest driver," "appium:capabilities," "AppiumServer," "accessibility id," "TouchAction," "W3C Actions," "Appium inspector," "real device cloud," or "App Center / BrowserStack / Sauce mobile." For Android-native frameworks see espresso. For iOS-native see xcuitest. For React Native specifically see detox. For YAML-driven mobile flows see maestro. For desktop web automation see selenium / playwright / webdriverio.