🛠️ Visual Studio: Fix a Launch Profile Skipped in Test Debugging
A project’s launchSettings.json profiles (environment variables, command-line arguments, the launch URL) only get applied by the mechanisms that actually read that file — dotnet run and the IDE’s own F5/Start button for the startup project. Test Explorer launches the test host process directly and never opens launchSettings.json at all, so any environment variable set only in a profile (a feature flag, a connection string override, ASPNETCORE_ENVIRONMENT) silently falls back to whatever the machine’s own defaults are the moment a test runs — even though the exact same code behaves correctly when you launch the API project normally.
🔎 The Problem
launchSettings.json profile "Staging":
ASPNETCORE_ENVIRONMENT = Staging
FEATURE_NEW_PRICING = true
F5 the Web API project directly:
IHostEnvironment.EnvironmentName == "Staging" [correct]
Run the same assertion from a test via Test Explorer:
IHostEnvironment.EnvironmentName == "Production" [wrong - env var never set,
test host never read the
profile at all]
✅ Fix: Set the Environment Where the Test Host Actually Reads It
- Set environment variables for test runs in a
.runsettingsfile’s<EnvironmentVariables>section, referenced from Test Explorer’s settings, since that is the file the test host itself reads before it starts. - For integration tests hosted via
WebApplicationFactory, call.UseEnvironment("Staging")directly in the factory’s code, rather than counting onlaunchSettings.jsonto have configured it for you. - Add a one-line assertion at the top of the test suite that logs the resolved environment name, so a silently-wrong fallback shows up immediately instead of surfacing later as a confusing data mismatch.
🚨 Why This Is Dangerous
- A test suite that passes because it quietly fell back to Development settings isn’t testing the Staging-specific code path at all, so a config-dependent bug can pass every pipeline run and only appear once real Staging traffic hits it.
launchSettings.json is a convenience for the IDE’s own launch button, not a config system the test host ever agreed to read.




