PluginBench
Skill
Pass
Audit score 90

csharp-testing

affaan-m/ecc

C# and .NET testing patterns with xUnit, FluentAssertions, mocking, and integration test best practices.

What is csharp-testing?

Comprehensive guide to writing unit, integration, and API tests for .NET applications using xUnit, FluentAssertions, NSubstitute, and Testcontainers. Use this when setting up test infrastructure, writing new tests, or improving test quality and coverage.

  • Structure unit tests with Arrange-Act-Assert pattern and xUnit
  • Write parameterized tests using Theory and MemberData for multiple scenarios
  • Mock dependencies with NSubstitute and verify method calls
  • Build ASP.NET Core integration tests with WebApplicationFactory
  • Test real infrastructure (databases, services) using Testcontainers
  • Organize tests into unit, integration, and helper projects with reusable builders

How to install csharp-testing

npx skills add null --skill csharp-testing
Claude Code
Cursor
Windsurf
Cline

How to use csharp-testing

  1. 1.Choose xUnit as your test framework and add FluentAssertions for readable assertions
  2. 2.Structure each test with Arrange-Act-Assert: set up state, execute code, verify outcomes
  3. 3.Use NSubstitute to mock dependencies and verify interactions in unit tests
  4. 4.For integration tests, use WebApplicationFactory to replace real services with test doubles
  5. 5.For database tests, use Testcontainers to spin up real PostgreSQL or other services
  6. 6.Organize tests into separate projects (UnitTests, IntegrationTests, TestHelpers) by concern
  7. 7.Create reusable test data builders to reduce setup boilerplate across tests

Use cases

Good for
  • Writing unit tests for business logic with mocked dependencies
  • Testing ASP.NET Core API endpoints with in-memory databases
  • Validating repository behavior against real PostgreSQL via Testcontainers
  • Creating parameterized tests for email validation, order processing, and similar multi-case scenarios
  • Building test data with fluent builders to reduce test setup boilerplate
Who it's for
  • C# and .NET developers writing new tests
  • Teams establishing testing standards and infrastructure
  • QA engineers reviewing test coverage and quality
  • Backend developers testing APIs and data access layers

csharp-testing FAQ

When should I use Theory vs Fact?

Use Fact for single-case tests. Use Theory with InlineData or MemberData for parameterized tests that verify behavior across multiple input scenarios.

How do I test async methods?

Use async Task test methods and await the code under test. Use Task.Delay with timeouts instead of Thread.Sleep, and leverage FluentAssertions for async assertions.

Should I mock or use real dependencies in integration tests?

Unit tests mock external dependencies. Integration tests use real infrastructure (databases via Testcontainers, in-memory databases for APIs) to verify end-to-end behavior.

How do I avoid test flakiness?

Avoid Thread.Sleep and timing assumptions. Use Testcontainers for deterministic infrastructure. Keep tests isolated with fresh instances per test. Avoid shared mutable state.

What is WebApplicationFactory used for?

WebApplicationFactory creates a test HTTP client for ASP.NET Core apps, allowing you to replace services (like DbContext) with test doubles and test API endpoints in isolation.

Full instructions (SKILL.md)

Source of truth, from affaan-m/ecc.


name: csharp-testing description: C# and .NET testing patterns with xUnit, FluentAssertions, mocking, integration tests, and test organization best practices. metadata: origin: ECC

C# Testing Patterns

Comprehensive testing patterns for .NET applications using xUnit, FluentAssertions, and modern testing practices.

When to Activate

  • Writing new tests for C# code
  • Reviewing test quality and coverage
  • Setting up test infrastructure for .NET projects
  • Debugging flaky or slow tests

Test Framework Stack

ToolPurpose
xUnitTest framework (preferred for .NET)
FluentAssertionsReadable assertion syntax
NSubstitute or MoqMocking dependencies
TestcontainersReal infrastructure in integration tests
WebApplicationFactoryASP.NET Core integration tests
BogusRealistic test data generation

Unit Test Structure

