将参数传递给Azure Devops中的测试

问题描述 投票:1回答:1

我有一个api项目,我想在Azure发布管道中运行一些集成测试。

  1. 建立项目。
  2. 创建发布。
  3. 将发布部署到插槽。
  4. 针对插槽运行NUnit集成测试。这需要对插槽的http请求。
  5. 如果测试通过,则将生产槽与测试的插槽交换。

我坚持第4步。在Visual Studio中将参数传递给测试夹具很容易。

[TestFixture(arguments: "https://urltomyslot.azurewebsites.net")]
public class CachedClientApiTokenManagerTests
{
    public CachedClientApiTokenManagerTests(string authority)
    {
        _authority = authority;
    }

    private readonly string _authority;

    // Runs tests using the url
}

我不知道该怎么做是基于环境从Azure Devops传递参数。我正在使用NUnit3TestAdapter包,它运行正常,但args是关键点。如果我们在实验室环境中执行此操作,则传递的URL与登台或生产URL不同。

我们如何使用args在Azure DevOps中配置它?

azure-devops nunit azure-pipelines
1个回答
1
投票

您可以在变量中定义环境:

enter image description here

然后用这种方式读取C#代码中的变量:

string environment = Environment.GetEnvironmentVariable("environment", EnvironmentVariableTarget.Process);

现在依赖于environment的值创建URL并运行测试。

例如,我创建了一个小型控制台应用程序:

class Program
{
    static void Main(string[] args)
    {
        string environment = Environment.GetEnvironmentVariable("environment", EnvironmentVariableTarget.Process);
        if (environment == "prod")
        {
            Console.WriteLine("We are in production :)");
        }
    }
}

我配置了变量:

enter image description here

我运行.exe文件,在输出中我可以看到We are in production :)打印:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.