Namespace Codebelt.Extensions.Xunit.Hosting
Exercise a real .NET application entry point, hosted service, or dependency-injection graph without rebuilding its host setup inside the test project. The Codebelt.Extensions.Xunit.Hosting namespace applies the same test model to console apps, workers, and Generic Host applications: bootstrap the application's Program assembly, customize the host for the scenario, inspect configuration and services, and dispose the test host through one IHostTest abstraction.
For a focused test, start with ApplicationTestFactory.Create<TEntryPoint>. It is the non-web counterpart to the entry-point pattern commonly associated with ASP.NET Core's WebApplicationFactory<TEntryPoint>: the generic argument identifies the application assembly, while the returned test context exposes the resulting host, configuration, and environment. Use the fixture/base-class form when several xUnit tests should share that application context.
Availability: .NET 10, .NET 9 and .NET Standard 2.0
Complements: xUnit: Shared Context between Tests 🔗
Choose a Hosting Path
| When you need to | Start with | Why |
|---|---|---|
| Bootstrap an existing console, worker, or Generic Host application for one test | ApplicationTestFactory.Create<TEntryPoint> |
Runs the application's entry-point setup and returns an owned IHostTest context that the caller disposes. |
| Share an existing application across an xUnit test class | ApplicationTest<TEntryPoint, TFixture> with BlockingManagedApplicationFixture<TEntryPoint> |
Moves application startup and disposal into xUnit's fixture lifecycle while retaining configuration and service access. |
| Build a conventional Generic Host entirely inside the test | HostTestFactory |
Configures IServiceCollection and IHostBuilder directly without requiring an application entry point. |
Build with the modern IHostApplicationBuilder model |
MinimalHostTestFactory |
Keeps minimal-host tests focused on services and application-builder configuration. |
| Configure the host now but decide when it starts | A SelfManaged fixture |
Leaves startup under test control so observers and pre-start assertions can be attached first. |
Fixture Naming Convention
Host fixtures follow a lifecycle naming convention:
| Prefix | Convention |
|---|---|
Managed |
The fixture owns host creation, configuration, startup and disposal using the default host runner. |
SelfManaged |
The fixture owns host creation and configuration, but leaves host startup to the test. |
BlockingManaged |
The fixture owns the host lifecycle and starts the host synchronously before returning control to the test. |
Application-entry-point fixtures use the BlockingManaged prefix by default. Existing application entry points are discovered and built from their Program assembly, so tests receive a ready host after fixture initialization. Use BlockingManagedApplicationFixture<TEntryPoint> when testing a console, worker, or Generic Host application from an existing entry point.
Extension Members
| Type | Ext | Methods |
|---|---|---|
| IApplicationFixture<TEntryPoint> | ⬇️ | HasValidState<TEntryPoint> |
| IGenericHostFixture | ⬇️ | HasValidState |
| ILogger | ⬇️ | GetTestStore |
| ILogger<T> | ⬇️ | GetTestStore<T> |
| IMinimalHostFixture | ⬇️ | HasValidState |
| IServiceCollection | ⬇️ | AddXunitTestLogging · AddXunitTestLoggingOutputHelperAccessor · AddXunitTestLoggingOutputHelperAccessor<T> |
| IServiceProvider | ⬇️ | GetRequiredScopedService<T> |
Classes
- ApplicationFixtureExtensions
Extension methods for the IApplicationFixture<TEntryPoint> interface.
- ApplicationHostFactory
Provides factory methods for creating application hosts from an entry point assembly.
- ApplicationTestFactory
Provides a set of static methods for IHost testing that bootstraps an existing .NET application entry point.
- ApplicationTest<TEntryPoint, T>
Represents a base class from which all implementations of unit testing, that uses Microsoft Dependency Injection and depends on an existing .NET application entry point, should derive.
- BlockingManagedApplicationFixture<TEntryPoint>
Provides a blocking managed implementation of the IApplicationFixture<TEntryPoint> interface.
- GenericHostFixtureExtensions
Extension methods for the IGenericHostFixture interface.
- HostFixture
Represents the base class from which all implementations of xUnit fixture concept should derive.
- HostTest
Represents the non-generic base class from where its generic equivalent should derive.
- HostTestFactory
Provides a set of static methods for IHost unit testing.
- HostTest<T>
Represents a base class from which all implementations of unit testing, that uses Microsoft Dependency Injection, should derive.
- LoggerExtensions
Extension methods for the ILogger<TCategoryName> interface.
- ManagedHostFixture
Provides a default implementation of the IGenericHostFixture interface.
- ManagedMinimalHostFixture
Provides a default implementation of the IMinimalHostFixture interface.
- MinimalHostFixtureExtensions
Extension methods for the IMinimalHostFixture interface.
- MinimalHostTest
Represents the non-generic base class from where its generic equivalent should derive.
- MinimalHostTestFactory
Provides a set of static methods for IHost unit testing (minimal style).
- MinimalHostTest<T>
Represents a base class from which all implementations of unit testing, that uses Microsoft Dependency Injection (minimal style), should derive.
- SelfManagedHostFixture
Represents a self-managed implementation of the ManagedHostFixture class.
- SelfManagedMinimalHostFixture
Represents a self-managed implementation of the ManagedMinimalHostFixture class.
- ServiceCollectionExtensions
Extension methods for the IServiceCollection interface.
- ServiceProviderExtensions
Provides extension methods for the IServiceProvider interface.
- XunitTestLoggerEntry
Represents a captured log-entry for testing purposes. This record encapsulates the LogLevel, EventId and message.
Interfaces
- IApplicationFixture<TEntryPoint>
Provides a way to use Microsoft Dependency Injection in tests that bootstrap an existing .NET application entry point.
- IConfigurationTest
Represents the members needed for DI testing with support for Configuration.
- IEnvironmentTest
Represents the members needed for DI testing with support for IHostEnvironment.
- IGenericHostFixture
Provides a way to use Microsoft Dependency Injection in unit tests.
- IHostFixture
Provides a way to support app and lifetime management in unit tests.
- IMinimalHostFixture
Provides a way to use Microsoft Dependency Injection in unit tests (minimal style).