All skills
github avatar

/csharp-xunit

@90921cc official
by githubgithub/awesome-copilot40k stars
5,040

Get best practices for XUnit unit testing, including data-driven tests

Use this Skill: https://skilld.dev/gh/github/awesome-copilot/csharp-xunit

This session only. Nothing lands on disk.

SKILL.md

≈21 tokens always: the name and description. ≈648 when used: this file.

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., CalculatorTests for Calculator)
  • Use .NET SDK test commands: dotnet test for running tests

Test Structure

  • No test class attributes required (unlike MSTest/NUnit)
  • Use fact-based tests with [Fact] attribute for simple tests
  • Follow the Arrange-Act-Assert (AAA) pattern
  • Name tests using the pattern MethodName_Scenario_ExpectedBehavior
  • Use constructor for setup and IDisposable.Dispose() for teardown
  • Use IClassFixture<T> for shared context between tests in a class
  • Use ICollectionFixture<T> for shared context between multiple test classes

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 [Theory] combined with data source attributes
  • Use [InlineData] for inline test data
  • Use [MemberData] for method-based test data
  • Use [ClassData] for class-based test data
  • Create custom data attributes by implementing DataAttribute
  • Use meaningful parameter names in data-driven tests

Assertions

  • Use Assert.Equal for value equality
  • Use Assert.Same for reference equality
  • Use Assert.True/Assert.False for boolean conditions
  • Use Assert.Contains/Assert.DoesNotContain for collections
  • Use Assert.Matches/Assert.DoesNotMatch for regex pattern matching
  • Use Assert.Throws<T> or await Assert.ThrowsAsync<T> to test exceptions
  • 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 [Trait("Category", "CategoryName")] for categorization
  • Use collection fixtures to group tests with shared dependencies
  • Consider output helpers (ITestOutputHelper) for test diagnostics
  • Skip tests conditionally with Skip = "reason" in fact/theory attributes

Source: SKILL.md on GitHub

1 warning16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    The skill provides comprehensive and safe guidelines for implementing xUnit.net testing in C# projects. It follows industry best practices for test structure, naming conventions, and data-driven testing using standard .NET ecosystem tools.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer7mo

    1/1 file flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 90921cc. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 18 hours ago.

Activeupdated 5 months ago
  • Testing
  • csharp
  • xunit
  • unit-testing
  • data-driven-tests
  • mocking
  • dotnet
  • assertions

README badge

README badge for github/awesome-copilot/csharp-xunit

Provides guidance on writing XUnit unit tests in C# following best practices for standard tests, data-driven tests with InlineData and Theory attributes, and test organization patterns. Covers test structure using Arrange-Act-Assert, fixture setup, assertions, mocking with Moq or NSubstitute, and categorization with traits.

Generated from the current SKILL.md.

Does XUnit require test class attributes like MSTest or NUnit?
No. XUnit does not require any test class attributes, unlike MSTest and NUnit.
How do I run data-driven tests in XUnit?
Use the `[Theory]` attribute combined with data source attributes like `[InlineData]`, `[MemberData]`, or `[ClassData]` to provide test parameters.
How do I share setup and teardown code across tests in XUnit?
Use the constructor for setup and `IDisposable.Dispose()` for teardown. For shared context between multiple tests or classes, use `IClassFixture<T>` or `ICollectionFixture<T>`.
Can I use mocking libraries like Moq with XUnit?
Yes. XUnit works alongside Moq and NSubstitute to mock dependencies and isolate units under test.
How should I name my test methods?
Follow the pattern `MethodName_Scenario_ExpectedBehavior` to clearly express what is being tested and what outcome is expected.

Generated from the current SKILL.md. These answers refresh after source changes.