Awesome-copilot csharp-xunit
Get best practices for XUnit unit testing, including data-driven tests
install
source · Clone the upstream repo
git clone https://github.com/github/awesome-copilot
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/github/awesome-copilot "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/csharp-xunit" ~/.claude/skills/github-awesome-copilot-csharp-xunit-7a2d02 && rm -rf "$T"
manifest:
skills/csharp-xunit/SKILL.mdsource content
XUnit Best Practices
Your goal is to help me write effective unit tests with XUnit, covering both standard and data-driven testing approaches.
Project Setup
- Use a separate test project with naming convention
[ProjectName].Tests - Reference Microsoft.NET.Test.Sdk, xunit, and xunit.runner.visualstudio packages
- Create test classes that match the classes being tested (e.g.,
forCalculatorTests
)Calculator - Use .NET SDK test commands:
for running testsdotnet test
Test Structure
- No test class attributes required (unlike MSTest/NUnit)
- Use fact-based tests with
attribute for simple tests[Fact] - Follow the Arrange-Act-Assert (AAA) pattern
- Name tests using the pattern
MethodName_Scenario_ExpectedBehavior - Use constructor for setup and
for teardownIDisposable.Dispose() - Use
for shared context between tests in a classIClassFixture<T> - Use
for shared context between multiple test classesICollectionFixture<T>
Standard Tests
- Keep tests focused on a single behavior
- Avoid testing multiple behaviors in one test method
- Use clear assertions that express intent
- Include only the assertions needed to verify the test case
- Make tests independent and idempotent (can run in any order)
- Avoid test interdependencies
Data-Driven Tests
- Use
combined with data source attributes[Theory] - Use
for inline test data[InlineData] - Use
for method-based test data[MemberData] - Use
for class-based test data[ClassData] - Create custom data attributes by implementing
DataAttribute - Use meaningful parameter names in data-driven tests
Assertions
- Use
for value equalityAssert.Equal - Use
for reference equalityAssert.Same - Use
/Assert.True
for boolean conditionsAssert.False - Use
/Assert.Contains
for collectionsAssert.DoesNotContain - Use
/Assert.Matches
for regex pattern matchingAssert.DoesNotMatch - Use
orAssert.Throws<T>
to test exceptionsawait Assert.ThrowsAsync<T> - Use fluent assertions library for more readable assertions
Mocking and Isolation
- Consider using Moq or NSubstitute alongside XUnit
- Mock dependencies to isolate units under test
- Use interfaces to facilitate mocking
- Consider using a DI container for complex test setups
Test Organization
- Group tests by feature or component
- Use
for categorization[Trait("Category", "CategoryName")] - Use collection fixtures to group tests with shared dependencies
- Consider output helpers (
) for test diagnosticsITestOutputHelper - Skip tests conditionally with
in fact/theory attributesSkip = "reason"