内存中集成测试和环境变量——.net core

In-memory integration tests and Environment Variables -- .net core

我们可以执行测试服务器进行集成测试。类似于:

很好。

但是,我无法将环境变量传递给测试服务器。 我可以使用 UseEnvironment("Development") 设置环境但不能添加变量。 应该可以,就是不知道怎么弄。

有人知道如何做到这一点吗?


环境变量将为我们的代码库需要的第 3 方 API 进行开发登录。 对于单元测试,我们模拟这个第 3 方 API。 针对我的特定问题的解决方案也可以是为集成测试模拟它,从而消除对环境变量的需求,但即便如此问题仍然存在。

您可以通过编程方式设置环境变量。

System.Environment.SetEnvironmentVariable("key", "value");

MSDN 写道 SetEnvironmentVariable(string, string)...

Creates, modifies, or deletes an environment variable stored in the current process... Because the environment variable is defined in the environment block of the current process only, it does not persist after the process has ended.