-
-
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 support for HostApplicationBuilder
- Loading branch information
Showing
7 changed files
with
105 additions
and
15 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
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; } | ||
} |
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,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(); | ||
} | ||
} |
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