question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to load test appsettings in integration tests

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I want to do some condition checks on the Program file (AKA Startup). That condition is dependent on some configuration. I put that configuration in appsettings.josn. In the test project I used different appsettings configurations. But unable to get the value of test configuration.

Expected Behavior

Need to bind the appsettings.json of test project.

Steps To Reproduce

Create a ASP Net Core Web API project selecting .net 6. Uncheck minimal API.

Put this to the program file.

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();

var useTests = builder.Configuration.GetValue<bool>("UseTests"); // not binding from test appsettings
if (useTests)
{

}
var app = builder.Build();

app.MapControllers();

app.Run();

public partial class Startup{}

Here is the custom web application factory:

public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
    {
        protected override void ConfigureWebHost(IWebHostBuilder builder)
        {
            builder.ConfigureAppConfiguration(config =>
            {
                var projectDir = Directory.GetCurrentDirectory();
                var configPath = Path.Combine(projectDir, "appsettings.json");
                config.AddJsonFile(configPath);
            });
        }
    }

Write a test:

public class UnitTest1 : IClassFixture<CustomWebApplicationFactory<Startup>>
    {
        private readonly CustomWebApplicationFactory<Startup> _factory;
        private readonly HttpClient _client;
        public UnitTest1(CustomWebApplicationFactory<Startup> factory)
        {
            _factory = factory;
        }
        [Fact]
        public void Test1()
        {
            var application = new CustomWebApplicationFactory<Startup>();
            var client = _factory.CreateClient();
            var result = client.GetAsync("WeatherForecast");
            Assert.Equal(true, true);
        }
    }

Exceptions (if any)

No response

.NET Version

6.0.100

Anything else?

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
msftbot[bot]commented, Mar 15, 2022

Thanks for contacting us. We’re moving this issue to the .NET 7 Planning milestone for future evaluation / consideration. Because it’s not immediately obvious that this is a bug in our framework, we would like to keep this around to collect more feedback, which can later help us determine the impact of it. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it’s very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

0reactions
msftbot[bot]commented, Mar 18, 2023

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AppSettings.json for Integration Test in ASP.NET Core
json file in the test project itself) so the test fails at creating the TestServer . I tried getting around this by modifying...
Read more >
Using custom appsettings.json with ASP.NET Core ...
We have to tweak our test class to make it include correct appsettings.json. As TestServer is run in output folder of integration tests...
Read more >
Integration tests in ASP.NET Core
NET Core supports integration tests using a unit test framework with a test web host and an in-memory test server. This article assumes...
Read more >
Overriding configuration in ASP.NET Core integration tests
This post gives an overview of the various ways to override configuration values in ASP.NET Core integration tests.
Read more >
Using xUnit to Test your C# Code
How to create unit tests and integration tests with xUnit for your C# applications.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found