docs: Update Run your tests to the latest version of TC for .NET (#16855)

This commit is contained in:
Andre Hofmeister 2023-03-13 10:42:51 +01:00 committed by GitHub
parent 5304b7fa04
commit 83a7a28178
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 10 deletions

View File

@ -23,10 +23,10 @@ $ cd /path/to/dotnet-docker
$ dotnet new xunit -n myWebApp.Tests -o tests $ dotnet new xunit -n myWebApp.Tests -o tests
``` ```
Next, we'll update the test project and add the Testcontainers for .NET package that allows us to run tests against Docker resources. Switch to the `tests` directory and run the following command: Next, we'll update the test project and add the Testcontainers for .NET package that allows us to run tests against Docker resources (PostgreSQL container). Switch to the `tests` directory and run the following command:
```console ```console
$ dotnet add package Testcontainers --version 2.3.0 $ dotnet add package Testcontainers.PostgreSql --version 3.0.0
``` ```
## Add a test ## Add a test
@ -34,10 +34,16 @@ $ dotnet add package Testcontainers --version 2.3.0
Open the test project in your favorite IDE and replace the contents of `UnitTest1` with the following code: Open the test project in your favorite IDE and replace the contents of `UnitTest1` with the following code:
```c# ```c#
using System;
using System.Net; using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using DotNet.Testcontainers.Builders; using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Containers; using DotNet.Testcontainers.Containers;
using DotNet.Testcontainers.Networks; using DotNet.Testcontainers.Networks;
using Testcontainers.PostgreSql;
using Xunit;
public sealed class UnitTest1 : IAsyncLifetime, IDisposable public sealed class UnitTest1 : IAsyncLifetime, IDisposable
{ {
@ -45,30 +51,29 @@ public sealed class UnitTest1 : IAsyncLifetime, IDisposable
private readonly CancellationTokenSource _cts = new(TimeSpan.FromMinutes(1)); private readonly CancellationTokenSource _cts = new(TimeSpan.FromMinutes(1));
private readonly IDockerNetwork _network; private readonly INetwork _network;
private readonly IDockerContainer _dbContainer; private readonly IContainer _dbContainer;
private readonly IDockerContainer _appContainer; private readonly IContainer _appContainer;
public UnitTest1() public UnitTest1()
{ {
_network = new TestcontainersNetworkBuilder() _network = new NetworkBuilder()
.WithName(Guid.NewGuid().ToString("D"))
.Build(); .Build();
_dbContainer = new TestcontainersBuilder<TestcontainersContainer>() _dbContainer = new PostgreSqlBuilder()
.WithImage("postgres") .WithImage("postgres")
.WithNetwork(_network) .WithNetwork(_network)
.WithNetworkAliases("db") .WithNetworkAliases("db")
.WithVolumeMount("postgres-data", "/var/lib/postgresql/data") .WithVolumeMount("postgres-data", "/var/lib/postgresql/data")
.Build(); .Build();
_appContainer = new TestcontainersBuilder<TestcontainersContainer>() _appContainer = new ContainerBuilder()
.WithImage("dotnet-docker") .WithImage("dotnet-docker")
.WithNetwork(_network) .WithNetwork(_network)
.WithPortBinding(HttpPort, true) .WithPortBinding(HttpPort, true)
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(HttpPort)) .WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request => request.ForPath("/")))
.Build(); .Build();
} }