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

时间:2019-04-29 17:42:04

标签: azure-devops nunit 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
}

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

我们如何在带有参数的Azure DevOps中配置它?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

您可以配置运行设置文件并根据环境覆盖测试参数。

运行设置文件:

 <TestRunParameters>
    <Parameter name="ApplicationUrl" value="https://qa.environment.url" /> 
 </TestRunParameters>

您可以访问测试运行参数,例如:

string applicationUrl = TestContext.Parameters["ApplicationUrl"];

如何在管道中的VsTest任务中覆盖参数:

enter image description here

相关问题