Nunit

From G1Joshi/Agent-Skills · View interactive version

Nunit is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert NUnit testing assistance covering .NET assertions, test fixtures, and parameterized testing. Use when writing C# / F# unit tests, testing .NET solutions, or running `dotnet test`.

How to use this skill

Skill file

NUnit

Ported from JUnit, NUnit is one of the oldest and most feature-rich frameworks for .NET. Ideally suited for complex test setups and legacy migrations.

When to Use

Quick Start

using NUnit.Framework;

[TestFixture]
public class Tests
{
    [SetUp]
    public void Setup()
    {
    }

    [Test]
    public void Test1()
    {
        Assert.Pass();
    }

    [TestCase(1, 2, 3)]
    [TestCase(2, 2, 4)]
    public void TestAdd(int a, int b, int expected)
    {
       Assert.AreEqual(expected, a + b);
    }
}

Core Concepts

Constraint-Based Assertion Model

NUnit emphasizes fluent constraint assertions over legacy multiple-assert methods:

using NUnit.Framework;

[TestFixture]
public class AccountTests
{
    [Test]
    public void TestDepositIncreasesBalance()
    {
        var account = new Account(100);
        account.Deposit(50);

        Assert.That(account.Balance, Is.EqualTo(150));
        Assert.That(account.Transactions, Has.Count.EqualTo(1));
        Assert.That(account.Owner, Does.StartWith("Jane"));
    }
}

Parameterized Test Cases (`[TestCase]`)

Passes inline parameters to test multiple input/output permutations:

[TestCase(10, 20, 30)]
[TestCase(-5, 5, 0)]
[TestCase(0, 0, 0)]
public void Add_ReturnsCorrectSum(int a, int b, int expected)
{
    var calculator = new Calculator();
    Assert.That(calculator.Add(a, b), Is.EqualTo(expected));
}

Multiple Assertions Block (`Assert.Multiple`)

Executes all assertions in a block, reporting all failures rather than terminating on the first:

[Test]
public void ValidateUserRecord()
{
    var user = GetUser();
    Assert.Multiple(() =>
    {
        Assert.That(user.FirstName, Is.EqualTo("Jane"));
        Assert.That(user.LastName, Is.EqualTo("Doe"));
        Assert.That(user.IsActive, Is.True);
    });
}

Common Patterns

Parameterized Test Fixture with Setup

Problem: Redundant boilerplate when testing multiple implementations of the same interface.

Solution:

Use [TestCase] with typed expectations:

using NUnit.Framework;

[TestFixture]
public class CalculatorTests
{
    private Calculator _calculator;

    [SetUp]
    public void SetUp()
    {
        _calculator = new Calculator();
    }

    [TestCase(5, 3, 8)]
    [TestCase(-1, 1, 0)]
    [TestCase(0, 0, 0)]
    public void Add_ReturnsExpectedSum(int a, int b, int expected)
    {
        int result = _calculator.Add(a, b);
        Assert.That(result, Is.EqualTo(expected));
    }
}

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :----------------------------------------------------------------- | :--------------------------------------------------------- | :------------------------------------------------------------------------------------------- |

| No test is available in ... Make sure test project has reference | Missing NUnit3TestAdapter NuGet package in test project. | Install NUnit3TestAdapter and Microsoft.NET.Test.Sdk. |

| AssertionException: Expected: ... But was: ... | Calculation result does not match expectation. | Check logic and verify constraints using fluent Assert.That(actual, Is.EqualTo(expected)). |

| System.NullReferenceException in SetUp | Accessing uninitialized fixture field in test execution. | Ensure initialization code is in a [SetUp] method, not in standard constructor. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.