ArwynFr.IntegrationTesting 0.2.18

dotnet add package ArwynFr.IntegrationTesting --version 0.2.18
NuGet\Install-Package ArwynFr.IntegrationTesting -Version 0.2.18
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ArwynFr.IntegrationTesting" Version="0.2.18" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ArwynFr.IntegrationTesting --version 0.2.18
#r "nuget: ArwynFr.IntegrationTesting, 0.2.18"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ArwynFr.IntegrationTesting as a Cake Addin
#addin nuget:?package=ArwynFr.IntegrationTesting&version=0.2.18

// Install ArwynFr.IntegrationTesting as a Cake Tool
#tool nuget:?package=ArwynFr.IntegrationTesting&version=0.2.18

ArwynFr.IntegrationTesting

This library provides utility classes for writing integration tests in dotnet using XUnit and WebApplicationFactory.

Nuget.org Nuget.org GitHub
License

Installation

dotnet new classlib -n MyTestProject
dotnet add package ArwynFr.IntegrationTesting
dotnet add package Microsoft.NET.Test.Sdk
dotnet add package xunit.runner.visualstudio

Usage

Read advanced usage documentation for further details.

By default, the lib redirects the tested application logs to XUnit output, so you get them in the test output in case of failure. It also overwrites the application configuration with values from user secrets and environement variables.

public class MyTest : IntegrationTestBase<Program>
{
    public MyTest(ITestOutputHelper output) : base(output) { }

    [Fact]
    public async Task OnTest()
    {
        // Call system under test
        var response = await Client.GetFromJsonAsync<OrderDto>($"/order");

        response.Should().HaveValue();
    }

    // Override a service with fake implementation in the tested app
    protected override void ConfigureAppServices(IServiceCollection services)
        => services.AddSingleton<IMyService, FakeService>();
}

EntityFrameworkCore integration

public class TestBaseDb : IntegrationTestBase<Program, MyDbContext>
{
    public TestBaseDb(ITestOutputHelper output) : base(output) { }

    [Fact]
    public async Task OnTest()
    {
        // Access the injected dbcontext
        var value = await Database.Values
            .Where(val => val.Id == 1)
            .Select(val => val.Result)
            .FirstOrDefaultAsync();

        // Call system under test
        var result = await Client.GetFromJsonAsync<int>("/api/value/1");

        result.Should().Be(value + 1);
    }

    // Create and drop a database for every test execution
    protected override IDatabaseTestStrategy<Context> DatabaseTestStrategy
        => IDatabaseTestStrategy<MyDbContext>.DatabasePerTest;

    // Configure EFcore with a random database name
    protected override void ConfigureDbContext(DbContextOptionsBuilder builder)
        => builder.UseSqlite($"Data Source={Guid.NewGuid()}.sqlite");
}

Fluent specification-based testing

// Actual code redacted for brievty
// Write a test driver that implements specifications:
private class MySpecDriver(MyDbContext dbContext, HttpClient client) : TestDriverBase<SpecDriver>
{
    // Arranges
    public async Task ThereIsEntityWithName(string name) { }
    public async Task ThereIsNoEntityWithName(string name) { }

    // Acts
    public async Task ListAllEntities() { }
    public async Task FindEntityWithName(string name) { }
    public async Task CreateEntity(EntityDetails payload) { }

    // Asserts
    public async Task ResultShouldBe(string name) { }
    public async Task DetailsCountShouldBe(int number) { }
}

public class MySpecTest(ITestOutputHelper output) : TestBaseDb
{
    // Write fluent specifiation test:
    [Theory, InlineData("ArwynFr")]
    public async Task OnTest(string name)
    {
        await Services.GetRequiredService<MySpecDriver>()
            .Given(x => x.ThereIsEntityWithName(name))
            .When(x => x.FindEntityWithName(name))
            .Then(x => x.ResultShouldBe(name))
            .ExecuteAsync();
    }

    // Configure DI library
    protected override void ConfigureAppServices(IServiceCollection services)
    {
        base.ConfigureAppServices(services);
        services.AddSingleton(_ => new MySpecDriver(Database, Client));
    }
}

Contributing

This project welcomes contributions:

Request for support:
TBD

Disclose vulnerability:
Please create a new security advisory on GitHub
Read our security policy

Report malfunctions:
Please create a new issue on GitHub

Suggest a feature:
Please create a new issue on GitHub

Offer some code:
Please fork the repository and submit a pull-request
Read our definition of done in contributing guidelines

Moderate contributions:
This project is not currently appointing new moderators.
Read our governance policy

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.2.18 77 4/28/2024
0.2.17 78 4/24/2024
0.2.16 72 4/18/2024
0.2.15 69 4/17/2024
0.2.14 68 4/17/2024
0.2.12 73 4/12/2024
0.2.11 83 4/12/2024
0.2.10 73 4/10/2024
0.2.9 61 4/8/2024
0.2.8 70 4/8/2024
0.2.7 96 3/15/2024
0.2.6 86 3/15/2024
0.2.5 88 3/6/2024
0.2.4 84 2/24/2024
0.2.3 75 2/16/2024
0.2.2 84 2/14/2024
0.2.1 98 2/8/2024
0.2.0 77 2/2/2024
0.1.2 82 1/30/2024
0.1.1 73 1/30/2024
0.1.0 79 1/26/2024