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

Dar*_*g8r 5 nunit azure-devops azure-pipelines

我有一个 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
}
Run Code Online (Sandbox Code Playgroud)

我不知道该怎么做是根据环境从 Azure Devops 传递参数。我正在使用 NUnit3TestAdapter 包并且它运行良好,但 args 是症结所在。如果我们在实验室环境中执行此操作,则传递的 url 与暂存或生产 url 不同。

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

Sha*_*zyk 7

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

在此处输入图片说明

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

string environment = Environment.GetEnvironmentVariable("environment", EnvironmentVariableTarget.Process);
Run Code Online (Sandbox Code Playgroud)

现在取决于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 :)");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我配置了变量:

在此处输入图片说明

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

在此处输入图片说明