WebApplicationFactory<TEntryPoint> WebApplicationFactory<TEntryPoint> WebApplicationFactory<TEntryPoint> Class


Factory for bootstrapping an application in memory for functional end to end tests.

public class WebApplicationFactory<TEntryPoint> : IDisposable where TEntryPoint : class
type WebApplicationFactory<'EntryPoint (requires 'EntryPoint : null)> = class
    interface IDisposable
Public Class WebApplicationFactory(Of TEntryPoint)
Implements IDisposable

Type Parameters


A type in the entry point assembly of the application. Typically the Startup or Program classes can be used.



WebApplicationFactory<TEntryPoint>() WebApplicationFactory<TEntryPoint>() WebApplicationFactory<TEntryPoint>()

Creates an instance of WebApplicationFactory<TEntryPoint>. This factory can be used to create a TestServer instance using the MVC application defined by TEntryPoint and one or more HttpClient instances used to send HttpRequestMessage to the TestServer. The WebApplicationFactory<TEntryPoint> will find the entry point class of TEntryPoint assembly and initialize the application by calling IWebHostBuilder CreateWebHostBuilder(string [] args) on TEntryPoint.

This constructor will infer the application content root path by searching for a WebApplicationFactoryContentRootAttribute on the assembly containing the functional tests with a key equal to the TEntryPoint assembly FullName. In case an attribute with the right key can't be found, WebApplicationFactory<TEntryPoint> will fall back to searching for a solution file (*.sln) and then appending TEntryPoint assembly name to the solution directory. The application root directory will be used to discover views and content files.

The application assemblies will be loaded from the dependency context of the assembly containing TEntryPoint. This means that project dependencies of the assembly containing TEntryPoint will be loaded as application assemblies.


ClientOptions ClientOptions ClientOptions

Gets the WebApplicationFactoryClientOptions used by CreateClient().

Factories Factories Factories

Gets the IReadOnlyList<T> of factories created from this factory by further customizing the IWebHostBuilder when calling WithWebHostBuilder(Action<IWebHostBuilder>).

Server Server Server

Gets the TestServer created by this WebApplicationFactory<TEntryPoint>.


ConfigureClient(HttpClient) ConfigureClient(HttpClient) ConfigureClient(HttpClient)

Configures HttpClient instances created by this WebApplicationFactory<TEntryPoint>.

ConfigureWebHost(IWebHostBuilder) ConfigureWebHost(IWebHostBuilder) ConfigureWebHost(IWebHostBuilder)

Gives a fixture an opportunity to configure the application before it gets built.

CreateClient() CreateClient() CreateClient()

Creates an instance of HttpClient that automatically follows redirects and handles cookies.

CreateClient(WebApplicationFactoryClientOptions) CreateClient(WebApplicationFactoryClientOptions) CreateClient(WebApplicationFactoryClientOptions)

Creates an instance of HttpClient that automatically follows redirects and handles cookies.

CreateDefaultClient(DelegatingHandler[]) CreateDefaultClient(DelegatingHandler[]) CreateDefaultClient(DelegatingHandler[])

Creates a new instance of an HttpClient that can be used to send HttpRequestMessage to the server. The base address of the HttpClient instance will be set to http://localhost.

CreateDefaultClient(Uri, DelegatingHandler[]) CreateDefaultClient(Uri, DelegatingHandler[]) CreateDefaultClient(Uri, DelegatingHandler[])

Creates a new instance of an HttpClient that can be used to send HttpRequestMessage to the server.

CreateServer(IWebHostBuilder) CreateServer(IWebHostBuilder) CreateServer(IWebHostBuilder)

Creates the TestServer with the bootstrapped application in builder.

CreateWebHostBuilder() CreateWebHostBuilder() CreateWebHostBuilder()

Creates a IWebHostBuilder used to set up TestServer.

Dispose() Dispose() Dispose()
Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

Finalize() Finalize() Finalize()

Finalizes an instance of the WebApplicationFactory<TEntryPoint> class.

GetTestAssemblies() GetTestAssemblies() GetTestAssemblies()

Gets the assemblies containing the functional tests. The WebApplicationFactoryContentRootAttribute applied to these assemblies defines the content root to use for the given TEntryPoint.

WithWebHostBuilder(Action<IWebHostBuilder>) WithWebHostBuilder(Action<IWebHostBuilder>) WithWebHostBuilder(Action<IWebHostBuilder>)

Creates a new WebApplicationFactory<TEntryPoint> with a IWebHostBuilder that is further customized by configuration.

Applies to