Best Testcontainers-dotnet code snippet using DotNet.Testcontainers.Tests.Unit.Docker
ResourceReaperTest.cs
Source:ResourceReaperTest.cs
...9 public sealed class ResourceReaperTest : IAsyncLifetime10 {11 private ResourceReaper resourceReaper;12 [Fact]13 public async Task DockerContainerShouldBeDisposedByResourceReaper()14 {15 // Given16 var containerName = $"testcontainers-container-{Guid.NewGuid():D}";17 await new TestcontainersBuilder<TestcontainersContainer>()18 .WithImage("alpine")19 .WithName(containerName)20 .WithCommand(KeepTestcontainersUpAndRunning.Command)21 .WithResourceReaperSessionId(this.resourceReaper.SessionId)22 .Build()23 .StartAsync()24 .ConfigureAwait(false);25 // When26 await this.resourceReaper.DisposeAsync()27 .ConfigureAwait(false);28 // Then29 Assert.False(Docker.Exists(DockerResource.Container, containerName));30 }31 [Fact]32 public async Task DockerImageShouldBeDisposedByResourceReaper()33 {34 // Given35 var dockerImageFullName = await new ImageFromDockerfileBuilder()36 .WithDockerfileDirectory("Assets")37 .WithResourceReaperSessionId(this.resourceReaper.SessionId)38 .Build()39 .ConfigureAwait(false);40 // When41 await this.resourceReaper.DisposeAsync()42 .ConfigureAwait(false);43 // Then44 Assert.False(Docker.Exists(DockerResource.Image, dockerImageFullName));45 }46 [Fact]47 public async Task DockerNetworkShouldBeDisposedByResourceReaper()48 {49 // Given50 var networkName = $"testcontainers-network-{Guid.NewGuid():D}";51 await new TestcontainersNetworkBuilder()52 .WithName(networkName)53 .WithResourceReaperSessionId(this.resourceReaper.SessionId)54 .Build()55 .CreateAsync()56 .ConfigureAwait(false);57 // When58 await this.resourceReaper.DisposeAsync()59 .ConfigureAwait(false);60 // Then61 Assert.False(Docker.Exists(DockerResource.Network, networkName));62 }63 [Fact]64 public async Task DockerVolumeShouldBeDisposedByResourceReaper()65 {66 // Given67 var volumeName = $"testcontainers-volume-{Guid.NewGuid():D}";68 await new TestcontainersVolumeBuilder()69 .WithName(volumeName)70 .WithResourceReaperSessionId(this.resourceReaper.SessionId)71 .Build()72 .CreateAsync()73 .ConfigureAwait(false);74 // When75 await this.resourceReaper.DisposeAsync()76 .ConfigureAwait(false);77 // Then78 Assert.False(Docker.Exists(DockerResource.Volume, volumeName));79 }80 public async Task InitializeAsync()81 {82 this.resourceReaper = await ResourceReaper.GetAndStartNewAsync()83 .ConfigureAwait(false);84 }85 public Task DisposeAsync()86 {87 return this.resourceReaper.DisposeAsync().AsTask();88 }89 }90}...
DefaultResourceReaperTest.cs
Source:DefaultResourceReaperTest.cs
...25 await testcontainer.StartAsync()26 .ConfigureAwait(false);27 }28 // Then29 Assert.False(Docker.Exists(DockerResource.Container, DefaultRyukContainerName));30 }31 [Fact]32 public async Task CleanUpTrueDoesStartDefaultResourceReaper()33 {34 // Given35 var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()36 .WithImage("nginx")37 .WithCleanUp(true);38 // When39 await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build())40 {41 await testcontainer.StartAsync()42 .ConfigureAwait(false);43 }44 // Then45 Assert.True(Docker.Exists(DockerResource.Container, DefaultRyukContainerName));46 }47 [Fact]48 public async Task UsingResourceReaperSessionIdDoesNotStartDefaultResourceReaper()49 {50 // Given51 var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()52 .WithImage("nginx")53 .WithAutoRemove(true)54 .WithResourceReaperSessionId(Guid.NewGuid());55 // When56 await using (ITestcontainersContainer testcontainer = testcontainersBuilder.Build())57 {58 await testcontainer.StartAsync()59 .ConfigureAwait(false);60 }61 // Then62 Assert.False(Docker.Exists(DockerResource.Container, DefaultRyukContainerName));63 }64 public async Task InitializeAsync()65 {66 if (Docker.Exists(DockerResource.Container, DefaultRyukContainerName))67 {68 var resourceReaper = await ResourceReaper.GetAndStartDefaultAsync()69 .ConfigureAwait(false);70 await resourceReaper.DisposeAsync()71 .ConfigureAwait(false);72 }73 }74 public Task DisposeAsync()75 {76 return Task.CompletedTask;77 }78 }79}...
ProtectDockerDaemonSocketTest.cs
Source:ProtectDockerDaemonSocketTest.cs
...8 using DotNet.Testcontainers.Configurations;9 using DotNet.Testcontainers.Tests.Fixtures;10 using Microsoft.Extensions.Logging.Abstractions;11 using Xunit;12 public static class ProtectDockerDaemonSocketTest13 {14 private static IDockerEndpointAuthenticationConfiguration GetAuthConfig(ProtectDockerDaemonSocket protectDockerDaemonSocket)15 {16 var customConfiguration = new PropertiesFileConfiguration(protectDockerDaemonSocket.CustomProperties.ToArray());17 return new IDockerEndpointAuthenticationProvider[] { new MTlsEndpointAuthenticationProvider(customConfiguration), new TlsEndpointAuthenticationProvider(customConfiguration) }.First(authProvider => authProvider.IsApplicable()).GetAuthConfig();18 }19 public sealed class MTls : IClassFixture<DockerMTlsFixture>20 {21 private readonly IDockerEndpointAuthenticationConfiguration authConfig;22 public MTls(DockerMTlsFixture dockerMTlsFixture)23 {24 this.authConfig = GetAuthConfig(dockerMTlsFixture);25 }26 [Fact]27 public async Task GetVersionReturnsVersion()28 {29 // Given30 IDockerSystemOperations dockerSystemOperations = new DockerSystemOperations(Guid.Empty, this.authConfig, NullLogger.Instance);31 // When32 var version = await dockerSystemOperations.GetVersion()33 .ConfigureAwait(false);34 // Then35 Assert.Equal(ProtectDockerDaemonSocket.DockerVersion, version.Version);36 }37 }38 public sealed class Tls : IClassFixture<DockerTlsFixture>39 {40 private readonly IDockerEndpointAuthenticationConfiguration authConfig;41 public Tls(DockerTlsFixture dockerTlsFixture)42 {43 this.authConfig = GetAuthConfig(dockerTlsFixture);44 }45 [Fact]46 public async Task GetVersionReturnsVersion()47 {48 // Given49 IDockerSystemOperations dockerSystemOperations = new DockerSystemOperations(Guid.Empty, this.authConfig, NullLogger.Instance);50 // When51 var version = await dockerSystemOperations.GetVersion()52 .ConfigureAwait(false);53 // Then54 Assert.Equal(ProtectDockerDaemonSocket.DockerVersion, version.Version);55 }56 }57 }58}...
Docker
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using DotNet.Testcontainers.Containers.Builders;4using DotNet.Testcontainers.Containers.Modules;5using DotNet.Testcontainers.Containers.WaitStrategies;6using DotNet.Testcontainers.Tests.Fixtures;7using Xunit;8using Xunit.Abstractions;9{10 {11 private readonly WindowsDockerContainerFixture dockerFixture;12 private readonly ITestOutputHelper output;13 public DockerTest(WindowsDockerContainerFixture dockerFixture, ITestOutputHelper output)14 {15 this.dockerFixture = dockerFixture;16 this.output = output;17 }18 public async Task RunDockerCommand()19 {20 var docker = new Docker(this.dockerFixture.DockerUri);21 var result = await docker.RunAsync("ps");22 output.WriteLine(result);23 }24 }25}26using System;27using System.Threading.Tasks;28using DotNet.Testcontainers.Containers.Builders;29using DotNet.Testcontainers.Containers.Modules;30using DotNet.Testcontainers.Containers.WaitStrategies;31using DotNet.Testcontainers.Tests.Fixtures;32using Xunit;33using Xunit.Abstractions;34{35 {36 private readonly WindowsDockerContainerFixture dockerFixture;37 private readonly ITestOutputHelper output;38 public DockerTest(WindowsDockerContainerFixture dockerFixture, ITestOutputHelper output)39 {40 this.dockerFixture = dockerFixture;41 this.output = output;42 }43 public async Task RunDockerCommand()44 {45 var docker = new Docker(this.dockerFixture.DockerUri);46 var result = await docker.RunAsync("ps");47 output.WriteLine(result);48 }49 }50}51public async Task RunDockerCommand()52{53 var docker = new Docker(this.dockerFixture.DockerUri);54 var result = await docker.RunAsync("ps");55 output.WriteLine(result);56}
Docker
Using AI Code Generation
1var containers = await docker.Containers.ListContainersAsync(new ContainersListParameters());2foreach (var container in containers)3{4 Console.WriteLine($"Container ID: {container.ID}");5 Console.WriteLine($"Container Image: {container.Image}");6 Console.WriteLine($"Container State: {container.State}");7 Console.WriteLine($"Container Status: {container.Status}");8 Console.WriteLine($"Container Ports: {container.Ports}");9 Console.WriteLine($"Container Names: {container.Names}");10 Console.WriteLine($"Container Labels: {container.Labels}");11 Console.WriteLine($"Container SizeRw: {container.SizeRw}");12 Console.WriteLine($"Container SizeRootFs: {container.SizeRootFs}");13 Console.WriteLine($"Container HostConfig: {container.HostConfig}");14 Console.WriteLine($"Container NetworkSettings: {container.NetworkSettings}");15 Console.WriteLine($"Container Mounts: {container.Mounts}");16}17var client = config.CreateClient();18var containers = await client.Containers.ListContainersAsync(new ContainersListParameters());19foreach (var container in containers)20{21 Console.WriteLine($"Container ID: {container.ID}");22 Console.WriteLine($"Container Image: {container.Image}");23 Console.WriteLine($"Container State: {container.State}");24 Console.WriteLine($"Container Status: {container.Status}");25 Console.WriteLine($"Container Ports: {container.Ports}");26 Console.WriteLine($"Container Names: {container.Names}");27 Console.WriteLine($"Container Labels: {container.Labels}");28 Console.WriteLine($"Container SizeRw: {container.SizeRw}");29 Console.WriteLine($"Container SizeRootFs: {container.SizeRootFs}");30 Console.WriteLine($"Container HostConfig: {container.HostConfig}");31 Console.WriteLine($"Container NetworkSettings: {container.NetworkSettings}");32 Console.WriteLine($"Container Mounts: {container.Mounts}");33}34var client = config.CreateClient();35var containers = await client.Containers.ListContainersAsync(new ContainersListParameters());36foreach (var container in containers)37{38 Console.WriteLine($"Container ID: {container.ID}");39 Console.WriteLine($"Container Image:
Docker
Using AI Code Generation
1using DotNet.Testcontainers.Tests.Unit;2using System;3using System.Threading.Tasks;4{5 {6 static async Task Main(string[] args)7 {8 var docker = new Docker();9 await docker.StartAsync();10 Console.WriteLine("Hello World!");11 }12 }13}14NU1102: Unable to find package DotNet.Testcontainers.Tests.Unit with version (>= 0.16.0)15 - Found 1 version(s) in nuget.org [ Nearest version: 0.15.0 ]16 - Found 0 version(s) in Microsoft Visual Studio Offline Packages17NU1102: Unable to find package DotNet.Testcontainers with version (>= 0.16.0)18 - Found 1 version(s) in nuget.org [ Nearest version: 0.15.0 ]19 - Found 0 version(s) in Microsoft Visual Studio Offline Packages20NU1102: Unable to find package DotNet.Testcontainers.Tests.Unit with version (>= 0.16.0)21 - Found 1 version(s) in nuget.org [ Nearest version: 0.15.0 ]22 - Found 0 version(s) in Microsoft Visual Studio Offline Packages23NU1102: Unable to find package DotNet.Testcontainers.Tests.Unit with version (>= 0.16.0)24 - Found 1 version(s) in nuget.org
Docker
Using AI Code Generation
1using DotNet.Testcontainers.Tests.Unit;2using System;3using System.Threading.Tasks;4{5 {6 static async Task Main(string[] args)7 {8 var docker = new Docker();9 var container = await docker.RunAsync();10 var containerId = container.Id;11 Console.WriteLine(containerId);12 }13 }14}15using DotNet.Testcontainers.Tests.Fixtures;16using System;17using System.Threading.Tasks;18{19 {20 static async Task Main(string[] args)21 {22 var docker = new Docker();23 var container = await docker.RunAsync();24 var containerId = container.Id;25 Console.WriteLine(containerId);26 }27 }28}29using DotNet.Testcontainers.Tests.Integration;30using System;31using System.Threading.Tasks;32{33 {34 static async Task Main(string[] args)35 {36 var docker = new Docker();37 var container = await docker.RunAsync();38 var containerId = container.Id;39 Console.WriteLine(containerId);40 }41 }42}43using DotNet.Testcontainers.Tests.Functional;44using System;45using System.Threading.Tasks;46{47 {48 static async Task Main(string[] args)49 {50 var docker = new Docker();51 var container = await docker.RunAsync();52 var containerId = container.Id;53 Console.WriteLine(containerId);54 }55 }56}57using DotNet.Testcontainers.Tests.EndToEnd;58using System;59using System.Threading.Tasks;60{61 {62 static async Task Main(string[] args)63 {64 var docker = new Docker();65 var container = await docker.RunAsync();66 var containerId = container.Id;67 Console.WriteLine(containerId);68 }69 }70}71using DotNet.Testcontainers.Tests.Acceptance;72using System;73using System.Threading.Tasks;
Docker
Using AI Code Generation
1using DotNet.Testcontainers.Tests.Unit;2using Docker.DotNet;3using Docker.DotNet.Models;4using System.Collections.Generic;5using System.Threading.Tasks;6using Xunit;7using Xunit.Abstractions;8{9 {10 private readonly ITestOutputHelper output;11 public DockerTest(ITestOutputHelper output)12 {13 this.output = output;14 }15 public async Task TestGetContainers()16 {17 var docker = new Docker();18 var containers = await docker.GetContainersAsync();19 foreach (var container in containers)20 {21 output.WriteLine(container.ID);22 }23 }24 }25}26using DotNet.Testcontainers.Tests.Fixtures;27using Docker.DotNet;28using Docker.DotNet.Models;29using System.Collections.Generic;30using System.Threading.Tasks;31using Xunit;32using Xunit.Abstractions;33{34 {35 private readonly ITestOutputHelper output;36 public DockerTest(ITestOutputHelper output)37 {38 this.output = output;39 }40 public async Task TestGetContainers()41 {42 var docker = new Docker();43 var containers = await docker.GetContainersAsync();44 foreach (var container in containers)45 {46 output.WriteLine(container.ID);47 }48 }49 }50}
Docker
Using AI Code Generation
1using DotNet.Testcontainers.Tests.Unit;2using System;3{4 {5 public string GetDockerHost()6 {7 return new Docker().Host;8 }9 }10}11 public DockerTest(ITestOutputHelper output)12 {13 this.output = output;14 }15 public async Task TestGetContainers()16 {17 var docker = new Docker();18 var containers = await docker.GetContainersAsync();19 foreach (var container in containers)20 {21 output.WriteLine(container.ID);22 }23 }24 }25}26using DotNet.Testcontainers.Tests.Unit;27using System;28using System.Threading.Tasks;29{30 {31 static async Task Main(string[] args)32 {33 var docker = new Docker();34 await docker.StartAsync();35 Console.WriteLine("Hello World!");36 }37 }38}39NU1102: Unable to find package DotNet.Testcontainers.Tests.Unit with version (>= 0.16.0)40 - Found 1 version(s) in nuget.org [ Nearest version: 0.15.0 ]41 - Found 0 version(s) in Microsoft Visual Studio Offline Packages42NU1102: Unable to find package DotNet.Testcontainers with version (>= 0.16.0)43 - Found 1 version(s) in nuget.org [ Nearest version: 0.15.0 ]44 - Found 0 version(s) in Microsoft Visual Studio Offline Packages45NU1102: Unable to find package DotNet.Testcontainers.Tests.Unit with version (>= 0.16.0)46 - Found 1 version(s) in nuget.org [ Nearest version: 0.15.0 ]47 - Found 0 version(s) in Microsoft Visual Studio Offline Packages48NU1102: Unable to find package DotNet.Testcontainers.Tests.Unit with version (>= 0.16.0)49 - Found 1 version(s) in nuget.org
Docker
Using AI Code Generation
1using DotNet.Testcontainers.Tests.Unit;2using Docker.DotNet;3using Docker.DotNet.Models;4using System.Collections.Generic;5using System.Threading.Tasks;6using Xunit;7using Xunit.Abstractions;8{9 {10 private readonly ITestOutputHelper output;11 public DockerTest(ITestOutputHelper output)12 {13 this.output = output;14 }15 public async Task TestGetContainers()16 {17 var docker = new Docker();18 var containers = await docker.GetContainersAsync();19 foreach (var container in containers)20 {21 output.WriteLine(container.ID);22 }23 }24 }25}26using DotNet.Testcontainers.Tests.Fixtures;27using Docker.DotNet;28using Docker.DotNet.Models;29using System.Collections.Generic;30using System.Threading.Tasks;31using Xunit;32using Xunit.Abstractions;33{34 {35 private readonly ITestOutputHelper output;36 public DockerTest(ITestOutputHelper output)37 {38 this.output = output;39 }40 public async Task TestGetContainers()41 {42 var docker = new Docker();43 var containers = await docker.GetContainersAsync();44 foreach (var container in containers)45 {46 output.WriteLine(container.ID);47 }48 }49 }50}
Docker
Using AI Code Generation
1using DotNet.Testcontainers.Tests.Unit;2using System;3{4 {5 public string GetDockerHost()6 {7 return new Docker().Host;8 }9 }10}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!