Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
.Net Core 5 Web Api - Xunit not reading my appsettings.Development

I'm starting to build tests from my api and for that I'm using xunit, but it's not reading my appsettings.Development.json, what's wrong?

namespace CorporateMembership.Test.Integration
{
    public class ContestTest
    {
        private readonly HttpClient _httpClient;

        public ContestTest()
        {
            var server = new TestServer(new WebHostBuilder()
                .UseEnvironment("Development")
                .UseStartup<Startup>());

            _httpClient = server.CreateClient();
        }...
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

By default when you create TestServer it doesn't build configuration and you need to build configuration and pass it to the test server.

public class ContestTest
{
    private readonly HttpClient _httpClient;

    public ContestTest()
    {
        var environment = "Development";
        var directory = Directory.GetCurrentDirectory();
        var configurationBuilder = new ConfigurationBuilder()
            .SetBasePath(directory)
            .AddJsonFile("appsettings.json")
            .AddJsonFile($"appsettings.{environment}.json");

        var server = new TestServer(new WebHostBuilder()
            .UseEnvironment(environment)
            .UseConfiguration(configurationBuilder.Build())
            .UseStartup<Startup>());

        _httpClient = server.CreateClient();
    }
}
over 4 years ago · Santiago Trujillo Report

0

Your test project is likely built into a different directory than your main project, so your appsettings.Development.json file is not there.

Load your settings with IConfigurationBuilder in your Startup. You can specify a relative path there through AddJsonFile method.

Also do you really need to start a server in your unit test? You test class functionality, which rarely needs a running server. In order to inject test settings into a class test you could use the options pattern and than in your test provide dummy settings via Options.Create(new MyDummySettings()).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!