$npx -y skills add github/awesome-copilot --skill csharp-xunitGet best practices for XUnit unit testing, including data-driven tests
| 1 | # XUnit Best Practices |
| 2 | |
| 3 | Your goal is to help me write effective unit tests with XUnit, covering both standard and data-driven testing approaches. |
| 4 | |
| 5 | ## Project Setup |
| 6 | |
| 7 | - Use a separate test project with naming convention `[ProjectName].Tests` |
| 8 | - Reference Microsoft.NET.Test.Sdk, xunit, and xunit.runner.visualstudio packages |
| 9 | - Create test classes that match the classes being tested (e.g., `CalculatorTests` for `Calculator`) |
| 10 | - Use .NET SDK test commands: `dotnet test` for running tests |
| 11 | |
| 12 | ## Test Structure |
| 13 | |
| 14 | - No test class attributes required (unlike MSTest/NUnit) |
| 15 | - Use fact-based tests with `[Fact]` attribute for simple tests |
| 16 | - Follow the Arrange-Act-Assert (AAA) pattern |
| 17 | - Name tests using the pattern `MethodName_Scenario_ExpectedBehavior` |
| 18 | - Use constructor for setup and `IDisposable.Dispose()` for teardown |
| 19 | - Use `IClassFixture<T>` for shared context between tests in a class |
| 20 | - Use `ICollectionFixture<T>` for shared context between multiple test classes |
| 21 | |
| 22 | ## Standard Tests |
| 23 | |
| 24 | - Keep tests focused on a single behavior |
| 25 | - Avoid testing multiple behaviors in one test method |
| 26 | - Use clear assertions that express intent |
| 27 | - Include only the assertions needed to verify the test case |
| 28 | - Make tests independent and idempotent (can run in any order) |
| 29 | - Avoid test interdependencies |
| 30 | |
| 31 | ## Data-Driven Tests |
| 32 | |
| 33 | - Use `[Theory]` combined with data source attributes |
| 34 | - Use `[InlineData]` for inline test data |
| 35 | - Use `[MemberData]` for method-based test data |
| 36 | - Use `[ClassData]` for class-based test data |
| 37 | - Create custom data attributes by implementing `DataAttribute` |
| 38 | - Use meaningful parameter names in data-driven tests |
| 39 | |
| 40 | ## Assertions |
| 41 | |
| 42 | - Use `Assert.Equal` for value equality |
| 43 | - Use `Assert.Same` for reference equality |
| 44 | - Use `Assert.True`/`Assert.False` for boolean conditions |
| 45 | - Use `Assert.Contains`/`Assert.DoesNotContain` for collections |
| 46 | - Use `Assert.Matches`/`Assert.DoesNotMatch` for regex pattern matching |
| 47 | - Use `Assert.Throws<T>` or `await Assert.ThrowsAsync<T>` to test exceptions |
| 48 | - Use fluent assertions library for more readable assertions |
| 49 | |
| 50 | ## Mocking and Isolation |
| 51 | |
| 52 | - Consider using Moq or NSubstitute alongside XUnit |
| 53 | - Mock dependencies to isolate units under test |
| 54 | - Use interfaces to facilitate mocking |
| 55 | - Consider using a DI container for complex test setups |
| 56 | |
| 57 | ## Test Organization |
| 58 | |
| 59 | - Group tests by feature or component |
| 60 | - Use `[Trait("Category", "CategoryName")]` for categorization |
| 61 | - Use collection fixtures to group tests with shared dependencies |
| 62 | - Consider output helpers (`ITestOutputHelper`) for test diagnostics |
| 63 | - Skip tests conditionally with `Skip = "reason"` in fact/theory attributes |