Skip to content

Commit

Permalink
add support for HostApplicationBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Mar 19, 2024
1 parent ab35518 commit 948c416
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
<PropertyGroup>
<MinVerTagPrefix>v</MinVerTagPrefix>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
<PackageReference Include="MinVer" Version="4.3.0" PrivateAssets="All" />
<PackageReference Include="MinVer" Version="5.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
Expand Down
11 changes: 11 additions & 0 deletions src/XUnit.Hosting/ITestHostFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

using Microsoft.Extensions.Hosting;

namespace XUnit.Hosting;

public interface ITestHostFixture
{
IHost Host { get; }

IServiceProvider Services { get; }
}
76 changes: 76 additions & 0 deletions src/XUnit.Hosting/TestApplicationFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

using XUnit.Hosting.Logging;

namespace XUnit.Hosting;

/// <summary>
/// XUnit collection fixture that supports <see cref="HostApplicationBuilder"/>
/// </summary>
public abstract class TestApplicationFixture : ITestHostFixture
{
private readonly Lazy<IHost> _host;

/// <summary>
/// Initializes a new instance of the <see cref="TestApplicationFixture"/> class.
/// </summary>
protected TestApplicationFixture()
{
_host = new Lazy<IHost>(CreateHost);
}

/// <summary>
/// Gets the host for this test.
/// </summary>
/// <value>
/// The host for this test.
/// </value>
public IHost Host => _host.Value;

/// <summary>
/// Gets the services configured for this test
/// </summary>
/// <value>
/// The services configured for this test.
/// </value>
public IServiceProvider Services => Host.Services;

/// <summary>
/// Create the test host program abstraction.
/// </summary>
/// <returns>An initialized <see cref="IHost"/>.</returns>
protected virtual IHost CreateHost()
{
var settings = CreateBuilderSettings();

var builder = Microsoft.Extensions.Hosting.Host.CreateApplicationBuilder(settings);

ConfigureApplication(builder);

var app = builder.Build();

app.Start();

return app;
}

/// <summary>
/// Creates the settings for constructing an Microsoft.Extensions.Hosting.HostApplicationBuilder.
/// </summary>
/// <returns>A new instance of <see cref="HostApplicationBuilderSettings"/></returns>
protected virtual HostApplicationBuilderSettings CreateBuilderSettings()
{
return new HostApplicationBuilderSettings();
}

/// <summary>
/// Configures the application using the secified <paramref name="builder"/>.
/// </summary>
/// <param name="builder">The host application builder to configure.</param>
protected virtual void ConfigureApplication(HostApplicationBuilder builder)
{
builder.Logging.SetMinimumLevel(LogLevel.Debug);
builder.Logging.AddMemoryLogger();
}
}
4 changes: 2 additions & 2 deletions src/XUnit.Hosting/TestHostBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
namespace XUnit.Hosting;

/// <summary>
///
/// Base class for hosted unit tests
/// </summary>
/// <typeparam name="TFixture">The type of the fixture.</typeparam>
/// <seealso cref="System.IDisposable" />
public abstract class TestHostBase<TFixture> : IDisposable
where TFixture : TestHostFixture
where TFixture : ITestHostFixture
{
/// <summary>
/// Initializes a new instance of the <see cref="TestHostBase{TFixture}"/> class.
Expand Down
5 changes: 3 additions & 2 deletions src/XUnit.Hosting/TestHostFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace XUnit.Hosting;
/// <summary>
/// XUnit collection fixture that supports <see cref="IHostBuilder"/>
/// </summary>
public abstract class TestHostFixture
[Obsolete("Use TestApplicationFixture instead")]
public abstract class TestHostFixture : ITestHostFixture
{
private readonly IHostBuilder _builder;
private readonly Lazy<IHost> _host;
Expand Down Expand Up @@ -44,7 +45,7 @@ protected TestHostFixture()
/// <value>
/// The host for this test.
/// </value>
protected IHost Host => _host.Value;
public IHost Host => _host.Value;

/// <summary>
/// Gets the services configured for this test
Expand Down
10 changes: 6 additions & 4 deletions test/XUnit.Hosting.Tests/DatabaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

namespace XUnit.Hosting.Tests;

public class DatabaseFixture : TestHostFixture
public class DatabaseFixture : TestApplicationFixture
{
protected override void ConfigureServices(HostBuilderContext context, IServiceCollection collection)
protected override void ConfigureApplication(HostApplicationBuilder builder)
{
collection.AddSingleton<IService, Service>();
collection.AddHostedService<DatabaseInitializer>();
base.ConfigureApplication(builder);

builder.Services.AddSingleton<IService, Service>();
builder.Services.AddHostedService<DatabaseInitializer>();
}
}
10 changes: 5 additions & 5 deletions test/XUnit.Hosting.Tests/XUnit.Hosting.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
Expand All @@ -10,13 +10,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down

0 comments on commit 948c416

Please sign in to comment.