Csharp Testing

affaan-m/ECC/pi/core/skills/csharp-testing

作者 affaan-mef648e01899ba3e8dc6371642deaaf64b4477775無授權條款275K 個星標收錄於 2026年10月9日更新於 2026年10月9日儲存庫4 天前更新

C# and .NET testing patterns with xUnit, FluentAssertions, mocking, integration tests, and test organization best practices. Use when writing or reviewing xUnit tests, mocks, or integration tests in a C# / .NET project.

AI 產生的概覽

指導使用 xUnit、FluentAssertions、模擬與整合測試來撰寫及審查 C#/.NET 測試。

功能
提供 .NET 測試的參考模式:Arrange-Act-Assert 單元測試、xUnit Theory、NSubstitute 模擬、以 WebApplicationFactory 進行的 ASP.NET Core 整合測試,以及以 Testcontainers 為基礎的資料庫測試。內容也涵蓋測試專案組織、測試資料建構器、常見反模式以及 dotnet test 指令。此技能僅含說明,不會產生檔案或指令碼。
適用情境
適用於為 C# 程式碼撰寫新測試、審查測試品質與涵蓋率、建置 .NET 測試基礎設施,或排查不穩定或執行緩慢的測試。
執行需求
未附指令碼。代理不需額外工具即可套用這些指引;依範例操作需要 .NET SDK,以及 xUnit、FluentAssertions、NSubstitute 或 Moq 等模擬程式庫,並可選用 Testcontainers、WebApplicationFactory 與 Bogus。

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

csharp
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

csharp
[Theory][InlineData("", false)][InlineData("a", false)][InlineData("[email protected]", false)][InlineData("[email protected]", true)][InlineData("[email protected]", 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

csharp
[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

csharp
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

csharp
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

csharp
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 testsvar 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

bash
# Run all testsdotnet test
# Run with coveragedotnet test --collect:"XPlat Code Coverage"
# Run specific projectdotnet test tests/MyApp.UnitTests/
# Filter by test namedotnet test --filter "FullyQualifiedName~OrderService"
# Watch mode during developmentdotnet watch test --project tests/MyApp.UnitTests/

來源與署名

來源:affaan-m/ECC位於pi/core/skills/csharp-testing提交ef648e0

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架