Skip to content

Commit d2c7627

Browse files
committed
style
1 parent f73c866 commit d2c7627

17 files changed

+209
-192
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using DotNet.Testcontainers.Containers;
2+
using FluentAssertions;
3+
using Testcontainers.Azurite;
4+
using Testcontainers.PostgreSql;
5+
using Xunit.Abstractions;
6+
7+
namespace ManagedCode.IntegrationTestBaseKit.Tests;
8+
9+
[Collection(nameof(TestApp))]
10+
public class ContainerTests(ITestOutputHelper log, TestApp testApplication)
11+
{
12+
[Fact]
13+
public void TestcontainersTest()
14+
{
15+
var azurite = testApplication.GetContainer<AzuriteContainer>();
16+
var postgree = testApplication.GetContainer<PostgreSqlContainer>("postgree");
17+
18+
azurite.GetConnectionString()
19+
.Should()
20+
.NotBeNullOrWhiteSpace();
21+
22+
azurite.State
23+
.Should()
24+
.Be(TestcontainersStates.Running);
25+
26+
27+
postgree.GetConnectionString()
28+
.Should()
29+
.NotBeNullOrWhiteSpace();
30+
postgree.State
31+
.Should()
32+
.Be(TestcontainersStates.Running);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using FluentAssertions;
2+
using Xunit.Abstractions;
3+
4+
namespace ManagedCode.IntegrationTestBaseKit.Tests;
5+
6+
[Collection(nameof(TestApp))]
7+
public class HealthTests(ITestOutputHelper log, TestApp testApplication)
8+
{
9+
[Fact]
10+
public async Task HealthTest()
11+
{
12+
var client = testApplication.CreateHttpClient();
13+
var responce = await client.GetAsync("/health");
14+
responce.EnsureSuccessStatusCode();
15+
}
16+
17+
[Fact]
18+
public async Task BrowserHealthTest()
19+
{
20+
var page = await testApplication.OpenNewPage("/health");
21+
var content = await page.ContentAsync();
22+
content.Contains("Healthy")
23+
.Should()
24+
.BeTrue();
25+
}
26+
}

ManagedCode.IntegrationTestBaseKit.Tests/ManagedCode.IntegrationTestBaseKit.Tests.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
<ItemGroup>
1313
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
15-
<PackageReference Include="Testcontainers.Azurite" Version="3.9.0" />
16-
<PackageReference Include="Testcontainers.PostgreSql" Version="3.9.0" />
15+
<PackageReference Include="Testcontainers.Azurite" Version="3.9.0"/>
16+
<PackageReference Include="Testcontainers.PostgreSql" Version="3.9.0"/>
1717
<PackageReference Include="xunit" Version="2.5.3"/>
1818
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
1919
<PackageReference Include="FluentAssertions" Version="6.12.0"/>
@@ -24,8 +24,8 @@
2424
</ItemGroup>
2525

2626
<ItemGroup>
27-
<ProjectReference Include="..\ManagedCode.IntegrationTestBaseKit\ManagedCode.IntegrationTestBaseKit.csproj" />
28-
<ProjectReference Include="..\TestBlazorApp\TestBlazorApp.csproj" />
27+
<ProjectReference Include="..\ManagedCode.IntegrationTestBaseKit\ManagedCode.IntegrationTestBaseKit.csproj"/>
28+
<ProjectReference Include="..\TestBlazorApp\TestBlazorApp.csproj"/>
2929
</ItemGroup>
3030

3131
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using TestBlazorApp;
2+
using Testcontainers.Azurite;
3+
using Testcontainers.PostgreSql;
4+
5+
namespace ManagedCode.IntegrationTestBaseKit.Tests;
6+
7+
[CollectionDefinition(nameof(TestApp))]
8+
public class TestApp : BaseTestApp<Program>, ICollectionFixture<TestApp>, IAsyncLifetime
9+
{
10+
public async Task DisposeAsync()
11+
{
12+
await base.DisposeAsync();
13+
}
14+
15+
protected override async Task ConfigureTestContainers()
16+
{
17+
AddContainer(new AzuriteBuilder().Build());
18+
AddContainer("postgree", new PostgreSqlBuilder().Build());
19+
}
20+
}

ManagedCode.IntegrationTestBaseKit.Tests/UnitTest1.cs

-67
This file was deleted.

ManagedCode.IntegrationTestBaseKit/BaseKit.cs renamed to ManagedCode.IntegrationTestBaseKit/BaseTestApp.cs

+32-32
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,14 @@
1111

1212
namespace ManagedCode.IntegrationTestBaseKit;
1313

14-
public abstract class BaseTestApp<TEntryPoint> : WebApplicationFactory<TEntryPoint>
15-
where TEntryPoint : class
14+
public abstract class BaseTestApp<TEntryPoint> : WebApplicationFactory<TEntryPoint> where TEntryPoint : class
1615
{
1716
private IHost? _host;
18-
19-
private PlaywrightWrapper Fixture { get; } = new ();
2017

21-
private void EnsureServer()
22-
{
23-
if (_host is null)
24-
{
25-
// This forces WebApplicationFactory to bootstrap the server
26-
using var _ = CreateDefaultClient();
27-
}
28-
}
29-
18+
private PlaywrightWrapper Fixture { get; } = new();
19+
3020
protected Dictionary<string, DockerContainer> Containers { get; } = new();
31-
32-
public T GetContainer<T>(string name) where T : DockerContainer
33-
{
34-
return (T)Containers[name];
35-
}
36-
37-
public T GetContainer<T>() where T : DockerContainer
38-
{
39-
return (T)Containers[typeof(T).Name];
40-
}
41-
21+
4222
public IBrowser Browser => Fixture.Browser;
4323

4424
public Uri ServerUri
@@ -58,16 +38,34 @@ public string ServerAddress
5838
return ClientOptions.BaseAddress.ToString();
5939
}
6040
}
41+
42+
private void EnsureServer()
43+
{
44+
if (_host is null)
45+
{
46+
// This forces WebApplicationFactory to bootstrap the server
47+
using var _ = CreateDefaultClient();
48+
}
49+
}
50+
51+
public T GetContainer<T>(string name) where T : DockerContainer
52+
{
53+
return (T)Containers[name];
54+
}
55+
56+
public T GetContainer<T>() where T : DockerContainer
57+
{
58+
return (T)Containers[typeof(T).Name];
59+
}
60+
6161
public virtual async Task InitializeAsync()
6262
{
6363
await ConfigureTestContainers();
6464
await Fixture.InitializeAsync();
6565
foreach (var container in Containers)
66-
{
6766
await container.Value.StartAsync();
68-
}
6967
}
70-
68+
7169
protected override IHost CreateHost(IHostBuilder builder)
7270
{
7371
var testHost = builder.Build();
@@ -89,7 +87,7 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
8987
{
9088
builder.UseEnvironment("Development");
9189
}
92-
90+
9391
public override async ValueTask DisposeAsync()
9492
{
9593
await Fixture.DisposeAsync();
@@ -98,6 +96,7 @@ public override async ValueTask DisposeAsync()
9896
await container.Value.StopAsync();
9997
await container.Value.DisposeAsync();
10098
}
99+
101100
await base.DisposeAsync();
102101
}
103102

@@ -122,7 +121,7 @@ public HubConnection CreateSignalRClient(string hubUrl, Action<HubConnectionBuil
122121
})
123122
.Build();
124123
}
125-
124+
126125
public async Task<IPage> OpenNewPage(string url)
127126
{
128127
var fullUrl = new Uri(ServerUri, url).ToString();
@@ -131,15 +130,16 @@ public async Task<IPage> OpenNewPage(string url)
131130
await page.GotoAsync(fullUrl);
132131
return page;
133132
}
134-
133+
135134
protected void AddContainer(string name, DockerContainer container)
136135
{
137136
Containers.Add(name, container);
138137
}
139-
138+
140139
protected void AddContainer(DockerContainer container)
141140
{
142-
Containers.Add(container.GetType().Name, container);
141+
Containers.Add(container.GetType()
142+
.Name, container);
143143
}
144144

