Part 5 · 1 chapters · ~8 min

Tooling and Testing

The dotnet CLI, solutions and projects, NuGet and central package management, xUnit, assertions and theories, NSubstitute or Moq, WebApplicationFactory for integration tests, Testcontainers, why the EF Core in-memory provider misleads, coverage with coverlet, dotnet format, and CI.

6

Integration tests through the real pipeline

code
public class TransferTests(LedgerFactory factory) : IClassFixture<LedgerFactory>
{
    [Fact]
    public async Task Hundred_identical_requests_create_one_transfer()
    {
        var client = factory.CreateClient();
        var key = Guid.NewGuid().ToString();
        var responses = await Task.WhenAll(Enumerable.Range(0, 100).Select(_ => {
            var req = new HttpRequestMessage(HttpMethod.Post, "/v1/transfers") { Content = JsonContent.Create(body) };
            req.Headers.Add("Idempotency-Key", key);
            return client.SendAsync(req);
        }));
        var ids = await Task.WhenAll(responses.Select(r => r.Content.ReadFromJsonAsync<TransferDto>()));
        Assert.Single(ids.Select(i => i!.Id).Distinct());
    }
}
public class LedgerFactory : WebApplicationFactory<Program>, IAsyncLifetime
{
    readonly PostgreSqlContainer _pg = new PostgreSqlBuilder().WithImage("postgres:16").Build();   // Testcontainers
    …
}
TOOLING AND TESTING
the dotnet toolbox
dotnet CLInew, build, run, test, publish,format, tool.NuGetPackages; central packagemanagement across projects.xUnit / NUnit / MSTestTest frameworks; xUnit is commonin new projects.WebApplicationFactoryIn-memory ASP.NET Core server forintegration tests.TestcontainersReal Postgres or SQL Server intests.BenchmarkDotNetStatistically soundmicro-benchmarks (part 9).
swipe the figure sideways, or tap expand for full screen
1/4
one CLI
The dotnet CLI covers the whole lifecycle; IDEs (Visual Studio, Rider, VS Code with C# Dev Kit) build on it.
dotnet CLIsame in CI