Skip to content

Commit 3d3f5ad

Browse files
authored
Integration testing 2.x set env update (#18254)
1 parent c92bb5a commit 3d3f5ad

1 file changed

Lines changed: 31 additions & 6 deletions

File tree

aspnetcore/test/integration-tests.md

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how integration tests ensure that an app's components functio
55
monikerRange: '>= aspnetcore-2.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 01/06/2019
8+
ms.date: 05/11/2020
99
no-loc: [Blazor, "Identity", "Let's Encrypt", Razor, SignalR]
1010
uid: test/integration-tests
1111
---
@@ -667,13 +667,38 @@ For more information on `WebApplicationFactoryClientOptions`, see the [Client op
667667
By default, the SUT's host and app environment is configured to use the Development environment. To override the SUT's environment:
668668

669669
* Set the `ASPNETCORE_ENVIRONMENT` environment variable (for example, `Staging`, `Production`, or other custom value, such as `Testing`).
670-
* Override `CreateHostBuilder` in the test app to read environment variables prefixed with `ASPNETCORE`.
670+
* Override `CreateWebHostBuilder` in the test app to read the `ASPNETCORE_ENVIRONMENT` environment variable.
671671

672672
```csharp
673-
protected override IHostBuilder CreateHostBuilder() =>
674-
base.CreateHostBuilder()
675-
.ConfigureHostConfiguration(
676-
config => config.AddEnvironmentVariables("ASPNETCORE"));
673+
public class CustomWebApplicationFactory<TStartup>
674+
: WebApplicationFactory<TStartup> where TStartup: class
675+
{
676+
protected override IWebHostBuilder CreateWebHostBuilder()
677+
{
678+
return base.CreateWebHostBuilder()
679+
.UseEnvironment(
680+
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
681+
}
682+
683+
...
684+
}
685+
```
686+
687+
The environment can also be set directly on the host builder in a custom <xref:Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory%601>:
688+
689+
```csharp
690+
public class CustomWebApplicationFactory<TStartup>
691+
: WebApplicationFactory<TStartup> where TStartup: class
692+
{
693+
protected override void ConfigureWebHost(IWebHostBuilder builder)
694+
{
695+
builder.UseEnvironment(
696+
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"));
697+
698+
...
699+
}
700+
701+
...
677702
```
678703

679704
## How the test infrastructure infers the app content root path

0 commit comments

Comments
 (0)