Skip to content

Commit 5b3d494

Browse files
authored
chore: Replace codeinclude with snippets (#1251)
1 parent 2da2080 commit 5b3d494

32 files changed

+175
-127
lines changed

docs/api/create_docker_container.md

+21
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,27 @@ using var timeoutCts = new CancellationTokenSource(TimeSpan.FromMinutes(1));
7676
await _container.StartAsync(timeoutCts.Token);
7777
```
7878

79+
## Getting log messages
80+
81+
Testcontainers for .NET provides two approaches for retrieving log messages from containers: `GetLogsAsync` and `WithOutputConsumer`. Each method serves different use cases for handling container logs.
82+
83+
The `GetLogsAsync` method is available through the `IContainer` interface. It allows you to fetch logs from a container for a specific time range or from the beginning until the present. This approach is useful for retrieving logs after a test has run, especially when troubleshooting issues or failures.
84+
85+
```csharp title="Getting all log messages"
86+
var (stdout, stderr) = await _container.GetLogsAsync();
87+
```
88+
89+
The `WithOutputConsumer` method is part of the `ContainerBuilder` class and is used to continuously forward container log messages to a specified output consumer. This approach provides real-time access to logs as the container runs.
90+
91+
```csharp title="Forwarding all log messages"
92+
using IOutputConsumer outputConsumer = Consume.RedirectStdoutAndStderrToConsole();
93+
94+
_ = new ContainerBuilder()
95+
.WithOutputConsumer(outputConsumer);
96+
```
97+
98+
The static class `Consume` offers pre-configured implementations of the `IOutputConsumer` interface for common use cases. If you need additional functionalities beyond those provided by the default implementations, you can create your own implementations of `IOutputConsumer`.
99+
79100
## Examples
80101

81102
An NGINX container that binds the HTTP port to a random host port and hosts static content. The example connects to the web server and checks the HTTP status code.

docs/custom_configuration/index.md

+15-9
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,29 @@ Testcontainers supports various configurations to set up your test environment.
1616
| `ryuk.container.privileged` | `TESTCONTAINERS_RYUK_CONTAINER_PRIVILEGED` | Runs Ryuk (resource reaper) in privileged mode. | `false` |
1717
| `ryuk.container.image` | `TESTCONTAINERS_RYUK_CONTAINER_IMAGE` | The Ryuk (resource reaper) Docker image. | `testcontainers/ryuk:0.5.1` |
1818
| `hub.image.name.prefix` | `TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX` | The name to use for substituting the Docker Hub registry part of the image name. | - |
19-
<!-- | `wait.strategy.retries` | `TESTCONTAINERS_WAIT_STRATEGY_RETRIES` | The wait strategy retry count. | `infinite` |
19+
| `wait.strategy.retries` | `TESTCONTAINERS_WAIT_STRATEGY_RETRIES` | The wait strategy retry count. | `infinite` |
2020
| `wait.strategy.interval` | `TESTCONTAINERS_WAIT_STRATEGY_INTERVAL` | The wait strategy interval<sup>1</sup>. | `00:00:01` |
2121
| `wait.strategy.timeout` | `TESTCONTAINERS_WAIT_STRATEGY_TIMEOUT` | The wait strategy timeout<sup>1</sup>. | `01:00:00` |
2222

23-
1) The value represent the string representation of a [TimeSpan](https://learn.microsoft.com/en-us/dotnet/api/system.timespan), for example, `00:00:01` for 1 second. -->
23+
1) The value represent the string representation of a [TimeSpan](https://learn.microsoft.com/en-us/dotnet/api/system.timespan), for example, `00:00:01` for 1 second.
2424

2525
## Configure remote container runtime
2626

2727
To configure a remote container runtime, Testcontainers provides support for Docker's environment variables in addition to the properties file. During initialization, Testcontainers' auto-discovery feature detect and apply custom configurations including container runtimes. If you are running Docker on a remote host, you can configure it using either of the following methods:
2828

29-
```console title="Properties File"
30-
docker.host=tcp://docker:2375
31-
```
29+
=== "Environment Variable"
30+
```
31+
DOCKER_HOST=tcp://docker:2375
32+
```
3233

33-
```console title="Environment Variable"
34-
DOCKER_HOST=tcp://docker:2375
35-
```
34+
=== "Properties File"
35+
```
36+
docker.host=tcp://docker:2375
37+
```
3638

3739
## Enable logging
3840

39-
In .NET logging usually goes through the test framework. Testcontainers is not aware of the project's test framework and may not forward log messages to the appropriate output stream. The default implementation forwards log messages to the `Console` (respectively `stdout` and `stderr`). The output should at least pop up in the IDE running tests in the `Debug` configuration. To override the default implementation, set the `TestcontainersSettings.Logger` property to an instance of an `ILogger` implementation before creating a Docker resource, such as a container.
41+
In .NET logging usually goes through the test framework. Testcontainers is not aware of the project's test framework and may not forward log messages to the appropriate output stream. The default implementation forwards log messages to the `Console` (respectively `stdout` and `stderr`). The output should at least pop up in the IDE running tests in the `Debug` configuration. To override the default implementation, use the builder's `WithLogger(ILogger)` method and provide an `ILogger` instance to replace the default console logger.
4042

4143
[testcontainers.org 00:00:00.34] Connected to Docker:
4244
Host: tcp://127.0.0.1:60706/
@@ -58,6 +60,10 @@ In .NET logging usually goes through the test framework. Testcontainers is not a
5860
[testcontainers.org 00:00:06.26] Start Docker container 027af397344d08d5fc174bf5b5d449f6b352a8a506306d3d96390aaa2bb0445d
5961
[testcontainers.org 00:00:06.64] Delete Docker container 027af397344d08d5fc174bf5b5d449f6b352a8a506306d3d96390aaa2bb0445d
6062

63+
!!!tip
64+
65+
These log messages are from the Testcontainers library and contain information about the test resources. They do not include log messages from the containers. To get the container log messages, see: [Getting log messages](https://dotnet.testcontainers.org/api/create_docker_container/#getting-log-messages).
66+
6167
To enable debug log messages in the default implementation, set the property `ConsoleLogger.Instance.DebugLogLevelEnabled` to `true`. This will forward messages related to building or pulling Docker images to the output stream.
6268

6369
[properties-file-format]: https://en.wikipedia.org/wiki/.properties

docs/examples/aspnet.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ The following example adds tests to an ASP.NET Core Blazor application. The test
7575

7676
To use Testcontainers' pre-configured Microsoft SQL Server module, add the [Testcontainers.MsSql](https://www.nuget.org/packages/Testcontainers.MsSql) NuGet dependency to your test project:
7777

78-
```console
78+
```shell
7979
dotnet add package Testcontainers.MsSql --version 3.0.0
8080
```
8181

docs/examples/dind.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
If you choose to run your tests in a Docker Wormhole configuration, which involves using sibling containers, it is necessary to mount Docker's raw socket `/var/run/docker.sock.raw`. You find more information and an explanation of the Docker bug in this [comment](https://github.com/docker/for-mac/issues/5588#issuecomment-934600089).
88

9-
```console
9+
```shell
1010
docker run -v /var/run/docker.sock.raw:/var/run/docker.sock $IMAGE dotnet test
1111
```
1212

@@ -29,7 +29,7 @@ services:
2929
entrypoint: dotnet
3030
command: test
3131
# Uncomment the lines below in the case of Docker Desktop (see note above).
32-
# TESTCONTAINERS_HOST_OVERRIDE is not needed in the case of Docker Engine.
32+
# TESTCONTAINERS_HOST_OVERRIDE is not needed in the case of Docker Engine.
3333
# environment:
3434
# - TESTCONTAINERS_HOST_OVERRIDE=host.docker.internal
3535
volumes:

docs/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Welcome to Testcontainers for .NET!
22

3-
```console title="Install the NuGet dependency"
3+
```shell title="Install the NuGet dependency"
44
dotnet add package Testcontainers
55
```
66

docs/modules/elasticsearch.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,27 @@
44

55
Add the following dependency to your project file:
66

7-
```console title="NuGet"
7+
```shell title="NuGet"
88
dotnet add package Testcontainers.Elasticsearch
99
```
1010

1111
You can start an Elasticsearch container instance from any .NET application. This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.
1212

13-
<!--codeinclude-->
14-
[Usage Example](../../tests/Testcontainers.Elasticsearch.Tests/ElasticsearchContainerTest.cs) inside_block:UseElasticsearchContainer
15-
<!--/codeinclude-->
13+
=== "Usage Example"
14+
```csharp
15+
--8<-- "tests/Testcontainers.Elasticsearch.Tests/ElasticsearchContainerTest.cs:UseElasticsearchContainer"
16+
```
1617

1718
The test example uses the following NuGet dependencies:
1819

19-
<!--codeinclude-->
20-
[Package References](../../tests/Testcontainers.Elasticsearch.Tests/Testcontainers.Elasticsearch.Tests.csproj) inside_block:PackageReferences
21-
<!--/codeinclude-->
20+
=== "Package References"
21+
```xml
22+
--8<-- "tests/Testcontainers.Elasticsearch.Tests/Testcontainers.Elasticsearch.Tests.csproj:PackageReferences"
23+
```
2224

2325
To execute the tests, use the command `dotnet test` from a terminal.
2426

25-
--8<-- "docs/modules/_call_out_test_projects.md"
27+
--8<-- "docs/modules/_call_out_test_projects.txt"
2628

2729
## A Note To Developers
2830

docs/modules/index.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Modules are great examples of Testcontainers' capabilities. To write tests again
44

55
Modules are standalone dependencies that can be installed from [NuGet.org](https://www.nuget.org/profiles/Testcontainers). To use a module in your test project, you need to add it as a dependency first:
66

7-
```console
7+
```shell
88
dotnet add package Testcontainers.ModuleName
99
```
1010

@@ -69,15 +69,15 @@ await moduleNameContainer.StartAsync();
6969

7070
The Testcontainers for .NET repository contains a .NET [template](https://github.com/testcontainers/testcontainers-dotnet/tree/develop/src/Templates) to scaffold advanced modules quickly. To create and add a new module to the Testcontainers solution file, checkout the repository and install the .NET template first:
7171

72-
```console
72+
```shell
7373
git clone --branch develop git@github.com:testcontainers/testcontainers-dotnet.git
7474
cd ./testcontainers-dotnet/
7575
dotnet new --install ./src/Templates
7676
```
7777

7878
The following CLI commands create and add a new PostgreSQL module to the solution file:
7979

80-
```console
80+
```shell
8181
dotnet new tcm --name PostgreSql --official-module true --output ./src
8282
dotnet sln add ./src/Testcontainers.PostgreSql/Testcontainers.PostgreSql.csproj
8383
```

docs/modules/mongodb.md

+18-14
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,40 @@
44

55
Add the following dependency to your project file:
66

7-
```console title="NuGet"
7+
```shell title="NuGet"
88
dotnet add package Testcontainers.MongoDb
99
```
1010

1111
You can start a MongoDB container instance from any .NET application. Here, we create different container instances and pass them to the base test class. This allows us to test different configurations.
1212

13-
<!--codeinclude-->
14-
[Create Container Instance](../../tests/Testcontainers.MongoDb.Tests/MongoDbContainerTest.cs) inside_block:CreateMongoDbContainer
15-
<!--/codeinclude-->
13+
=== "Create Container Instance"
14+
```csharp
15+
--8<-- "tests/Testcontainers.MongoDb.Tests/MongoDbContainerTest.cs:CreateMongoDbContainer"
16+
```
1617

1718
This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.
1819

19-
<!--codeinclude-->
20-
[Usage Example](../../tests/Testcontainers.MongoDb.Tests/MongoDbContainerTest.cs) inside_block:UseMongoDbContainer
21-
<!--/codeinclude-->
20+
=== "Usage Example"
21+
```csharp
22+
--8<-- "tests/Testcontainers.MongoDb.Tests/MongoDbContainerTest.cs:UseMongoDbContainer"
23+
```
2224

2325
The test example uses the following NuGet dependencies:
2426

25-
<!--codeinclude-->
26-
[Package References](../../tests/Testcontainers.MongoDb.Tests/Testcontainers.MongoDb.Tests.csproj) inside_block:PackageReferences
27-
<!--/codeinclude-->
27+
=== "Package References"
28+
```xml
29+
--8<-- "tests/Testcontainers.MongoDb.Tests/Testcontainers.MongoDb.Tests.csproj:PackageReferences"
30+
```
2831

2932
To execute the tests, use the command `dotnet test` from a terminal.
3033

31-
--8<-- "docs/modules/_call_out_test_projects.md"
34+
--8<-- "docs/modules/_call_out_test_projects.txt"
3235

3336
## MongoDb Replica Set
3437

3538
By default, MongoDB runs as a standalone instance. If your tests require a MongoDB replica set, use the following configuration which will initialize a single-node replica set:
3639

37-
<!--codeinclude-->
38-
[Replica Set Configuration](../../tests/Testcontainers.MongoDb.Tests/MongoDbContainerTest.cs) inside_block:ReplicaSetContainerConfiguration
39-
<!--/codeinclude-->
40+
=== "Replica Set Configuration"
41+
```csharp
42+
--8<-- "tests/Testcontainers.MongoDb.Tests/MongoDbContainerTest.cs:ReplicaSetContainerConfiguration"
43+
```

docs/modules/mssql.md

+14-11
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,34 @@
44

55
Add the following dependency to your project file:
66

7-
```console title="NuGet"
7+
```shell title="NuGet"
88
dotnet add package Testcontainers.MsSql
99
```
1010

1111
You can start a MSSQL container instance from any .NET application. Here, we create different container instances and pass them to the base test class. This allows us to test different configurations.
1212

13-
<!--codeinclude-->
14-
[Create Container Instance](../../tests/Testcontainers.MsSql.Tests/MsSqlContainerTest.cs) inside_block:CreateMsSqlContainer
15-
<!--/codeinclude-->
13+
=== "Create Container Instance"
14+
```csharp
15+
--8<-- "tests/Testcontainers.MsSql.Tests/MsSqlContainerTest.cs:CreateMsSqlContainer"
16+
```
1617

1718
This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.
1819

19-
<!--codeinclude-->
20-
[Usage Example](../../tests/Testcontainers.MsSql.Tests/MsSqlContainerTest.cs) inside_block:UseMsSqlContainer
21-
<!--/codeinclude-->
20+
=== "Usage Example"
21+
```csharp
22+
--8<-- "tests/Testcontainers.MsSql.Tests/MsSqlContainerTest.cs:UseMsSqlContainer"
23+
```
2224

2325
The test example uses the following NuGet dependencies:
2426

25-
<!--codeinclude-->
26-
[Package References](../../tests/Testcontainers.MsSql.Tests/Testcontainers.MsSql.Tests.csproj) inside_block:PackageReferences
27-
<!--/codeinclude-->
27+
=== "Package References"
28+
```xml
29+
--8<-- "tests/Testcontainers.MsSql.Tests/Testcontainers.MsSql.Tests.csproj:PackageReferences"
30+
```
2831

2932
To execute the tests, use the command `dotnet test` from a terminal.
3033

31-
--8<-- "docs/modules/_call_out_test_projects.md"
34+
--8<-- "docs/modules/_call_out_test_projects.txt"
3235

3336
## A Note To Developers
3437

docs/modules/neo4j.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@
44

55
Add the following dependency to your project file:
66

7-
```console title="NuGet"
7+
```shell title="NuGet"
88
dotnet add package Testcontainers.Neo4j
99
```
1010

1111
You can start an Neo4j container instance from any .NET application. This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.
1212

13-
<!--codeinclude-->
14-
[Usage Example](../../tests/Testcontainers.Neo4j.Tests/Neo4jContainerTest.cs) inside_block:UseNeo4jContainer
15-
<!--/codeinclude-->
13+
=== "Usage Example"
14+
```csharp
15+
--8<-- "tests/Testcontainers.Neo4j.Tests/Neo4jContainerTest.cs:UseNeo4jContainer"
16+
```
1617

1718
The test example uses the following NuGet dependencies:
1819

19-
<!--codeinclude-->
20-
[Package References](../../tests/Testcontainers.Neo4j.Tests/Testcontainers.Neo4j.Tests.csproj) inside_block:PackageReferences
21-
<!--/codeinclude-->
20+
=== "Package References"
21+
```xml
22+
--8<-- "tests/Testcontainers.Neo4j.Tests/Testcontainers.Neo4j.Tests.csproj:PackageReferences"
23+
```
2224

2325
To execute the tests, use the command `dotnet test` from a terminal.
2426

25-
--8<-- "docs/modules/_call_out_test_projects.md"
27+
--8<-- "docs/modules/_call_out_test_projects.txt"

docs/modules/postgres.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@
44

55
Add the following dependency to your project file:
66

7-
```console title="NuGet"
7+
```shell title="NuGet"
88
dotnet add package Testcontainers.PostgreSql
99
```
1010

1111
You can start an PostgreSQL container instance from any .NET application. This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.
1212

13-
<!--codeinclude-->
14-
[Usage Example](../../tests/Testcontainers.PostgreSql.Tests/PostgreSqlContainerTest.cs) inside_block:UsePostgreSqlContainer
15-
<!--/codeinclude-->
13+
=== "Usage Example"
14+
```csharp
15+
--8<-- "tests/Testcontainers.PostgreSql.Tests/PostgreSqlContainerTest.cs:UsePostgreSqlContainer"
16+
```
1617

1718
The test example uses the following NuGet dependencies:
1819

19-
<!--codeinclude-->
20-
[Package References](../../tests/Testcontainers.PostgreSql.Tests/Testcontainers.PostgreSql.Tests.csproj) inside_block:PackageReferences
21-
<!--/codeinclude-->
20+
=== "Package References"
21+
```xml
22+
--8<-- "tests/Testcontainers.PostgreSql.Tests/Testcontainers.PostgreSql.Tests.csproj:PackageReferences"
23+
```
2224

2325
To execute the tests, use the command `dotnet test` from a terminal.
2426

25-
--8<-- "docs/modules/_call_out_test_projects.md"
27+
--8<-- "docs/modules/_call_out_test_projects.txt"

docs/modules/pulsar.md

+14-11
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,34 @@
44

55
Add the following dependency to your project file:
66

7-
```console title="NuGet"
7+
```shell title="NuGet"
88
dotnet add package Testcontainers.Pulsar
99
```
1010

1111
You can start a Apache Pulsar container instance from any .NET application. Here, we create different container instances and pass them to the base test class. This allows us to test different configurations.
1212

13-
<!--codeinclude-->
14-
[Create Container Instance](../../tests/Testcontainers.Pulsar.Tests/PulsarContainerTest.cs) inside_block:CreatePulsarContainer
15-
<!--/codeinclude-->
13+
=== "Create Container Instance"
14+
```csharp
15+
--8<-- "tests/Testcontainers.Pulsar.Tests/PulsarContainerTest.cs:CreatePulsarContainer"
16+
```
1617

1718
This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method.
1819

19-
<!--codeinclude-->
20-
[Usage Example](../../tests/Testcontainers.Pulsar.Tests/PulsarContainerTest.cs) inside_block:UsePulsarContainer
21-
<!--/codeinclude-->
20+
=== "Usage Example"
21+
```csharp
22+
--8<-- "tests/Testcontainers.Pulsar.Tests/PulsarContainerTest.cs:UsePulsarContainer"
23+
```
2224

2325
The test example uses the following NuGet dependencies:
2426

25-
<!--codeinclude-->
26-
[Package References](../../tests/Testcontainers.Pulsar.Tests/Testcontainers.Pulsar.Tests.csproj) inside_block:PackageReferences
27-
<!--/codeinclude-->
27+
=== "Package References"
28+
```xml
29+
--8<-- "tests/Testcontainers.Pulsar.Tests/Testcontainers.Pulsar.Tests.csproj:PackageReferences"
30+
```
2831

2932
To execute the tests, use the command `dotnet test` from a terminal.
3033

31-
--8<-- "docs/modules/_call_out_test_projects.md"
34+
--8<-- "docs/modules/_call_out_test_projects.txt"
3235

3336
## Access Apache Pulsar
3437

0 commit comments

Comments
 (0)