Best Testcontainers-dotnet code snippet using DotNet.Testcontainers.Containers.TestcontainersContainer.GetMappedPublicPort
PubSubTests.cs
Source:PubSubTests.cs
...33 .WithPortBinding("1883", true)34 .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(1883));35 mosquittoNoAuthContainer = testcontainersBuilder.Build();36 await mosquittoNoAuthContainer.StartAsync();37 var subscriber1 = new MqttEventSubscriber("mqtt://localhost:" + mosquittoNoAuthContainer.GetMappedPublicPort(1883), 38 new JsonNETSerializer(new Newtonsoft.Json.JsonSerializerSettings()), "admin", "password");39 await subscriber1.Start();40 var testEventHandler1 = new TestEventHandler1();41 await subscriber1.SubscribeAsync<TestEventHandler1, TestEvent1>("testing", testEventHandler1);42 var subscriber2 = new MqttEventSubscriber("mqtt://localhost:" + mosquittoNoAuthContainer.GetMappedPublicPort(1883), 43 new JsonNETSerializer(new Newtonsoft.Json.JsonSerializerSettings()), "admin", "password");44 await subscriber2.Start();45 var testEventHandler2 = new TestEventHandler2();46 await subscriber2.SubscribeAsync<TestEventHandler2, TestEvent1>("testing", testEventHandler2);47 var publisher = new MqttEventPublisher("mqtt://localhost:" + mosquittoNoAuthContainer.GetMappedPublicPort(1883), 48 new JsonNETSerializer(new Newtonsoft.Json.JsonSerializerSettings()), "testing", "admin", "password");49 await publisher.Start();50 var testEvent1 = new TestEvent1() {51 Prop1 = Guid.NewGuid().ToString()52 };53 await publisher.Publish(testEvent1);54 Thread.Sleep(1000);55 Assert.That(testEventHandler1.ReceivedEvents.Count, Is.EqualTo(1));56 Assert.That(testEventHandler1.ReceivedEvents[0].Prop1, Is.EqualTo(testEvent1.Prop1));57 Assert.That(testEventHandler2.ReceivedEvents.Count, Is.EqualTo(1));58 Assert.That(testEventHandler2.ReceivedEvents[0].Prop1, Is.EqualTo(testEvent1.Prop1));59 var testEvent2 = new TestEvent1() {60 Prop1 = Guid.NewGuid().ToString()61 };...
MockServerFixture.cs
Source:MockServerFixture.cs
...19 public MockServerFixture()20 {21 _container = CreateMySqlContainer();22 Hostname = _container.Hostname;23 Port = _container.GetMappedPublicPort("8882");24 var mockMapper = new MapperConfiguration(cfg =>25 {26 cfg.AddProfile(new OrderProfile());27 });28 Mapper = mockMapper.CreateMapper();29 }30 public TestcontainersContainer CreateMySqlContainer()31 {32 var container = new TestcontainersBuilder<TestcontainersContainer>()33 .WithImage("node:6.11-slim")34 .WithWorkingDirectory("/mocks")35 .WithMount("../../../../dependencies/stubby/", "/mocks")36 .WithEntrypoint("/bin/sh", "-c", "npm install -g stubby@4.1.1 && stubby -d /mocks/init.yml -s 8882 -a 8889 -w")37 .WithCleanUp(false)...
SfMockAuthServerFixture.cs
Source:SfMockAuthServerFixture.cs
...28 .WithPortBinding(3001)29 .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(3001));30 Container = containerBuilder.Build();31 await Container.StartAsync();32 PublicPort = Container.GetMappedPublicPort(3001);33 }34 public async Task DisposeAsync()35 {36 await Container.StopAsync();37 }38 }39}...
GetMappedPublicPort
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using DotNet.Testcontainers.Containers.Builders;4using DotNet.Testcontainers.Containers.Configurations;5using DotNet.Testcontainers.Containers.Modules;6using DotNet.Testcontainers.Containers.WaitStrategies;7{8 {9 static async Task Main(string[] args)10 {11 var testcontainersBuilder = new TestcontainersBuilder<GenericContainer>()12 .WithImage("nginx")13 .WithPortBinding(80)14 .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(80));15 using (var container = testcontainersBuilder.Build())16 {17 await container.StartAsync();18 var port = container.GetMappedPublicPort(80);19 Console.WriteLine($"Nginx is available on port {port}.");20 }21 }22 }23}
GetMappedPublicPort
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using DotNet.Testcontainers.Containers;4using DotNet.Testcontainers.Containers.Builders;5using DotNet.Testcontainers.Containers.Modules;6using DotNet.Testcontainers.Containers.WaitStrategies;7using DotNet.Testcontainers.Images;8{9 {10 static async Task Main(string[] args)11 {12 var container = new TestcontainersBuilder<TestcontainersContainer>()13 .WithImage("redis")14 .WithPortBinding(6379)15 .WithWaitStrategy(Wait.ForUnixContainer()16 .UntilPortIsAvailable(6379))17 .Build();18 await container.StartAsync();19 Console.WriteLine(container.GetMappedPublicPort(6379));20 await container.StopAsync();21 }22 }23}
GetMappedPublicPort
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using DotNet.Testcontainers.Containers.Modules;4using DotNet.Testcontainers.Containers.Modules.Databases;5using DotNet.Testcontainers.Containers.WaitStrategies;6using DotNet.Testcontainers.Images;7using DotNet.Testcontainers.Images.Builders;8using DotNet.Testcontainers.Tests.Fixtures;9using Xunit;10{11 {12 private readonly WindowsDockerFixture dockerFixture;13 public TestcontainersContainerTest(WindowsDockerFixture dockerFixture)14 {15 this.dockerFixture = dockerFixture;16 }17 public async Task GetMappedPublicPort()18 {19 var testcontainersBuilder = new TestcontainersBuilder<MsSqlTestcontainer>()20 .WithDatabase(new MsSqlTestcontainerConfiguration("2017-latest"))21 .WithImage(new TestcontainersImageBuilder()22 .From("mcr.microsoft.com/mssql/server:2017-latest")23 .Build())24 .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(1433))25 .WithCleanUp(true);26 var testcontainers = testcontainersBuilder.Build();27 await testcontainers.StartAsync();28 var mappedPublicPort = testcontainers.GetMappedPublicPort(1433);29 await testcontainers.StopAsync();30 await testcontainers.DisposeAsync();31 Assert.True(mappedPublicPort > 0);32 }33 }34}35using System;36using System.Threading.Tasks;37using DotNet.Testcontainers.Containers.Modules;38using DotNet.Testcontainers.Containers.Modules.Databases;39using DotNet.Testcontainers.Containers.WaitStrategies;40using DotNet.Testcontainers.Images;41using DotNet.Testcontainers.Images.Builders;42using DotNet.Testcontainers.Tests.Fixtures;43using Xunit;44{45 {46 private readonly WindowsDockerFixture dockerFixture;47 public TestcontainersContainerTest(WindowsDockerFixture dockerFixture)48 {49 this.dockerFixture = dockerFixture;50 }
GetMappedPublicPort
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;6{7 {8 static async Task Main(string[] args)9 {10 var container = new TestcontainersBuilder<GenericContainer>()11 .WithImage("alpine")12 .WithCommand("tail -f /dev/null")13 .WithPortBinding(80, 8080)14 .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(80))15 .Build();16 await container.StartAsync();17 Console.WriteLine(container.GetMappedPublicPort(80));18 await container.StopAsync();19 }20 }21}22TestcontainersBuilder<GenericContainer> builder = new TestcontainersBuilder<GenericContainer>()23 .WithImage("alpine")24 .WithCommand("tail -f /dev/null")25 .WithPortBinding(80, 8080)26 .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(80));27TestcontainersContainer container = builder.Build();28await container.StartAsync();29await container.StopAsync();30Console.WriteLine(container.GetMappedPublicPort(80));31using System;32using System.Threading.Tasks;33using DotNet.Testcontainers.Containers.Builders;34using DotNet.Testcontainers.Containers.Modules;35using DotNet.Testcontainers.Containers.WaitStrategies;
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!!