145145
protected abstract Task ConfigureTestContainers();

ManagedCode.IntegrationTestBaseKit/ManagedCode.IntegrationTestBaseKit.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@
1919
</ItemGroup>
2020

2121

22-
2322
</Project>

ManagedCode.IntegrationTestBaseKit/PlaywrightFixture.cs renamed to ManagedCode.IntegrationTestBaseKit/PlaywrightWrapper.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public Task InitializeAsync()
1515
Headless = true
1616
});
1717
}
18-
18+
1919
public async Task InitializeAsync(BrowserTypeLaunchOptions options)
2020
{
2121
await PlaywrightInstaller.Install();

README.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
# IntegrationTestBaseKit
22

33
## Overview
4-
IntegrationTestBaseKit is a library designed to facilitate the creation and management of Docker containers for integration testing purposes. It provides a set of tools to start, stop, and check the readiness of Docker containers in a thread-safe manner.
4+
5+
IntegrationTestBaseKit is a library designed to facilitate the creation and management of Docker containers for
6+
integration testing purposes. It provides a set of tools to start, stop, and check the readiness of Docker containers in
7+
a thread-safe manner.
58

69
## Features
10+
711
- Start and stop Docker containers asynchronously.
812
- Check container readiness using customizable wait strategies.
913
- Event-driven notifications for container lifecycle events (starting, started, stopping, stopped).
1014

1115
## Installation
16+
1217
To install the library, use the following command:
1318

1419
```sh
1520
dotnet add package ManagedCode.IntegrationTestBaseKit
1621
```
1722

1823
## Usage
19-
Creating a Test Application
20-
Define a TestApp class that inherits from `BaseTestApp<TestBlazorApp.Program>`, implements `ICollectionFixture<TestApp>` and `IAsyncLifetime`.
2124

25+
Creating a Test Application
26+
Define a TestApp class that inherits from `BaseTestApp<TestBlazorApp.Program>`, implements `ICollectionFixture<TestApp>`
27+
and `IAsyncLifetime`.
2228

2329
```csharp
2430
using DotNet.Testcontainers.Containers;
@@ -51,8 +57,8 @@ namespace ManagedCode.IntegrationTestBaseKit.Tests
5157
```
5258

5359
## Writing Tests
54-
Use the TestApp class in your tests to manage Docker containers.
5560

61+
Use the TestApp class in your tests to manage Docker containers.
5662

5763
```csharp
5864
using DotNet.Testcontainers.Containers;

0 commit comments

Comments
 (0)