Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set environment in integration tests

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?

like image 715
michasaucer Avatar asked Jul 15 '20 09:07

michasaucer


People also ask

What is an integrated test environment?

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.

How do you automate an integration test?

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.

What should integration tests cover?

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.


1 Answers

I had the same issue and following worked out for me:

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
     builder.UseEnvironment("Test");
}
like image 150
hamzeh karbasiyan Avatar answered Oct 18 '22 09:10

hamzeh karbasiyan