asp.net – Wrong Appsettings.json picked at build with Unit test project referencing another project with its own appsettings


With Visual Studio 2022 in C#, we are referencing from our Unit tests project another project which is a WebApplication.

The web application is tested using Microsoft’s WebApplicationFactory ASP.Net feature, hence the direct reference of the web project in the unit test project.

This works very well, but we randomly encounter a problem :

Both projects have an appsettings.json file.
Sometimes , while executing the unit tests (with MSTest or with Resharper), the build picks the wrong appsettings.json (the one of the referenced project instead of the one of the unit test project.).
Putting the appsetting file of the TestProject in “Copy always” does not fix the problem.

When the problem occurs, a “quick dirty manual fix” is to manually change anything (like a whitespace) in the Test project appsettings.json to make it newer than the one of the WebApp, by rebuilding VS now takes the proper appsettings.json.

I would like to know how to fix this ?

Having appsettings with different file names or managing environments is not an easy option, because it would lead to changes in one or both or the projects.

Instead, I would prefer a solution purely in build (like a build script in test project build) or VS configuration (.runsettings, something like this), or a solution involving the least possible changes in projects.

Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *