Testing with xUnit.net
The Testcontainers.Xunit package simplifies writing tests with containers in xUnit.net. By leveraging xUnit.net's shared context, this package automates the setup and teardown of test resources, creating and disposing of containers as needed. This reduces repetitive code and avoids common patterns that developers would otherwise need to implement repeatedly.
To get started, add the following dependency to your project file:
NuGet | |
---|---|
1 |
|
Creating an isolated test context
To create a new test resource instance for each test, inherit from the ContainerTest<TBuilderEntity, TContainerEntity>
class. Each test resource instance is isolated and not shared across other tests, making this approach ideal for destructive operations that could interfere with other tests. You can access the generic TContainerEntity
container instance through the Container
property.
The example below demonstrates how to override the Configure(TBuilderEntity)
method and pin the image version. This method allows you to configure the container instance specifically for your test case, with all container builder methods available. If your tests rely on a Testcontainers' module, the module's default configurations will be applied.
1 2 3 4 5 6 7 8 9 |
|
Tip
Always pin the image version to avoid flakiness. This ensures consistency and prevents unexpected behavior, as the latest
tag may pointing to a new version.
The base class also receives an instance of xUnit.net's ITestOutputHelper to capture and forward log messages to the running test.
Considering that xUnit.net runs tests in a deterministic natural sort order (like Test1
, Test2
, etc.), retrieving the Redis (string) value in the second test will always return null
since a new test resource instance (Redis container) is created for each test.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
If you check the output of docker ps
, you will notice that three container instances in total are run, with two of them being Redis instances.
List running containers | |
---|---|
1 2 3 4 5 |
|
Creating a shared test context
Sometimes, creating and disposing of a test resource can be an expensive operation that you do not want to repeat for every test. By inheriting from the ContainerFixture<TBuilderEntity, TContainerEntity>
class, you can share the test resource instance across all tests within the same test class.
xUnit.net's fixture implementation does not rely on the ITestOutputHelper
interface to capture and forward log messages; instead, it expects an implementation of IMessageSink
. Make sure your fixture's default constructor accepts the interface implementation and forwards it to the base class.
1 2 3 4 5 6 7 8 9 |
|
This ensures that the fixture is created only once for the entire test class, which also improves overall test performance. You must implement the IClassFixture<TFixture>
interface with the previously created container fixture type in your test class and add the type as argument to the default constructor.
1 2 |
|
In this case, retrieving the Redis (string) value in the second test will no longer return null
. Instead, it will return the value added in the first test.
1 2 3 4 5 6 7 |
|
The output of docker ps
shows that, instead of two Redis containers, only one runs.
List running containers | |
---|---|
1 2 3 4 |
|
Testing ADO.NET services
In addition to the two mentioned base classes, the package contains two more classes: DbContainerTest
and DbContainerFixture
, which behave identically but offer additional convenient features when working with services accessible through an ADO.NET provider.
Inherit from either the DbContainerTest
or DbContainerFixture
class and override the Configure(TBuilderEntity)
method to configure your database service.
In this example, we use the default configuration of the PostgreSQL module. The container image capabilities are used to instantiate the database, schema, and test data. During startup, the PostgreSQL container runs SQL scripts placed under the /docker-entrypoint-initdb.d/
directory automatically.
1 2 3 4 5 6 7 8 9 10 |
|
Inheriting from the database container test or fixture class requires you to implement the abstract DbProviderFactory
property and resolve a compatible DbProviderFactory
according to your ADO.NET service.
1 |
|
Note
Depending on how you initialize and access the database, it may be necessary to override the ConnectionString
property and replace the default database name with the one actual in use.
After configuring the dependent ADO.NET service, you can add the necessary tests. In this case, we run an SQL SELECT
statement to retrieve the first record from the album
table.
1 2 3 4 5 6 7 8 |
|
Tip
For the complete source code of this example and additional information, please refer to our test projects.