Skip to content

Commit c92bb5a

Browse files
authored
Add info for setting environment on IWebHostBuilder (#18313)
1 parent 386ec7f commit c92bb5a

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

aspnetcore/test/integration-tests.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,18 +303,25 @@ For more information on `WebApplicationFactoryClientOptions`, see the [Client op
303303

304304
## Set the environment
305305

306-
By default, the SUT's host and app environment is configured to use the Development environment. To override the SUT's environment:
306+
By default, the SUT's host and app environment is configured to use the Development environment. To override the SUT's environment when using `IHostBuilder`:
307307

308308
* Set the `ASPNETCORE_ENVIRONMENT` environment variable (for example, `Staging`, `Production`, or other custom value, such as `Testing`).
309309
* Override `CreateHostBuilder` in the test app to read environment variables prefixed with `ASPNETCORE`.
310310

311311
```csharp
312-
protected override IHostBuilder CreateHostBuilder() =>
312+
protected override IHostBuilder CreateHostBuilder() =>
313313
base.CreateHostBuilder()
314314
.ConfigureHostConfiguration(
315315
config => config.AddEnvironmentVariables("ASPNETCORE"));
316316
```
317317

318+
If the SUT uses the Web Host (`IWebHostBuilder`), override `CreateWebHostBuilder`:
319+
320+
```csharp
321+
protected override IWebHostBuilder CreateWebHostBuilder() =>
322+
base.CreateWebHostBuilder().UseEnvironment("Testing");
323+
```
324+
318325
## How the test infrastructure infers the app content root path
319326

320327
The `WebApplicationFactory` constructor infers the app [content root](xref:fundamentals/index#content-root) path by searching for a [WebApplicationFactoryContentRootAttribute](/dotnet/api/microsoft.aspnetcore.mvc.testing.webapplicationfactorycontentrootattribute) on the assembly containing the integration tests with a key equal to the `TEntryPoint` assembly `System.Reflection.Assembly.FullName`. In case an attribute with the correct key isn't found, `WebApplicationFactory` falls back to searching for a solution file (*.sln*) and appends the `TEntryPoint` assembly name to the solution directory. The app root directory (the content root path) is used to discover views and content files.

0 commit comments

Comments
 (0)