在生成服务器上运行时跳过单元测试 [英] Skip unit tests while running on the build server

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

问题描述

我们有一些无法在生成服务器上运行的UI集成测试,因为启动测试GUI应用程序需要以用户身份运行生成代理(而不是当前安装的服务)。

这会导致构建管道停滞。因此,我希望在本地运行这些测试,而不是在构建服务器上运行。

是否可以使用xUnitMSTestAzure DevOps生成管道来实现此目的?

推荐答案

您当然可以。

设置一个环境变量,以指示它是否在Build.yml文件中的生成服务器上运行。

variables:
- name: IsRunningOnBuildServer
  value: true

答案1:使用xUnit

现在创建自定义事实属性以使用该属性:

// 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()
    {
        return bool.TryParse(Environment.GetEnvironmentVariable("IsRunningOnBuildServer"), out var buildServerFlag) ? buildServerFlag : false;
    }
}
现在,在您希望跳过在构建服务器上运行的测试方法上使用FactAttribute。例如:

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

答案2:使用MSTest

创建自定义测试方法属性以覆盖Execute方法:

public class SkipTestOnBuildServerAttribute : TestMethodAttribute
{
    public override TestResult[] Execute(ITestMethod testMethod)
    {
        if (!IsRunningOnBuildServer())
        {
            return base.Execute(testMethod);
        }
        else
        {
            return new TestResult[] { new TestResult { Outcome = UnitTestOutcome.Inconclusive } };
        }
    }

    public static bool IsRunningOnBuildServer()
    {
        return bool.TryParse(Environment.GetEnvironmentVariable("IsRunningOnBuildServer"), out var buildServerFlag) ? buildServerFlag : false;
    }
}
现在,在您希望跳过在构建服务器上运行的测试方法上使用TestMethodAttribute。例如:

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

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

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