Arrange-Act-Assert

public sealed class OrderServiceTests
{
    private readonly IOrderRepository _repository = Substitute.For<IOrderRepository>();
    private readonly ILogger<OrderService> _logger = Substitute.For<ILogger<OrderService>>();
    private readonly OrderService _sut;

    public OrderServiceTests()
    {
        _sut = new OrderService(_repository, _logger);
    }

    [Fact]
    public async Task PlaceOrderAsync_ReturnsSuccess_WhenRequestIsValid()
    {
        // Arrange
        var request = new CreateOrderRequest
        {
            CustomerId = "cust-123",
            Items = [new OrderItem("SKU-001", 2, 29.99m)]
        };

        // Act
        var result = await _sut.PlaceOrderAsync(request, CancellationToken.None);

        // Assert
        result.IsSuccess.Should().BeTrue();
        result.Value.Should().NotBeNull();
        result.Value!.CustomerId.Should().Be("cust-123");
    }

    [Fact]
    public async Task PlaceOrderAsync_ReturnsFailure_WhenNoItems()
    {
        // Arrange
        var request = new CreateOrderRequest
        {
            CustomerId = "cust-123",
            Items = []
        };

        // Act
        var result = await _sut.PlaceOrderAsync(request, CancellationToken.None);

        // Assert
        result.IsSuccess.Should().BeFalse();
        result.Error.Should().Contain("at least one item");
    }
}

Parameterized Tests with Theory

[Theory]
[InlineData("", false)]
[InlineData("a", false)]
[InlineData("ab@c.d", false)]
[InlineData("user@example.com", true)]
[InlineData("user+tag@example.co.uk", true)]
public void IsValidEmail_ReturnsExpected(string email, bool expected)
{
    EmailValidator.IsValid(email).Should().Be(expected);
}

[Theory]
[MemberData(nameof(InvalidOrderCases))]
public async Task PlaceOrderAsync_RejectsInvalidOrders(CreateOrderRequest request, string expectedError)
{
    var result = await _sut.PlaceOrderAsync(request, CancellationToken.None);

    result.IsSuccess.Should().BeFalse();
    result.Error.Should().Contain(expectedError);
}

public static TheoryData<CreateOrderRequest, string> InvalidOrderCases => new()
{
    { new() { CustomerId = "", Items = [ValidItem()] }, "CustomerId" },
    { new() { CustomerId = "c1", Items = [] }, "at least one item" },
    { new() { CustomerId = "c1", Items = [new("", 1, 10m)] }, "SKU" },
};

Mocking with NSubstitute

[Fact]
public async Task GetOrderAsync_ReturnsNull_WhenNotFound()
{
    // Arrange
    var orderId = Guid.NewGuid();
    _repository.FindByIdAsync(orderId, Arg.Any<CancellationToken>())
        .Returns((Order?)null);

    // Act
    var result = await _sut.GetOrderAsync(orderId, CancellationToken.None);

    // Assert
    result.Should().BeNull();
}

[Fact]
public async Task PlaceOrderAsync_PersistsOrder()
{
    // Arrange
    var request = ValidOrderRequest();

    // Act
    await _sut.PlaceOrderAsync(request, CancellationToken.None);

    // Assert — verify the repository was called
    await _repository.Received(1).AddAsync(
        Arg.Is<Order>(o => o.CustomerId == request.CustomerId),
        Arg.Any<CancellationToken>());
}

ASP.NET Core Integration Tests

WebApplicationFactory Setup

public sealed class OrderApiTests : IClassFixture<WebApplicationFactory<Program>>
{
    private readonly HttpClient _client;

    public OrderApiTests(WebApplicationFactory<Program> factory)
    {
        _client = factory.WithWebHostBuilder(builder =>
        {
            builder.ConfigureServices(services =>
            {
                // Replace real DB with in-memory for tests
                services.RemoveAll<DbContextOptions<AppDbContext>>();
                services.AddDbContext<AppDbContext>(options =>
                    options.UseInMemoryDatabase("TestDb"));
            });
        }).CreateClient();
    }

