Testcontainers is a dotnet standard 2.0 library that supports NUnit and XUnit tests, providing lightweight, throwaway instances of common databases or anything else that can run in a Docker container.
Uses common Microsoft dependency injection patterns, app and host settings, and Microsoft Extensions Logging (MEL).
This is a port of testcontainers-java for dotnet.
- Container management
- Todo: Start Container from Dockerfile
- Ryuk resource reaper
- Container management
- Todo: Start Container from Dockerfile
- Todo: Windows version of Ryuk [Help wanted]
Container | Readme | Version |
---|---|---|
Generic Container | -- | |
MsSql Container | README | |
PostgreSql Container | README | |
ArangoDb Container | README |
For more examples, see integration tests
var container = new ContainerBuilder<GenericContainer>()
.ConfigureHostConfiguration(builder => builder.AddInMemoryCollection()) // host settings
.ConfigureAppConfiguration((context, builder) => builder.AddInMemoryCollection()) // app settings
.ConfigureDockerImageName(PlatformSpecific.TinyDockerImage)
.ConfigureLogging(builder => builder.AddConsole()) // Microsoft extensions logging
.ConfigureContainer((context, container) =>
{
// add labels
container.Labels.Add(CustomLabel.Key, CustomLabel.Value);
// add environment labels
container.Env[InjectedEnvVar.Key] = InjectedEnvVar.Value;
// add exposed ports (automatically mapped to higher port
container.ExposedPorts.Add(ExposedPort);
/*
to do something like `docker run -p 2345:34567 alpine:latest`,
both expose port and port binding must be set
*/
container.ExposedPorts.Add(PortBinding.Key);
container.PortBindings.Add(PortBinding.Key, PortBinding.Value);
// add bind mounts
container.BindMounts.Add(new Bind
{
HostPath = HostPathBinding.Key,
ContainerPath = HostPathBinding.Value,
AccessMode = AccessMode.ReadOnly
});
// set working directory
container.WorkingDirectory = WorkingDirectory;
// set command to run
container.Command = PlatformSpecific.ShellCommand(
$"{PlatformSpecific.Touch} {FileTouchedByCommand}; {PlatformSpecific.Shell}")
.ToList();
})
.Build();