【问题标题】:How to use environment variables in unit tests (.net core)如何在单元测试中使用环境变量(.net core)
【发布时间】:2019-08-05 13:01:20
【问题描述】:

我有一个我正在尝试测试的方法,它使用我的“local.settings.json”中的环境变量

private static string _environmentVar = Environment.GetEnvironmentVariable("envirnomentVarConfig");

public string MyMethod()
{
    var result = DoStuff(_environmentVar)
    return result;  
}

在我的测试中,我调用了这个方法,在调试时,我可以看到 _environmentVar 为空。

我需要在测试中设置 envirnomentVarConfig 吗?如果有怎么办?

【问题讨论】:

  • 为什么你认为 local.settings.json 会被读取并填充到环境变量中?
  • 我希望它能够读取它,因为当测试调用该方法时,GetEnvironmentVariable 在该方法之前被命中,这让我相信它会从设置 json 中获取该变量。显然不是
  • 我只是好奇为什么您认为将某些内容放入 .json 文件会导致它成为环境变量。这不是不可能的——你可以有一些你没有向我们展示的代码来做到这一点。但它不会开箱即用。如果您需要从文件中读取配置,则需要使用专门的类来执行此操作。 Environment.GetEnvironmentVariable 不这样做。
  • 正常运行代码就可以了?
  • 您的意思是在运行应用程序时不是作为单元测试?你那里有额外的代码来读取配置吗?也许在 Startup.cs 文件中?

标签: c# .net asp.net-core .net-core mstest


【解决方案1】:

通过在测试中设置变量来解决这个问题:

Environment.SetEnvironmentVariable("environmentVarConfig", "environmentVarValue");

【讨论】:

  • 请注意:可能不适用于需要重启的机器范围变量
  • 这似乎对我不起作用(使用 XUnit + .net5),所以我制作了一个包装类和接口,然后我对其进行了模拟......
【解决方案2】:

您可以在.runsettings 文件中specify environment variables

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
    <RunConfiguration>
        <EnvironmentVariables>
            <YOUR_VARIABLE>Value for your variable</YOUR_VARIABLE>
            <SOME_OTHER_VARIABLE>With another Value</SOME_OTHER_VARIABLE>
        </EnvironmentVariables>
    </RunConfiguration>
</RunSettings>

您也可以implement DataCollector 通过ITestExecutionEnvironmentSpecifier 提供环境变量

// Add a reference to nuget package `Microsoft.TestPlatform.ObjectModel`
// The assembly name must end with `Collector` (i.e. match `*collector.dll`)

[DataCollectorFriendlyName("my own example collector")]
[DataCollectorTypeUri("datacollector://myown/examplecollector/1.0")]
public class MyDataCollector : DataCollector, ITestExecutionEnvironmentSpecifier
{
    public override void Initialize(
        XmlElement configurationElement,
        DataCollectionEvents events,
        DataCollectionSink dataSink,
        DataCollectionLogger logger,
        DataCollectionEnvironmentContext environmentContext)
    {
        // inspect configurationElement for your custom settings
    }

    public IEnumerable<KeyValuePair<string, string>> GetTestExecutionEnvironmentVariables()
    {
        return new Dictionary<string, string>
        {
            ["YOUR_VARIABLE"] = "your value",
        };
    }
}

您还可以通过.runsettings 文件配置您的数据收集器:

<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
    <RunConfiguration>
        <TestAdaptersPaths>path/where/to/find/your/collector</TestAdaptersPaths>
    </RunConfiguration>
    <DataCollectionRunSettings>
        <DataCollectors>
            <DataCollector friendlyName="my own example collector" uri="datacollector://myown/examplecollector/1.0">
                <Configuration>
                    <SomeSettingHere/>
                </Configuration>
            </DataCollector>
        </DataCollectors>
    </DataCollectionRunSettings>
</RunSettings>

【讨论】:

  • 我在项目根目录中添加了.runsettings,而不是在解决方案中。直到我手动指出文件的位置,它才起作用。 Test explorer &gt; settings (cog icon) &gt; Configure run settings &gt; Select solution wide .runsettings file.
  • 我链接的文档提供了 various ways 如何设置 VS 以获取 .runsettings 文件
猜你喜欢
  • 1970-01-01
  • 2019-04-30
  • 2017-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-16
相关资源
最近更新 更多