Xunit

From G1Joshi/Agent-Skills · View interactive version

Xunit is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert xUnit.net testing assistance covering .NET Fact/Theory tests, constructor fixtures, and output capture. Use when writing C# unit tests, testing ASP.NET Core applications, or running `dotnet test`.

How to use this skill

Skill file

xUnit.net

xUnit.net is the modern, open-source unit testing tool for .NET (C#, F#, VB). It is chosen by Microsoft/dotnet for their own repositories.

When to Use

Quick Start

using Xunit;

public class CalculatorTests
{
    [Fact]
    public void PassingTest()
    {
        Assert.Equal(4, Add(2, 2));
    }

    [Theory]
    [InlineData(3)]
    [InlineData(5)]
    [InlineData(6)]
    public void MyTheory(int value)
    {
        Assert.True(IsOdd(value));
    }
}

Core Concepts

Instance-Per-Test Lifecycle Architecture

Unlike NUnit and MSTest, xUnit instantiates a completely new instance of the test class for every [Fact], preventing shared instance field state:

[ Test Class ] ──new()──→ Runs Fact 1 ──Dispose()
[ Test Class ] ──new()──→ Runs Fact 2 ──Dispose()

Facts vs Theories (`[Fact]` vs `[Theory]`)

using Xunit;

public class OrderValidatorTests
{
    [Fact]
    public void EmptyOrder_IsInvalid()
    {
        var order = new Order();
        Assert.False(order.IsValid());
    }

    [Theory]
    [InlineData(100, "DISCOUNT10", 90)]
    [InlineData(50, "SAVE5", 45)]
    [InlineData(20, "", 20)]
    public void ApplyDiscount_CalculatesCorrectTotal(decimal price, string code, decimal expected)
    {
        var total = OrderService.CalculateTotal(price, code);
        Assert.Equal(expected, total);
    }
}

Shared Context via Class Fixtures (`IClassFixture<T>`)

Shares expensive setup (database, web test servers) across tests without static state:

public class DatabaseFixture : IDisposable
{
    public SqlConnection Connection { get; }
    public DatabaseFixture() { Connection = new SqlConnection("Server=localhost;..."); Connection.Open(); }
    public void Dispose() { Connection.Dispose(); }
}

public class CustomerRepositoryTests : IClassFixture<DatabaseFixture>
{
    private readonly DatabaseFixture _fixture;
    public CustomerRepositoryTests(DatabaseFixture fixture) => _fixture = fixture;

    [Fact]
    public void QueriesCustomerRecord() { /* Uses _fixture.Connection */ }
}

Common Patterns

Theory with InlineData and ClassData

Problem: Writing duplicate test methods for multiple inputs in C# applications.

Solution:

Use [Theory] with [InlineData]:

using Xunit;

public class MathTests
{
    [Theory]
    [InlineData(2, 3, 5)]
    [InlineData(-1, 1, 0)]
    [InlineData(0, 0, 0)]
    public void Add_ValidInputs_ReturnsExpectedSum(int a, int b, int expected)
    {
        var calculator = new Calculator();
        var result = calculator.Add(a, b);
        Assert.Equal(expected, result);
    }
}

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

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

| No test matches the given testcase filter | Filter string doesn't match namespace or class name. | Run dotnet test --filter FullyQualifiedName~MathTests. |

| Console.WriteLine produces no output | xUnit isolates console output to prevent race conditions. | Inject ITestOutputHelper in constructor and call _output.WriteLine(). |

| Assert.Equal() Failure: Expected ... Actual ... | Output mismatch in assertion. | Review diff and check floating-point precision with tolerance arguments. |

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.