    [Fact]
    public async Task GetOrder_Returns404_WhenNotFound()
    {
        var response = await _client.GetAsync($"/api/orders/{Guid.NewGuid()}");

        response.StatusCode.Should().Be(HttpStatusCode.NotFound);
    }

    [Fact]
    public async Task CreateOrder_Returns201_WithValidRequest()
    {
        var request = new CreateOrderRequest
        {
            CustomerId = "cust-1",
            Items = [new("SKU-001", 1, 19.99m)]
        };

        var response = await _client.PostAsJsonAsync("/api/orders", request);

        response.StatusCode.Should().Be(HttpStatusCode.Created);
        response.Headers.Location.Should().NotBeNull();
    }
}

Testing with Testcontainers

public sealed class PostgresOrderRepositoryTests : IAsyncLifetime
{
    private readonly PostgreSqlContainer _postgres = new PostgreSqlBuilder()
        .WithImage("postgres:16-alpine")
        .Build();

    private AppDbContext _db = null!;

    public async Task InitializeAsync()
    {
        await _postgres.StartAsync();
        var options = new DbContextOptionsBuilder<AppDbContext>()
            .UseNpgsql(_postgres.GetConnectionString())
            .Options;
        _db = new AppDbContext(options);
        await _db.Database.MigrateAsync();
    }

    public async Task DisposeAsync()
    {
        await _db.DisposeAsync();
        await _postgres.DisposeAsync();
    }

    [Fact]
    public async Task AddAsync_PersistsOrder()
    {
        var repo = new SqlOrderRepository(_db);
        var order = Order.Create("cust-1", [new OrderItem("SKU-001", 2, 10m)]);

        await repo.AddAsync(order, CancellationToken.None);

        var found = await repo.FindByIdAsync(order.Id, CancellationToken.None);
        found.Should().NotBeNull();
        found!.Items.Should().HaveCount(1);
    }
}

Test Organization

tests/
  MyApp.UnitTests/
    Services/
      OrderServiceTests.cs
      PaymentServiceTests.cs
    Validators/
      EmailValidatorTests.cs
  MyApp.IntegrationTests/
    Api/
      OrderApiTests.cs
    Repositories/
      OrderRepositoryTests.cs
  MyApp.TestHelpers/
    Builders/
      OrderBuilder.cs
    Fixtures/
      DatabaseFixture.cs

Test Data Builders

public sealed class OrderBuilder
{
    private string _customerId = "cust-default";
    private readonly List<OrderItem> _items = [new("SKU-001", 1, 10m)];

    public OrderBuilder WithCustomer(string customerId)
    {
        _customerId = customerId;
        return this;
    }

    public OrderBuilder WithItem(string sku, int quantity, decimal price)
    {
        _items.Add(new OrderItem(sku, quantity, price));
        return this;
    }

    public Order Build() => Order.Create(_customerId, _items);
}

// Usage in tests
var order = new OrderBuilder()
    .WithCustomer("cust-vip")
    .WithItem("SKU-PREMIUM", 3, 99.99m)
    .Build();

Common Anti-Patterns

Anti-PatternFix
Testing implementation detailsTest behavior and outcomes
Shared mutable test stateFresh instance per test (xUnit does this via constructors)
Thread.Sleep in async testsUse Task.Delay with timeout, or polling helpers
Asserting on ToString() outputAssert on typed properties
One giant assertion per testOne logical assertion per test
Test names describing implementationName by behavior: Method_ExpectedResult_WhenCondition
Ignoring CancellationTokenAlways pass and verify cancellation

Running Tests

# Run all tests
dotnet test

# Run with coverage
dotnet test --collect:"XPlat Code Coverage"

# Run specific project
dotnet test tests/MyApp.UnitTests/

# Filter by test name
dotnet test --filter "FullyQualifiedName~OrderService"

# Watch mode during development
dotnet watch test --project tests/MyApp.UnitTests/