在构建服务器上运行时跳过某些测试 [英] Skip certain tests while running on the build server

查看:32
本文介绍了在构建服务器上运行时跳过某些测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一些 UI 集成测试无法在构建服务器上运行,因为启动测试 GUI 应用需要以用户身份运行构建代理(而不是当前设置的服务).

We have some UI integration tests that can't be run on the build server as launching test GUI app requires running the build agent as an user (instead of a service which is how it's currently setup).

这会导致构建管道卡住.所以我想在本地而不是在构建服务器上运行这些测试.

This causes the build pipeline to get stuck. So I'd like to run these tests locally but not on the build server.

有没有办法使用 xUnitAzure DevOps 构建管道来实现这一目标?

Is there a way to achieve this using xUnit and Azure DevOps build pipeline?

推荐答案

你当然可以.

在您的 .yml 文件中设置一个环境变量以指示它是否在构建服务器上运行.

Setup an Environment variable to indicate if it's running on the build server in your .yml file.

variables:
- name: IsRunningOnBuildServer
  value: true

现在创建一个自定义事实属性来使用它:

Now create a custom fact attribute to use this:

// This is taken from this SO answer: https://stackoverflow.com/a/4421941/8644294
public class IgnoreOnBuildServerFactAttribute : FactAttribute
{
    public IgnoreOnBuildServerFactAttribute()
    {
        if (IsRunningOnBuildServer())
        {
            Skip = "This integration test is skipped running in the build server as it involves launching an UI which requires build agents to be run as non-service. Run it locally!";
        }
    }
    /// <summary>
    /// Determine if the test is running on build server
    /// </summary>
    /// <returns>True if being executed in Build server, false otherwise.</returns>
    public static bool IsRunningOnBuildServer()
    {
        // "IsRunningOnBuildServer" is setup in build.yml file's variables
        return bool.TryParse(Environment.GetEnvironmentVariable("IsRunningOnBuildServer"), out var buildServerFlag) ? buildServerFlag : false;
    }
}

现在在要跳过在构建服务器上运行的测试方法上使用此 FactAttribute.例如:

Now use this FactAttribute on your test methods that you want to skip running on build server. For eg:

[IgnoreOnBuildServerFact]
public async Task Can_Identify_Some_Behavior_Async()
{
   // Your test code...
}

这篇关于在构建服务器上运行时跳过某些测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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