$npx -y skills add LambdaTest/agent-skills --skill nunit-skillGenerates NUnit 3 tests in C#. Covers Assert.That constraint model, parameterized tests, setup/teardown, and Moq mocking. Use when user mentions "NUnit", "[TestFixture]", "[Test]", "Assert.That", "C# unit test". Triggers on: "NUnit", "[Test]", "Assert.That", "C# test", "NUnit3".
| 1 | # NUnit 3 Testing Skill |
| 2 | |
| 3 | ## Core Patterns |
| 4 | |
| 5 | ### Basic Test |
| 6 | |
| 7 | ```csharp |
| 8 | using NUnit.Framework; |
| 9 | |
| 10 | [TestFixture] |
| 11 | public class CalculatorTests |
| 12 | { |
| 13 | private Calculator _calculator; |
| 14 | |
| 15 | [SetUp] |
| 16 | public void SetUp() => _calculator = new Calculator(); |
| 17 | |
| 18 | [Test] |
| 19 | public void Add_TwoPositiveNumbers_ReturnsSum() |
| 20 | { |
| 21 | Assert.That(_calculator.Add(2, 3), Is.EqualTo(5)); |
| 22 | } |
| 23 | |
| 24 | [Test] |
| 25 | public void Divide_ByZero_ThrowsException() |
| 26 | { |
| 27 | Assert.Throws<DivideByZeroException>(() => _calculator.Divide(10, 0)); |
| 28 | } |
| 29 | |
| 30 | [TearDown] |
| 31 | public void TearDown() { /* cleanup */ } |
| 32 | } |
| 33 | ``` |
| 34 | |
| 35 | ### Constraint Model (Assert.That) |
| 36 | |
| 37 | ```csharp |
| 38 | Assert.That(actual, Is.EqualTo(expected)); |
| 39 | Assert.That(actual, Is.Not.EqualTo(unexpected)); |
| 40 | Assert.That(value, Is.GreaterThan(5)); |
| 41 | Assert.That(value, Is.InRange(1, 10)); |
| 42 | Assert.That(str, Does.Contain("hello")); |
| 43 | Assert.That(str, Does.StartWith("He").IgnoreCase); |
| 44 | Assert.That(collection, Has.Count.EqualTo(3)); |
| 45 | Assert.That(collection, Has.Member("item")); |
| 46 | Assert.That(collection, Is.All.GreaterThan(0)); |
| 47 | Assert.That(obj, Is.Null); |
| 48 | Assert.That(obj, Is.Not.Null); |
| 49 | Assert.That(obj, Is.InstanceOf<MyClass>()); |
| 50 | Assert.That(actual, Is.EqualTo(3.14).Within(0.01)); |
| 51 | Assert.That(() => Method(), Throws.TypeOf<ArgumentException>() |
| 52 | .With.Message.Contains("invalid")); |
| 53 | ``` |
| 54 | |
| 55 | ### Parameterized Tests |
| 56 | |
| 57 | ```csharp |
| 58 | [TestCase(2, 3, 5)] |
| 59 | [TestCase(-1, 1, 0)] |
| 60 | [TestCase(0, 0, 0)] |
| 61 | public void Add_ReturnsCorrectSum(int a, int b, int expected) |
| 62 | { |
| 63 | Assert.That(_calculator.Add(a, b), Is.EqualTo(expected)); |
| 64 | } |
| 65 | |
| 66 | [TestCaseSource(nameof(DivisionCases))] |
| 67 | public void Divide_ReturnsCorrectQuotient(int a, int b, double expected) |
| 68 | { |
| 69 | Assert.That(_calculator.Divide(a, b), Is.EqualTo(expected).Within(0.01)); |
| 70 | } |
| 71 | |
| 72 | private static IEnumerable<TestCaseData> DivisionCases() |
| 73 | { |
| 74 | yield return new TestCaseData(10, 2, 5.0).SetName("10/2=5"); |
| 75 | yield return new TestCaseData(7, 3, 2.33).SetName("7/3=2.33"); |
| 76 | } |
| 77 | ``` |
| 78 | |
| 79 | ### Mocking with Moq |
| 80 | |
| 81 | ```csharp |
| 82 | using Moq; |
| 83 | |
| 84 | [TestFixture] |
| 85 | public class UserServiceTests |
| 86 | { |
| 87 | private Mock<IUserRepository> _mockRepo; |
| 88 | private Mock<IEmailService> _mockEmail; |
| 89 | private UserService _service; |
| 90 | |
| 91 | [SetUp] |
| 92 | public void SetUp() |
| 93 | { |
| 94 | _mockRepo = new Mock<IUserRepository>(); |
| 95 | _mockEmail = new Mock<IEmailService>(); |
| 96 | _service = new UserService(_mockRepo.Object, _mockEmail.Object); |
| 97 | } |
| 98 | |
| 99 | [Test] |
| 100 | public void CreateUser_SavesAndSendsEmail() |
| 101 | { |
| 102 | _mockRepo.Setup(r => r.Save(It.IsAny<User>())).Returns(new User { Id = 1 }); |
| 103 | |
| 104 | var result = _service.CreateUser("alice@test.com", "Alice"); |
| 105 | |
| 106 | Assert.That(result.Id, Is.EqualTo(1)); |
| 107 | _mockRepo.Verify(r => r.Save(It.IsAny<User>()), Times.Once); |
| 108 | _mockEmail.Verify(e => e.SendWelcome("alice@test.com"), Times.Once); |
| 109 | } |
| 110 | } |
| 111 | ``` |
| 112 | |
| 113 | ### Lifecycle |
| 114 | |
| 115 | ``` |
| 116 | [OneTimeSetUp] → Before all tests in fixture |
| 117 | [SetUp] → Before each test |
| 118 | [Test] → Test method |
| 119 | [TearDown] → After each test |
| 120 | [OneTimeTearDown] → After all tests in fixture |
| 121 | ``` |
| 122 | |
| 123 | ### Categories and Attributes |
| 124 | |
| 125 | ```csharp |
| 126 | [Test, Category("Smoke")] |
| 127 | public void QuickTest() { } |
| 128 | |
| 129 | [Test, Ignore("Bug #123")] |
| 130 | public void SkippedTest() { } |
| 131 | |
| 132 | [Test, Timeout(5000)] |
| 133 | public void TimeLimitedTest() { } |
| 134 | |
| 135 | [Test, Retry(3)] |
| 136 | public void FlakyTest() { } |
| 137 | ``` |
| 138 | |
| 139 | ### Anti-Patterns |
| 140 | |
| 141 | | Bad | Good | Why | |
| 142 | |-----|------|-----| |
| 143 | | `Assert.AreEqual(a, b)` (classic) | `Assert.That(a, Is.EqualTo(b))` | Constraint model is richer | |
| 144 | | No `[SetUp]` | Proper lifecycle | Resource management | |
| 145 | | Testing private methods | Test public API | Encapsulation | |
| 146 | | No categories | `[Category("Smoke")]` | Run subsets | |
| 147 | |
| 148 | ## Setup: `dotnet add package NUnit NUnit3TestAdapter Microsoft.NET.Test.Sdk` |
| 149 | ## Run: `dotnet test` or `dotnet test --filter TestCategory=Smoke` |
| 150 | |
| 151 | ## Deep Patterns |
| 152 | |
| 153 | For advanced patterns, debugging guides, CI/CD integration, and best practices, |
| 154 | see `reference/playbook.md`. |