I want to set environment in my integration tests using WebApplicationFactory
. by defualt, env is set to Development
. Code of my web application factory looks like this:
public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup>
where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseSolutionRelativeContentRoot(AppContext.BaseDirectory);
base.ConfigureWebHost(builder);
}
protected override IWebHostBuilder CreateWebHostBuilder()
{
return WebHost.CreateDefaultBuilder()
.UseStartup<TStartup>()
.UseEnvironment("test"); // i want to launch `test` environment while im
// testing my app
}
}
When i start to debug Startup
class (while i run tests) i still getting Development
environment:
public Startup(IHostEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
// env.EnvironmentName is set to 'Development' while i set it to 'test'
// in my WebApplicationFactory
.AddEnvironmentVariables();
Configuration = builder.Build();
}
How to set environment in WebApplicationFactory
properly? Or maybe how to change strategy for tests only when in startup i depends on appsettings
files?
The Integrated Test Environment (ITE) is our solution to handle complex hardware or software test projects. ITE provides features to define and manage requirements and test cases in different documents, link them with each other as well as plan tests and test campaigns.
Test with the Automated Testing Tool This automated testing process saves you time and provides a repeatable test set that includes happy path and edge case scenarios. To test using the automated testing tool, follow these steps: From the main navigation, go to Inventory > Integration Testing. Click New test.
Integration test cases focus mainly on the interface between the modules, integrated links, data transfer between the modules as modules/components that are already unit tested i.e. the functionality and the other testing aspects have already been covered.
I had the same issue and following worked out for me:
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Test");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With