Best Testcontainers-dotnet code snippet using DotNet.Testcontainers.Builders.TestcontainersBuilderDatabaseExtension
Program.cs
Source:Program.cs
...10{11 var dbBuilder = new DotNet.Testcontainers.Builders.TestcontainersBuilder<DotNet.Testcontainers.Containers.MsSqlTestcontainer>();12 var config = new DotNet.Testcontainers.Configurations.MsSqlTestcontainerConfiguration();13 config.Password = "Test123Friday!";14 var dbBuilder2 = DotNet.Testcontainers.Builders.TestcontainersBuilderDatabaseExtension.WithDatabase(dbBuilder, config);15 await using (var testcontainers = dbBuilder2.Build())16 {17 await testcontainers.StartAsync();18 // Server=localhost,49159;Database=master;User Id=sa;Password=Test123Friday!; << doesn't work with localhost19 // Server=127.0.0.1,49159;Database=master;User Id=sa;Password=Test123Friday!;20 WriteLine($"Started SQL Server. Connect with {testcontainers.ConnectionString}");21 WriteLine("Press enter to end it");22 ReadLine();23 }24}25async Task Redis()26{27 var dbBuilder = new DotNet.Testcontainers.Builders.TestcontainersBuilder<DotNet.Testcontainers.Containers.RedisTestcontainer>();28 var config = new DotNet.Testcontainers.Configurations.RedisTestcontainerConfiguration();29 var dbBuilder2 = DotNet.Testcontainers.Builders.TestcontainersBuilderDatabaseExtension.WithDatabase(dbBuilder, config);30 await using (var testcontainers = dbBuilder2.Build())31 {32 await testcontainers.StartAsync();33 WriteLine($"Started Redis. Connect with {testcontainers.ConnectionString}");34 // localhost:4916135 WriteLine("Press enter to end it");36 ReadLine();37 }38}39// async Task Kafka2(){40// await Task.Delay(1);41// KafkaTestcontainerConfiguration configuration = new KafkaTestcontainerConfiguration();42// var kafkaBuilder1 = new DotNet.Testcontainers.Builders.TestcontainersBuilder<KafkaTestcontainer>();43// var kafkaBuilder = kafkaBuilder1.WithKafka(configuration);...
TestcontainersBuilderDatabaseExtension.cs
Source:TestcontainersBuilderDatabaseExtension.cs
...7 /// <summary>8 /// This class applies the extended Testcontainer configurations for databases.9 /// </summary>10 [PublicAPI]11 public static class TestcontainersBuilderDatabaseExtension12 {13 public static ITestcontainersBuilder<T> WithDatabase<T>(this ITestcontainersBuilder<T> builder, TestcontainerDatabaseConfiguration configuration)14 where T : TestcontainerDatabase15 {16 builder = configuration.Environments.Aggregate(builder, (current, environment)17 => current.WithEnvironment(environment.Key, environment.Value));18 return builder19 .WithImage(configuration.Image)20 .WithExposedPort(configuration.DefaultPort)21 .WithPortBinding(configuration.Port, configuration.DefaultPort)22 .WithOutputConsumer(configuration.OutputConsumer)23 .WithWaitStrategy(configuration.WaitStrategy)24 .ConfigureContainer(container =>25 {...
TestcontainersBuilderDatabaseExtension
Using AI Code Generation
1using DotNet.Testcontainers.Builders;2using DotNet.Testcontainers.Containers;3using DotNet.Testcontainers.Containers.Builders;4using DotNet.Testcontainers.Containers.Modules;5using DotNet.Testcontainers.Containers.WaitStrategies;6using DotNet.Testcontainers.Images;7using DotNet.Testcontainers.Images.Builders;8using DotNet.Testcontainers.Images.Configurations;9using System.Threading.Tasks;10{11 {12 public static async Task Main(string[] args)13 {14 var builder = new TestcontainersBuilder<TestcontainersContainer>()15 .WithImage("postgres")16 .WithDatabase(new TestcontainersDatabaseBuilder()17 .WithDatabase("postgres")18 .WithUsername("postgres")19 .WithPassword("postgres")20 .Build())21 .WithEnvironment("POSTGRES_PASSWORD", "postgres")22 .WithEnvironment("POSTGRES_USER", "postgres")23 .WithEnvironment("POSTGRES_DB", "postgres")24 .WithPortBinding(5432)25 .WithWaitStrategy(Wait.ForUnixContainer()26 .UntilPortIsAvailable(5432)27 .UntilCommandIsCompleted("pg_isready -U postgres"));28 using (var postgres = builder.Build())29 {30 await postgres.StartAsync();31 }32 }33 }34}35using DotNet.Testcontainers.Builders;36using DotNet.Testcontainers.Containers;37using DotNet.Testcontainers.Containers.Builders;38using DotNet.Testcontainers.Containers.Modules;39using DotNet.Testcontainers.Containers.WaitStrategies;40using DotNet.Testcontainers.Images;41using DotNet.Testcontainers.Images.Builders;42using DotNet.Testcontainers.Images.Configurations;43using System.Threading.Tasks;44{45 {46 public static async Task Main(string[] args)47 {48 var builder = new TestcontainersBuilder<TestcontainersContainer>()49 .WithImage("postgres")50 .WithDatabase(new TestcontainersDatabaseBuilder()51 .WithDatabase("postgres")52 .WithUsername("postgres")53 .WithPassword("postgres")54 .Build())55 .WithEnvironment("POSTGRES_PASSWORD", "postgres")56 .WithEnvironment("POSTGRES_USER", "postgres")57 .WithEnvironment("POSTGRES_DB", "postgres")58 .WithPortBinding(5432)59 .WithWaitStrategy(Wait
TestcontainersBuilderDatabaseExtension
Using AI Code Generation
1using DotNet.Testcontainers.Builders;2using DotNet.Testcontainers.Containers;3using DotNet.Testcontainers.Containers.Builders;4using DotNet.Testcontainers.Containers.Modules;5using DotNet.Testcontainers.Containers.WaitStrategies;6using System;7using System.Data.SqlClient;8using System.Threading.Tasks;9{10 {11 static async Task Main(string[] args)12 {13 var testcontainer = new TestcontainersBuilder<MsSqlTestcontainer>()14 .WithDatabase(new MsSqlTestcontainerConfiguration("2017-latest"))15 .Build();16 await testcontainer.StartAsync();17 var connectionString = testcontainer.ConnectionString;18 using (SqlConnection connection = new SqlConnection(connectionString))19 {20 {21 connection.Open();22 Console.WriteLine("Connected");23 }24 catch (Exception ex)25 {26 Console.WriteLine(ex.Message);27 }28 }29 Console.ReadLine();30 }31 }32}33using DotNet.Testcontainers.Builders;34using DotNet.Testcontainers.Containers;35using DotNet.Testcontainers.Containers.Builders;36using DotNet.Testcontainers.Containers.Modules;37using DotNet.Testcontainers.Containers.WaitStrategies;38using System;39using System.Data.SqlClient;40using System.Threading.Tasks;41{42 {43 static async Task Main(string[] args)44 {45 var testcontainer = new TestcontainersBuilder<MsSqlTestcontainer>()46 .WithDatabase(new MsSqlTestcontainerConfiguration("2017-latest"))47 .Build();48 await testcontainer.StartAsync();49 var connectionString = testcontainer.ConnectionString;50 using (SqlConnection connection = new SqlConnection(connectionString))51 {52 {53 connection.Open();54 Console.WriteLine("Connected");55 }56 catch (Exception ex)57 {58 Console.WriteLine(ex.Message);59 }60 }61 Console.ReadLine();62 }63 }64}65using DotNet.Testcontainers.Builders;66using DotNet.Testcontainers.Containers;67using DotNet.Testcontainers.Containers.Builders;68using DotNet.Testcontainers.Containers.Modules;69using DotNet.Testcontainers.Containers.WaitStrategies;70using System;71using System.Data.SqlClient;72using System.Threading.Tasks;73{74 {
TestcontainersBuilderDatabaseExtension
Using AI Code Generation
1using System;2using System.Data.SqlClient;3using DotNet.Testcontainers.Builders;4using DotNet.Testcontainers.Containers;5using DotNet.Testcontainers.Containers.Builders;6using DotNet.Testcontainers.Containers.Modules;7using DotNet.Testcontainers.Containers.WaitStrategies;8using DotNet.Testcontainers.Images;9{10 {11 static void Main(string[] args)12 {13 var testcontainersBuilderDatabaseExtension = new TestcontainersBuilderDatabaseExtension();14 var databaseContainer = testcontainersBuilderDatabaseExtension.Build();15 databaseContainer.StartAsync().Wait();16 var connectionString = databaseContainer.ConnectionString;17 Console.WriteLine(connectionString);18 var sqlConnection = new SqlConnection(connectionString);19 sqlConnection.Open();20 sqlConnection.Close();21 databaseContainer.StopAsync().Wait();22 }23 }24}25using System;26using System.Data.SqlClient;27using DotNet.Testcontainers.Builders;28using DotNet.Testcontainers.Containers;29using DotNet.Testcontainers.Containers.Builders;30using DotNet.Testcontainers.Containers.Modules;31using DotNet.Testcontainers.Containers.WaitStrategies;32using DotNet.Testcontainers.Images;33{34 {35 static void Main(string[] args)36 {37 var testcontainersBuilderDatabaseExtension = new TestcontainersBuilderDatabaseExtension();38 var databaseContainer = testcontainersBuilderDatabaseExtension.Build();39 databaseContainer.StartAsync().Wait();40 var connectionString = databaseContainer.ConnectionString;41 Console.WriteLine(connectionString);42 var sqlConnection = new SqlConnection(connectionString);43 sqlConnection.Open();44 sqlConnection.Close();45 databaseContainer.StopAsync().Wait();46 }47 }48}49using System;50using System.Data.SqlClient;51using DotNet.Testcontainers.Builders;52using DotNet.Testcontainers.Containers;53using DotNet.Testcontainers.Containers.Builders;54using DotNet.Testcontainers.Containers.Modules;55using DotNet.Testcontainers.Containers.WaitStrategies;56using DotNet.Testcontainers.Images;57{58 {59 static void Main(string[] args)60 {61 var testcontainersBuilderDatabaseExtension = new TestcontainersBuilderDatabaseExtension();62 var databaseContainer = testcontainersBuilderDatabaseExtension.Build();63 databaseContainer.StartAsync().Wait();
TestcontainersBuilderDatabaseExtension
Using AI Code Generation
1using System;2using System.Data.SqlClient;3using DotNet.Testcontainers.Builders;4using DotNet.Testcontainers.Containers.Builders;5using DotNet.Testcontainers.Containers.Modules.Databases;6using DotNet.Testcontainers.Containers.WaitStrategies;7using Microsoft.VisualStudio.TestTools.UnitTesting;8{9 {10 public void TestMethod1()11 {12 using (var sqlServer = new TestcontainersBuilder<TestcontainersContainer>()13 .WithDatabase(new TestcontainersBuilderDatabaseExtension())14 .WithImage("mcr.microsoft.com/mssql/server:2017-latest")15 .WithEnvironment("ACCEPT_EULA", "Y")16 .WithEnvironment("SA_PASSWORD", "P@ssword1")17 .WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(1433))18 .Build())19 {20 sqlServer.Start();21 var connectionString = sqlServer.ConnectionString;22 using (var connection = new SqlConnection(connectionString))23 {24 connection.Open();25 using (var command = new SqlCommand("CREATE TABLE [Test] ([Id] INT)", connection))26 {27 command.ExecuteNonQuery();28 }29 using (var command = new SqlCommand("INSERT INTO [Test] ([Id]) VALUES (1)", connection))30 {31 command.ExecuteNonQuery();32 }33 using (var command = new SqlCommand("SELECT [Id] FROM [Test]", connection))34 {35 using (var reader = command.ExecuteReader())36 {37 while (reader.Read())38 {39 Console.WriteLine(reader["Id"]);40 }41 }42 }43 }44 }45 }46 }47}
TestcontainersBuilderDatabaseExtension
Using AI Code Generation
1using DotNet.Testcontainers.Builders;2using DotNet.Testcontainers.Containers.Builders;3using DotNet.Testcontainers.Containers.Configurations.Databases;4using DotNet.Testcontainers.Containers.Modules.Databases;5using System.Threading.Tasks;6{7 {8 static async Task Main(string[] args)9 {10 await new TestcontainersBuilderDatabaseExtension()11 .WithDatabase(new PostgreSqlTestcontainerConfiguration("postgres:11.5-alpine"))12 .Build()13 .StartAsync();14 }15 }16}17using DotNet.Testcontainers.Builders;18using DotNet.Testcontainers.Containers.Builders;19using DotNet.Testcontainers.Containers.Configurations.Databases;20using DotNet.Testcontainers.Containers.Modules.Databases;21using System.Threading.Tasks;22{23 {24 static async Task Main(string[] args)25 {26 await new TestcontainersBuilderDatabaseExtension()27 .WithDatabase(new MySqlTestcontainerConfiguration("mysql:8.0.20"))28 .Build()29 .StartAsync();30 }31 }32}33using DotNet.Testcontainers.Builders;34using DotNet.Testcontainers.Containers.Builders;35using DotNet.Testcontainers.Containers.Configurations.Databases;36using DotNet.Testcontainers.Containers.Modules.Databases;37using System.Threading.Tasks;38{39 {40 static async Task Main(string[] args)41 {42 await new TestcontainersBuilderDatabaseExtension()43 .WithDatabase(new MariaDbTestcontainerConfiguration("mariadb:10.5.5"))44 .Build()45 .StartAsync();46 }47 }48}49using DotNet.Testcontainers.Builders;50using DotNet.Testcontainers.Containers.Builders;51using DotNet.Testcontainers.Containers.Configurations.Databases;52using DotNet.Testcontainers.Containers.Modules.Databases;53using System.Threading.Tasks;54{55 {56 static async Task Main(string[] args)57 {58 await new TestcontainersBuilderDatabaseExtension()59 .WithDatabase(new SqlServerTestcontainerConfiguration("
TestcontainersBuilderDatabaseExtension
Using AI Code Generation
1using DotNet.Testcontainers.Builders;2{3 {4 static void Main(string[] args)5 {6 var database = new TestcontainersBuilderDatabaseExtension()7 .WithDatabase(new PostgreSqlTestcontainerConfiguration())8 .Build();9 }10 }11}12using DotNet.Testcontainers.Builders;13{14 {15 static void Main(string[] args)16 {17 var database = new TestcontainersBuilderDatabaseExtension()18 .WithDatabase(new MySqlTestcontainerConfiguration())19 .Build();20 }21 }22}23using DotNet.Testcontainers.Builders;24{25 {26 static void Main(string[] args)27 {28 var database = new TestcontainersBuilderDatabaseExtension()29 .WithDatabase(new MariaDbTestcontainerConfiguration())30 .Build();31 }32 }33}34using DotNet.Testcontainers.Builders;35{36 {37 static void Main(string[] args)38 {39 var database = new TestcontainersBuilderDatabaseExtension()40 .WithDatabase(new SqlServerTestcontainerConfiguration())41 .Build();42 }43 }44}45using DotNet.Testcontainers.Builders;46{47 {48 static void Main(string[] args)49 {50 var database = new TestcontainersBuilderDatabaseExtension()51 .WithDatabase(new OracleTestcontainerConfiguration())52 .Build();53 }54 }55}56using DotNet.Testcontainers.Builders;57{58 {59 static void Main(string[] args)60 {61 var database = new TestcontainersBuilderDatabaseExtension()62 .WithDatabase(new MongoDBTestcontainerConfiguration())63 .Build();64 }
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!!