Integration Tests Settings File Conflict (Appsettings.json)


One of the biggest issues when testing our .NET Core Web api apps is the conflict of appsettings.json files between the main project and the testing project.

One of the solution is to copy always the appsettings files belonging to the test project. But this solution is not that perfect. When forgetting to do so, the test engine behaves strangely.

One of the solution I have adopted is pretty straightforward ! Use a different name for the settings json and use this file when doings tests. That’s pretty much it.

Took a look at the code snippet below when creating an instance of “TestServer" :

   protected TestServer CreateServer<TSTARTUP>()
            where TSTARTUP : class
            return new TestServer(new WebHostBuilder()

and when creating the configuration instance :

  var builder = new ConfigurationBuilder()
                .AddJsonFile(configPath, optional: true, reloadOnChange: true);
            Configuration = builder.Build();


Enjoy !

Add comment