Skip to content

Commit 183f56d

Browse files
committed
v0.0.2
1 parent de0cea3 commit 183f56d

File tree

8 files changed

+91
-14
lines changed

8 files changed

+91
-14
lines changed

Directory.Build.props

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
<RepositoryUrl>https://github.com/managedcode/IntegrationTestBaseKit</RepositoryUrl>
2525
<PackageProjectUrl>https://github.com/managedcode/IntegrationTestBaseKit</PackageProjectUrl>
2626
<Product>Managed Code - IntegrationTestBaseKit</Product>
27-
<Version>0.0.1</Version>
28-
<PackageVersion>0.0.1</PackageVersion>
27+
<Version>0.0.2</Version>
28+
<PackageVersion>0.0.2</PackageVersion>
2929

3030
</PropertyGroup>
3131
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">

ManagedCode.IntegrationTestBaseKit.Tests/HealthTests.cs

+17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using FluentAssertions;
2+
using TestBlazorApp;
3+
using Testcontainers.Azurite;
4+
using Testcontainers.PostgreSql;
25
using Xunit.Abstractions;
36

47
namespace ManagedCode.IntegrationTestBaseKit.Tests;
@@ -23,4 +26,18 @@ public async Task BrowserHealthTest()
2326
.Should()
2427
.BeTrue();
2528
}
29+
30+
[Fact]
31+
public void ConnectionStringTest()
32+
{
33+
StaticContainer.AzureBlobConnectionString
34+
.Should()
35+
.Be(testApplication.GetContainer<AzuriteContainer>()
36+
.GetConnectionString());
37+
38+
StaticContainer.PostgreSqlConnectionString
39+
.Should()
40+
.Be(testApplication.GetContainer<PostgreSqlContainer>("postgree")
41+
.GetConnectionString());
42+
}
2643
}

ManagedCode.IntegrationTestBaseKit.Tests/TestApp.cs

+8
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,12 @@ protected override async Task ConfigureTestContainers()
1313
AddContainer(new AzuriteBuilder().Build());
1414
AddContainer("postgree", new PostgreSqlBuilder().Build());
1515
}
16+
17+
protected override void ConfigureConfiguration()
18+
{
19+
SetConfigurationValue("AzureBlob", GetContainer<AzuriteContainer>()
20+
.GetConnectionString());
21+
SetConfigurationValue("ConnectionStrings:PostgreSql", GetContainer<PostgreSqlContainer>("postgree")
22+
.GetConnectionString());
23+
}
1624
}

ManagedCode.IntegrationTestBaseKit.XUnit/BaseXUnitTestApp.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
namespace ManagedCode.IntegrationTestBaseKit.XUnit;
44

5-
public abstract class BaseXUnitTestApp<TEntryPoint> : BaseTestApp<TEntryPoint>, IAsyncLifetime
6-
where TEntryPoint : class
5+
public abstract class BaseXUnitTestApp<TEntryPoint> : BaseTestApp<TEntryPoint>, IAsyncLifetime where TEntryPoint : class
76
{
7+
async Task IAsyncLifetime.InitializeAsync()
8+
{
9+
await InitializeAsync();
10+
CreateHttpClient();
11+
}
12+
813
async Task IAsyncLifetime.DisposeAsync()
914
{
10-
await base.DisposeAsync();
15+
await DisposeAsync();
1116
}
1217
}

ManagedCode.IntegrationTestBaseKit/BaseTestApp.cs

+43-9
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@
55
using Microsoft.AspNetCore.Http.Connections.Client;
66
using Microsoft.AspNetCore.Mvc.Testing;
77
using Microsoft.AspNetCore.SignalR.Client;
8+
using Microsoft.Extensions.Configuration;
89
using Microsoft.Extensions.DependencyInjection;
910
using Microsoft.Extensions.Hosting;
1011
using Microsoft.Playwright;
1112

1213
namespace ManagedCode.IntegrationTestBaseKit;
1314

14-
public abstract class BaseTestApp<TEntryPoint> : WebApplicationFactory<TEntryPoint>
15-
where TEntryPoint : class
15+
public abstract class BaseTestApp<TEntryPoint> : WebApplicationFactory<TEntryPoint> where TEntryPoint : class
1616
{
1717
private IHost? _host;
1818

19+
private readonly ConfigurationBuilder ConfigurationBuilder = new();
20+
21+
protected virtual bool UsePlaywright { get; } = true;
1922
private PlaywrightWrapper Fixture { get; } = new();
2023

2124
protected Dictionary<string, DockerContainer> Containers { get; } = new();
@@ -62,18 +65,44 @@ public T GetContainer<T>() where T : DockerContainer
6265
public virtual async Task InitializeAsync()
6366
{
6467
await ConfigureTestContainers();
65-
await Fixture.InitializeAsync();
68+
69+
if (UsePlaywright)
70+
await Fixture.InitializeAsync();
71+
6672
foreach (var container in Containers)
6773
await container.Value.StartAsync();
6874
}
6975

7076
protected override IHost CreateHost(IHostBuilder builder)
7177
{
78+
ConfigureConfiguration();
79+
var configuration = ConfigurationBuilder.Build();
80+
builder.ConfigureWebHost(hostBuilder =>
81+
{
82+
foreach (var setting in configuration.AsEnumerable(true))
83+
hostBuilder.UseSetting(setting.Key, setting.Value);
84+
});
85+
86+
// Create the host for TestServer now before we
87+
// modify the builder to use Kestrel instead.
7288
var testHost = builder.Build();
89+
90+
// Modify the host builder to use Kestrel instead
91+
// of TestServer so we can listen on a real address.
7392
builder.ConfigureWebHost(hostBuilder => hostBuilder.UseKestrel());
74-
_host = builder.Build();
93+
94+
// Create and start the Kestrel server before the test server,
95+
// otherwise due to the way the deferred host builder works
96+
// for minimal hosting, the server will not get "initialized
97+
// enough" for the address it is listening on to be available.
98+
// See https://github.com/dotnet/aspnetcore/issues/33846.
99+
_host = builder.Build(); //base.CreateHost(builder);
75100
_host.Start();
76101

102+
// Extract the selected dynamic port out of the Kestrel server
103+
// and assign it onto the client options for convenience so it
104+
// "just works" as otherwise it'll be the default http://localhost
105+
// URL, which won't route to the Kestrel-hosted HTTP server.
77106
var server = _host.Services.GetRequiredService<IServer>();
78107
var addressFeature = server.Features.Get<IServerAddressesFeature>();
79108
ClientOptions.BaseAddress = addressFeature!.Addresses
@@ -84,13 +113,9 @@ protected override IHost CreateHost(IHostBuilder builder)
84113
return testHost;
85114
}
86115

87-
protected override void ConfigureWebHost(IWebHostBuilder builder)
88-
{
89-
builder.UseEnvironment("Development");
90-
}
91-
92116
public override async ValueTask DisposeAsync()
93117
{
118+
_host?.Dispose();
94119
await Fixture.DisposeAsync();
95120
foreach (var container in Containers)
96121
{
@@ -125,6 +150,9 @@ public HubConnection CreateSignalRClient(string hubUrl, Action<HubConnectionBuil
125150

126151
public async Task<IPage> OpenNewPage(string url)
127152
{
153+
if (!UsePlaywright)
154+
throw new InvalidOperationException("Playwright is not enabled");
155+
128156
var fullUrl = new Uri(ServerUri, url).ToString();
129157
var context = await Browser.NewContextAsync();
130158
var page = await context.NewPageAsync();
@@ -144,4 +172,10 @@ protected void AddContainer(DockerContainer container)
144172
}
145173

146174
protected abstract Task ConfigureTestContainers();
175+
protected abstract void ConfigureConfiguration();
176+
177+
protected void SetConfigurationValue(string key, string value)
178+
{
179+
ConfigurationBuilder.AddInMemoryCollection(new Dictionary<string, string> { { key, value } }!);
180+
}
147181
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dotnet add package ManagedCode.IntegrationTestBaseKit
2121
```
2222

2323
for xUnit integration use the following command:
24+
2425
```sh
2526
dotnet add package ManagedCode.ManagedCode.IntegrationTestBaseKit.XUnit
2627
```

TestBlazorApp/Program.cs

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public static void Main(string[] args)
2121
.AddHealthChecks()
2222
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
2323

24+
25+
StaticContainer.PostgreSqlConnectionString = builder.Configuration.GetConnectionString("PostgreSql");
26+
StaticContainer.AzureBlobConnectionString = builder.Configuration.GetValue<string>("AzureBlob");
27+
28+
2429
var app = builder.Build();
2530

2631
// Configure the HTTP request pipeline.

TestBlazorApp/StaticContainer.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace TestBlazorApp;
2+
3+
public static class StaticContainer
4+
{
5+
public static string PostgreSqlConnectionString;
6+
public static string AzureBlobConnectionString;
7+
}

0 commit comments

Comments
 (0)