generated from frankhaugen/DotnetRepoTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new test bases and update logging system
Expanded the testing capabilities by creating HostApplicationTestBase and WebHostApplicationTestBase. These bases should make it easier to write and encapsulate common setup and teardown operations on tests involving WebHost and Host. Additionally, revised the logging system to include a "SimpleTestLogger" that operates without dependencies on the existing PulseFlow system. Made adjustments to related classes accordingly.
- Loading branch information
1 parent
c43c04e
commit 3747062
Showing
18 changed files
with
386 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Collections.Concurrent; | ||
|
||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
using Xunit.Abstractions; | ||
|
||
namespace Frank.Testing.Logging; | ||
|
||
public class SimpleTestLoggerProvider(ITestOutputHelper outputHelper, IOptionsMonitor<LoggerFilterOptions> options) : ILoggerProvider | ||
{ | ||
private readonly ConcurrentDictionary<string, SimpleTestLogger> _loggers = new(); | ||
|
||
/// <inheritdoc /> | ||
public void Dispose() | ||
{ | ||
_loggers.Clear(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public ILogger CreateLogger(string categoryName) => _loggers.GetOrAdd(categoryName, new SimpleTestLogger(outputHelper, options.CurrentValue.MinLevel, categoryName)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" /> | ||
<PackageReference Include="xunit.extensibility.core" Version="2.6.6" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Frank.Testing.Logging\Frank.Testing.Logging.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Frank.Testing.Logging; | ||
|
||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Frank.Testing.TestBases; | ||
|
||
public abstract class HostApplicationTestBase : IAsyncLifetime | ||
{ | ||
private readonly HostApplicationBuilder _hostApplicationBuilder; | ||
private IHost? _host; | ||
private readonly CancellationTokenSource _cancellationTokenSource = new(); | ||
private bool _initialized = false; | ||
|
||
protected HostApplicationTestBase(ITestOutputHelper outputHelper, LogLevel logLevel = LogLevel.Information) | ||
{ | ||
_hostApplicationBuilder = Host.CreateEmptyApplicationBuilder(new HostApplicationBuilderSettings()); | ||
_hostApplicationBuilder.Logging.AddSimpleTestLoggingProvider(outputHelper, logLevel); | ||
} | ||
|
||
public IServiceProvider Services => (_initialized ? _host?.Services : throw new InvalidOperationException("The host has not been initialized yet.")) ?? throw new InvalidOperationException("!!!"); | ||
|
||
protected virtual async Task SetupAsync(HostApplicationBuilder builder) => await Task.CompletedTask; | ||
|
||
public async Task InitializeAsync() | ||
{ | ||
await SetupAsync(_hostApplicationBuilder); | ||
_host = _hostApplicationBuilder.Build(); | ||
await _host.StartAsync(_cancellationTokenSource.Token); | ||
_initialized = true; | ||
} | ||
|
||
public async Task DisposeAsync() | ||
{ | ||
await _cancellationTokenSource.CancelAsync(); | ||
await _host?.StopAsync()!; | ||
await _host.WaitForShutdownAsync(); | ||
_host.Dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Frank.Testing.Logging; | ||
|
||
using Microsoft.AspNetCore; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Frank.Testing.TestBases; | ||
|
||
public abstract class WebHostApplicationTestBase : IAsyncLifetime | ||
{ | ||
private readonly IWebHostBuilder _hostApplicationBuilder; | ||
private IWebHost? _host; | ||
private readonly CancellationTokenSource _cancellationTokenSource = new(); | ||
private bool _initialized = false; | ||
|
||
protected WebHostApplicationTestBase(ITestOutputHelper outputHelper, LogLevel logLevel = LogLevel.Information) | ||
{ | ||
_hostApplicationBuilder = WebHost.CreateDefaultBuilder(); | ||
_hostApplicationBuilder.ConfigureLogging(logging => logging.AddSimpleTestLoggingProvider(outputHelper, logLevel)); | ||
} | ||
|
||
public IServiceProvider Services => (_initialized ? _host?.Services : throw new InvalidOperationException("The host has not been initialized yet.")) ?? throw new InvalidOperationException("!!!"); | ||
|
||
protected virtual async Task SetupAsync(IWebHostBuilder builder) => await Task.CompletedTask; | ||
|
||
protected HttpClient TestClient => (_initialized ? _host?.CreateTestClient() : throw new InvalidOperationException("The host has not been initialized yet.")) ?? throw new InvalidOperationException("!!!"); | ||
protected IEnumerable<string> GetServerEndpoints() => (_initialized ? _host?.GetServerEndpoints() : throw new InvalidOperationException("The host has not been initialized yet.")) ?? throw new InvalidOperationException("!!!"); | ||
public async Task InitializeAsync() | ||
{ | ||
await SetupAsync(_hostApplicationBuilder); | ||
_host = _hostApplicationBuilder.Build(); | ||
await _host.StartAsync(_cancellationTokenSource.Token); | ||
_initialized = true; | ||
} | ||
|
||
public async Task DisposeAsync() | ||
{ | ||
await _cancellationTokenSource.CancelAsync(); | ||
await _host?.StopAsync()!; | ||
await _host.WaitForShutdownAsync(); | ||
_host.Dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Hosting.Server; | ||
using Microsoft.AspNetCore.Hosting.Server.Features; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Frank.Testing.TestBases; | ||
|
||
public static class WebHostExtensions | ||
{ | ||
public static HttpClient CreateTestClient(this IWebHost host) | ||
{ | ||
var baseAddress = host.ServerFeatures.Get<IServerAddressesFeature>()?.Addresses.First(); | ||
|
||
return new HttpClient | ||
{ | ||
BaseAddress = new Uri(baseAddress ?? throw new InvalidOperationException("The host has not been initialized yet."), UriKind.Absolute) | ||
}; | ||
} | ||
|
||
public static IEnumerable<string?> GetServerEndpoints(this IWebHost host) | ||
{ | ||
return host.Services.GetServices<EndpointDataSource>().SelectMany(x => x.Endpoints).Select(x => x.DisplayName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.