GetEnvironmentVariable是否应该在xUnit Test中工作? [英] Should GetEnvironmentVariable Work in xUnit Test?

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

问题描述

如果使用项目属性页在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->调试页面,然后将变量写入 Properties\launchSettings.json ,Visual Studio进行所有工作以使用选定的配置文件启动应用程序。如您所见,默认情况下甚至不会将 launchSettings.json 复制到输出文件夹。无法将此文件作为 dotnet运行 dotnet测试的参数传递,如果运行测试,则会导致明显的问题自动在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 Properties\launchSettings.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:

  • Constructor
  • Base class
  • Fixture

例如,此集合夹具从 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 ,以使文件可从测试中访问。

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

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

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