forked from testcontainers/testcontainers-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Testing with xUnit documentation
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Testing with xUnit | ||
|
||
The [Testcontainers.Xunit](https://www.nuget.org/packages/Testcontainers.Xunit) package provides classes that simplify writing tests using Docker containers with [xUnit](https://xunit.net). Starting and disposing containers is handled automatically. Setting up logging is straightforward. | ||
|
||
## Use a container per test method | ||
|
||
If you need a fresh contaner per test method, inherit `ContainerTest<TBuilderEntity, TContainerEntity>` and use its `Container` property. | ||
|
||
```csharp | ||
using System.Threading.Tasks; | ||
using StackExchange.Redis; | ||
using Testcontainers.Redis; | ||
using Testcontainers.Xunit; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace MyProject.Tests; | ||
|
||
public sealed class RedisContainerTest(ITestOutputHelper output) | ||
: ContainerTest<RedisBuilder, RedisContainer>(output) | ||
{ | ||
protected override RedisBuilder Configure(RedisBuilder builder) | ||
{ | ||
// 👇 Pin the Redis image to version 7.4.0 (alpine) | ||
return builder.WithImage("redis:7.4.0-alpine"); | ||
} | ||
|
||
[Fact] | ||
public async Task Test1() | ||
{ | ||
// 👆 A new fresh container is started before the test is run | ||
// 👇 get the connection string from the container | ||
var connectionString = Container.GetConnectionString(); | ||
using var redis = ConnectionMultiplexer.Connect(connectionString); | ||
await redis.GetDatabase().StringSetAsync("key", "value"); | ||
|
||
// 👇 The container is disposed after the test has finished running | ||
} | ||
|
||
[Fact] | ||
public async Task Test2() | ||
{ | ||
// 👆 Another fresh container is started before the second test is run | ||
var connectionString = Container.GetConnectionString(); | ||
using var redis = ConnectionMultiplexer.Connect(connectionString); | ||
string value = await redis.GetDatabase().StringGetAsync("key"); | ||
Assert.Null(value); // 👈 value is null since the container is not shared | ||
// 👇 The container is disposed after the test has finished running | ||
} | ||
} | ||
``` | ||
|
||
Injecting a `ITestOutputHelper` allows Testcontainers logs to be printed into the test runner output. | ||
|
||
## Use a container per test class | ||
|
||
Using a container per test method might be too heavy, especially if the container is slow to start. You might want to share a single container across all tests of a single class by using the `ContainerFixture<TBuilderEntity, TContainerEntity>` class that can be injected into your test classes. | ||
|
||
```csharp | ||
// TODO: sample code | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters