$npx -y skills add github/awesome-copilot --skill csharp-nunitGet best practices for NUnit unit testing, including data-driven tests
| 1 | # NUnit Best Practices |
| 2 | |
| 3 | Your goal is to help me write effective unit tests with NUnit, 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, NUnit, and NUnit3TestAdapter 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 | - Apply `[TestFixture]` attribute to test classes |
| 15 | - Use `[Test]` attribute for test methods |
| 16 | - Follow the Arrange-Act-Assert (AAA) pattern |
| 17 | - Name tests using the pattern `MethodName_Scenario_ExpectedBehavior` |
| 18 | - Use `[SetUp]` and `[TearDown]` for per-test setup and teardown |
| 19 | - Use `[OneTimeSetUp]` and `[OneTimeTearDown]` for per-class setup and teardown |
| 20 | - Use `[SetUpFixture]` for assembly-level setup and teardown |
| 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 `[TestCase]` for inline test data |
| 34 | - Use `[TestCaseSource]` for programmatically generated test data |
| 35 | - Use `[Values]` for simple parameter combinations |
| 36 | - Use `[ValueSource]` for property or method-based data sources |
| 37 | - Use `[Random]` for random numeric test values |
| 38 | - Use `[Range]` for sequential numeric test values |
| 39 | - Use `[Combinatorial]` or `[Pairwise]` for combining multiple parameters |
| 40 | |
| 41 | ## Assertions |
| 42 | |
| 43 | - Use `Assert.That` with constraint model (preferred NUnit style) |
| 44 | - Use constraints like `Is.EqualTo`, `Is.SameAs`, `Contains.Item` |
| 45 | - Use `Assert.AreEqual` for simple value equality (classic style) |
| 46 | - Use `CollectionAssert` for collection comparisons |
| 47 | - Use `StringAssert` for string-specific assertions |
| 48 | - Use `Assert.Throws<T>` or `Assert.ThrowsAsync<T>` to test exceptions |
| 49 | - Use descriptive messages in assertions for clarity on failure |
| 50 | |
| 51 | ## Mocking and Isolation |
| 52 | |
| 53 | - Consider using Moq or NSubstitute alongside NUnit |
| 54 | - Mock dependencies to isolate units under test |
| 55 | - Use interfaces to facilitate mocking |
| 56 | - Consider using a DI container for complex test setups |
| 57 | |
| 58 | ## Test Organization |
| 59 | |
| 60 | - Group tests by feature or component |
| 61 | - Use categories with `[Category("CategoryName")]` |
| 62 | - Use `[Order]` to control test execution order when necessary |
| 63 | - Use `[Author("DeveloperName")]` to indicate ownership |
| 64 | - Use `[Description]` to provide additional test information |
| 65 | - Consider `[Explicit]` for tests that shouldn't run automatically |
| 66 | - Use `[Ignore("Reason")]` to temporarily skip tests |