dotnet / dotnet/aspnetcore

Add additional configuration sources for the WebHostBuilder in the WebApplicationFactory

Open
#4,898 2 comments 0 reactions 0 assignees View on GitHub
area-minimal area-mvc enhancement feature-mvc-testing
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

#### Is this a Bug or Feature request?:
Feature request.

#### Description of the problem:
There are some situations where users have manifested the desire to configure some aspects of the app for testing purposes through environment variables and other mechanisms.

We normally recommend overriding ConfigureWebHostBuilder to do these types of changes, but there are a couple of opinions that the WebApplicationFactory makes about what environment the app is using and where the content root is found from.

In both cases, these pieces of data are defined statically in code.
* In the default setup of the WebApplicationFactory for the environment the tests run on.
* In the WebApplicationFactoryContentRoot attributes for the content roots of the app.

I've discussed this with a bunch of people and we want to switch to a system based on config to allow these things to be configured based on the environment. This way, these settings that the factory needs to correctly boot up the project can be defined when the tests are going to run in addition to at compile time without users customizing the factory.

#### Proposed changes
* Add an additional step to WebApplicationFactory to add additional configuration sources to the webhostbuilder we create.
* Switch from custom implementations for determining the content root and the environment to run on to use config.
* Provide three additional configuration sources in the appropriate order:
* Static config source. Maps our attributes to config keys and defaults the environment to "Development"
* Environment variables provider. Maps environment variables with a prefix ASPNETCOREINTEGRATIONTEST
* Optional json file appSettings.integrationtests.json to allow these things to be specified on a json file.

Any new customization that needs to be added for testing purposes will be done this way.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.