GetEnvironmentVariable 应该在 xUnit 测试中工作吗? [英] Should GetEnvironmentVariable Work in xUnit Test?

查看:33
本文介绍了GetEnvironmentVariable 应该在 xUnit 测试中工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用项目属性页面在 Visual Studio 2017 中为 .Net Core Web 项目设置环境变量,我可以使用 Environment.GetEnvironmentVariable 读取变量的值;但是,当我为 xUnit 测试项目设置环境变量然后调试测试时,Environment.GetEnvironmentVariable 总是返回 null.它是一个测试项目,是否应该防止变量与 Web 项目一样被使用?如果是这样,有没有办法为测试项目设置环境变量?谢谢.

If I set environment variables for a .Net Core web project in Visual Studio 2017 using the project properties page, I can read the value of the variable using Environment.GetEnvironmentVariable; however, when I set the environment variable for my xUnit testing project and then debug the test, Environment.GetEnvironmentVariable always returns null. Is there something about the fact that it is a testing project that should prevent the variable from being used the same as with the web project? If so, is there a way that I can set the environment variables for a test project? Thank you.

推荐答案

GetEnvironmentVariable 在 xUnit 测试中运行良好.问题是正确设置变量.如果您在 Properties -> 中设置变量Debug 页面,然后将变量写入 PropertieslaunchSettings.json 并且 Visual Studio 使用所选配置文件启动应用程序.如您所见,launchSettings.json 甚至不会默认复制到输出文件夹.不可能将此文件作为参数传递给 dotnet rundotnet test,如果测试在 CI 服务器上自动运行,则会导致明显的问题.因此,launchSettings.json 不被测试运行者考虑也就不足为奇了.

The GetEnvironmentVariable works fine in xUnit tests. The problem is to properly set a variable. If you set the variable at Properties -> Debug page, then the variable is written to PropertieslaunchSettings.json and Visual Studio makes all work to launch an application with the selected profile. As you could see, launchSettings.json even isn't copied to output folder by default. It's impossible to pass this file as argument to dotnet run or dotnet test, that leads to obvious problem if tests are runned automatically on a CI server. So it is not surprising that launchSettings.json isn't considered by a test runner.

解决方案:有很多方法可以在 xUnit 中设置测试环境:

Solution: there are a lot of ways to setup a test environment in xUnit:

例如,这个集合夹具设置了 launchSettings.json 中的所有环境变量:

For example, this collection fixture sets up all environment variables from launchSettings.json:

public class LaunchSettingsFixture : IDisposable
{
    public LaunchSettingsFixture()
    {
        using (var file = File.OpenText("Properties\launchSettings.json"))
        {
            var reader = new JsonTextReader(file);
            var jObject = JObject.Load(reader);

            var variables = jObject
                .GetValue("profiles")
                //select a proper profile here
                .SelectMany(profiles => profiles.Children())
                .SelectMany(profile => profile.Children<JProperty>())
                .Where(prop => prop.Name == "environmentVariables")
                .SelectMany(prop => prop.Value.Children<JProperty>())
                .ToList();

            foreach (var variable in variables)
            {
                Environment.SetEnvironmentVariable(variable.Name, variable.Value.ToString());
            }
        }
    }

    public void Dispose()
    {
        // ... clean up
    }
}

launchSettings.json 设置 Copy to output directory: Always 以使文件可从测试中访问.

Set Copy to output directory: Always for launchSettings.json to make the file accessible from tests.

这篇关于GetEnvironmentVariable 应该在 xUnit 测试中工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