Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Add Microsoft SQL Server example #1008

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions docs/modules/microsoft-sql-server.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Microsoft SQL Server

[Microsoft SQL Server](https://www.microsoft.com/en-us/sql-server), also known as MSSQL, is a relational database engine developed by Microsoft and is a popular choice in enterprise systems. The following example provides .NET developers with a starting point to use a Microsoft SQL Server instance in the [xUnit][xunit] tests.

The following example uses the following NuGet packages:

```console title="Install the NuGet dependencies"
dotnet add package Testcontainers.MsSql
dotnet add package Microsoft.Data.SqlClient
dotnet add package xunit
```

IDEs and editors may also require the following packages to run tests: `xunit.runner.visualstudio` and `Microsoft.NET.Test.Sdk`.

Copy and paste the following code into a new `.cs` test file within an existing test project.

```csharp
using Microsoft.Data.SqlClient;
using Testcontainers.MsSql;
using Xunit;

namespace TestcontainersModules;

public class MsSqlServerContainerTest : IAsyncLifetime
{
private readonly MsSqlContainer msSqlContainer
= new MsSqlBuilder()
.Build();

[Fact]
public async Task ReadFromMsSqlDatabase()
{
await using var connection = new SqlConnection(msSqlContainer.GetConnectionString());
await connection.OpenAsync();

await using var command = connection.CreateCommand();
command.CommandText = "Select 1;";

var actual = await command.ExecuteScalarAsync() as int?;
Assert.Equal(expected: 1, actual: actual.GetValueOrDefault());
}

public Task InitializeAsync()
=> msSqlContainer.StartAsync();

public Task DisposeAsync()
=> msSqlContainer.DisposeAsync().AsTask();
}
```

To execute the tests, use the command `dotnet test` from a terminal.

## A Note To Developers

Once Testcontainers creates a server instance, developers may use the connection string with any of the popular data-access technologies found in the .NET Ecosystem. Some of these libraries include [Entity Framework Core](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore), [Dapper](https://www.nuget.org/packages/Dapper), and [NHibernate](https://www.nuget.org/packages/NHibernate). At which point, developers can execute database migrations and SQL scripts.

[xunit]: https://xunit.net/