Getting Started
Factory
To run Integration Tests with TestContainers library, first of all you need to have a factory to generate your Docker containers.
public class RestaurantMicroserviceFactory :
WebApplicationFactory<IAnchor>, IAsyncLifetime
{ }
Then you need to create your containers here's an example of MSSQL database container.
SQL Server
private readonly MsSqlTestcontainer _mssqlContainer;
public RestaurantMicroserviceFactory()
{
var mssqlConfiguration = new DatabaseContainerConfiguration();
_mssqlContainer = new TestcontainersBuilder<MsSqlTestcontainer>()
.WithDatabase(mssqlConfiguration)
.Build();
}
public Task InitializeAsync()
{
return _mssqlContainer.StartAsync();
}
public Task DisposeAsync()
{
return _mssqlContainer.DisposeAsync().AsTask();
}
Tests
When your factory is ready write some tests.
public sealed class RestaurantMicroserviceTest : IClassFixture<RestaurantMicroserviceFactory>, IDisposable
{
public RestaurantMicroserviceTest(RestaurantMicroserviceFactory restaurantMicroserviceFactory)
{
_httpClient = _webApplicationFactory.CreateClient();
}
public void Dispose()
{
_httpClient.Dispose();
}
[Fact]
public async Task GetRestaurantById_ShouldReturnSuccess_WhenCalled()
{
// Arrange
var id = 1;
const string path = $"api/v1/Restaurants/{id}";
// Act
var response = await _httpClient.GetAsync(path);
// Assert
response....
}
}
Last modified: 21 May 2024