Skip to content

pandarun/testcontainers-dotnet

 
 

Repository files navigation

TestContainers dotnet

codecov Sonarcloud Status Donation

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.

Linux: Build Status

Windows: Build status


Feature parity

Linux environment

  • Container management
  • Todo: Start Container from Dockerfile
  • Ryuk resource reaper

Windows environment

  • Container management
  • Todo: Start Container from Dockerfile
  • Todo: Windows version of Ryuk [Help wanted]

Built-in containers

Container Readme Version
Generic Container -- Generic
MsSql Container README Generic
PostgreSql Container README Generic
ArangoDb Container README Generic

Example code

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();

About

dotnet port of testcontainers-java

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 94.6%
  • Shell 2.2%
  • Go 2.2%
  • Other 1.0%