WorldDomination.SimpleTestcontainers.xUnit.v3.Databases 1.0.0-pre02

This is a prerelease version of WorldDomination.SimpleTestcontainers.xUnit.v3.Databases.
dotnet add package WorldDomination.SimpleTestcontainers.xUnit.v3.Databases --version 1.0.0-pre02                
NuGet\Install-Package WorldDomination.SimpleTestcontainers.xUnit.v3.Databases -Version 1.0.0-pre02                
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="WorldDomination.SimpleTestcontainers.xUnit.v3.Databases" Version="1.0.0-pre02" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WorldDomination.SimpleTestcontainers.xUnit.v3.Databases --version 1.0.0-pre02                
#r "nuget: WorldDomination.SimpleTestcontainers.xUnit.v3.Databases, 1.0.0-pre02"                
#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 WorldDomination.SimpleTestcontainers.xUnit.v3.Databases as a Cake Addin
#addin nuget:?package=WorldDomination.SimpleTestcontainers.xUnit.v3.Databases&version=1.0.0-pre02&prerelease

// Install WorldDomination.SimpleTestcontainers.xUnit.v3.Databases as a Cake Tool
#tool nuget:?package=WorldDomination.SimpleTestcontainers.xUnit.v3.Databases&version=1.0.0-pre02&prerelease                

<h1 align="center">Simple: Microsoft SqlServer Testcontainer Helpers for xUnit v3</h1>

<div align="center"> ⚡ Making it simple to run your database tests in true database-isolation, quickly! ⚡ </div>

<br />

<div align="center">

<a href="https://choosealicense.com/licenses/mit/">
<img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square" alt="License - MIT" />
</a>

<a href="https://www.nuget.org/packages/WorldDomination.SimpleTestcontainers.MsSql/">
<img src="https://buildstats.info/nuget/WorldDomination.SimpleTestcontainers.MsSql" alt="NuGet" />
</a>

</div>


Overview

Testing code that depends on a database can be slow and brittle.

This library aims to make it simple and fast to test your code that depends on a database.

Testcontainers are a great way to test your code that depends on a database. It can also be slow with the common xUnit pattern of using a "Test Collection" which means each test in that 'collection' is sequential. It's also annoying to creating lots of xUnit ceremony to make multiple collections. Finally, it's frustrating that multiple tests hitting the same Database can cause the tests to be brittle.

This library aims to make it simple to test your code that depends on a database:

  • ✅ A single database per test class.
  • ✅ Tests are now isolated from each other.
  • ⚡ The entire test run can be faster because they now run in parallel (based off xUnit's Test Collections concept)

💻 TL;DR; Show me some code!

A bare minimum example.

public class SomeTests(
    SqlServerFixture SqlServerFixture, // The MSSql Testcontainer. Either this simple one or your own custom one.
    ITestOutputHelper TestOutputHelper // xUnit's output helper.
)
{
    private static readonly CancellationToken _cancellationToken = TestContext.Current.CancellationToken;

    [Fact]
    public async Task ToListAsync_GivenAnExistingDatabase_ShouldReturnAllUsers()
    {
        // Arrange.
        // Grab our unique connection string.
        var connectionString = SqlServerFixture.CreateDbConnectionString(TestContext.Current, TestOutputHelper);

        using (var db = new SqlConnection(connectionString))
        {
            // Act.
            var users = await db.QueryAsync<User>("SELECT * FROM Users", cancellationToken: _cancellationToken).ToListAsync(_cancellationToken);
        }

        // Assert.
        ....
    }

Using Entity Framework Core.

public class SomeTests(
    SqlServerFixture SqlServerFixture, // The MSSql Testcontainer. Either this simple one or your own custom one.
    ITestOutputHelper TestOutputHelper // xUnit's output helper.
)
{
    private static readonly CancellationToken _cancellationToken = TestContext.Current.CancellationToken;

    [Fact]
    public async Task ToListAsync_GivenAnExistingDatabase_ShouldReturnAllUsers()
    {
        // Arrange.
        // Grab our unique connection string.
        var connectionString = SqlServerFixture.CreateDbConnectionString(TestContext.Current, TestOutputHelper);

        // ** We're using Entity Framework to connect to our unique Database instance.
        // ** Can you anything you like here - like Dapper, etc.

        // Wire up our EF Core DbContext to this unique connection string.
        var efOptions = new DbContextOptionsBuilder<SqlServerDbContext>()
            .UseSqlServer(connectionString)
            .Options;

        // Create our context.
        var dbContext = new SqlServerDbContext(efOptions);

        // Create the dabase if it doesn't exist (which it shouldn't)
        await dbContext.Database.EnsureCreatedAsync(testContext.CancellationToken);

        // ** DB is ready to go.

        // Setup some fake data.
        var user = new User
        {
            FirstName = "Princess",
            LastName = "Leia",
            Email = "i.am.a.princess@example.com"
        };

        await dbContext.Users.AddAsync(user, _cancellationToken);
        await dbContext.SaveChangesAsync(_cancellationToken);

        // ** DB now has some data.

        // Act.
        var users = await dbContext.Users.ToListAsync(_cancellationToken);

        // Assert.
        Assert.NotNull(users);
    }
}

Contribute

Yep - contributions are always welcome. Please read the contribution guidelines first.

Code of Conduct

If you wish to participate in this repository then you need to abide by the code of conduct.

Feedback

Yes! Please use the Issues section to provide feedback - either good or needs improvement 🆒


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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on WorldDomination.SimpleTestcontainers.xUnit.v3.Databases:

Package Downloads
WorldDomination.SimpleTestcontainers.xUnit.v3.MsSql

A simplified way to create a Microsoft SqlServer Testcontainer with each test having it's own unique database in the same docker image instance. This means each test is having is running in true data isolation. It also means you can parallelize your tests easier, instead of having all the tests run in a (common) single collection/sequential run.

WorldDomination.SimpleTestcontainers.xUnit.v3.PostgreSQL

A simplified way to create a PostgreSQL Testcontainer with each test having it's own unique database in the same docker image instance. This means each test is having is running in true data isolation. It also means you can parallelize your tests easier, instead of having all the tests run in a (common) single collection/sequential run.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0-pre02 40 1/6/2025
1.0.0-pre01 56 1/5/2025