Summary
A set of environment variables defined in Visual Studio 2026’s launchSettings.json were not visible to test code executed under NUnit, causing Environment.GetEnvironmentVariable("MY_VARIABLE") to always return an empty string. The root issue is that NUnit tests do not run under the Visual Studio debug profile, so the environment variables defined there are never applied.
Root Cause
The environment variables configured in:
- Visual Studio → Project Properties → Debug
launchSettings.json→environmentVariables
apply only to F5/debug runs of the application, not to:
- NUnit test runners
- Test Explorer
- dotnet test
- Any external test host process
Because NUnit launches tests in a separate test host process, Visual Studio’s debug profile is completely bypassed.
Why This Happens in Real Systems
This behavior is common because:
- Test runners spawn their own process, ignoring project debug profiles.
launchSettings.jsonis only used by the debugger, not by the build or test pipeline.- Unit test frameworks isolate execution, so they do not inherit environment variables from the IDE.
- Locked-down Windows environments prevent setting machine/user environment variables, making the issue more visible.
Real-World Impact
This leads to:
- Tests failing locally but passing in CI, creating confusion.
- Developers misdiagnosing environment issues as code bugs.
- Inconsistent behavior between running the app and running tests.
- Hard-to-reproduce failures when environment variables differ across machines.
Example or Code (if necessary and relevant)
Below is the code that fails because NUnit does not inherit Visual Studio debug variables:
var value = Environment.GetEnvironmentVariable("MY_VARIABLE") ?? string.Empty;
How Senior Engineers Fix It
Experienced engineers avoid relying on Visual Studio debug profiles for tests. Instead, they:
- Set environment variables inside the test project, using:
RunSettings.runsettingsfilesDirectory.Build.propsTestContextsetup
- Inject configuration via dependency injection, not environment variables.
- Use
.envfiles loaded explicitly in test setup. - Configure environment variables in CI and local test runners consistently.
- Mock configuration providers instead of reading environment variables directly.
Typical fix using .runsettings:
my_value
Why Juniors Miss It
Less experienced engineers often assume:
- Visual Studio settings apply everywhere, including tests.
- Environment variables behave the same in CI and locally.
launchSettings.jsonis a universal configuration file, rather than a debugger-only file.- Test runners inherit the IDE environment, which they do not.
They also may not realize that:
- NUnit runs in a completely separate process, isolated from Visual Studio.
- Locked-down Windows environments hide the underlying issue, making debugging harder.
By understanding how test hosts, debug profiles, and environment variable scopes differ, engineers avoid this class of configuration pitfalls entirely.