如何 运行 一些测试只在 azure devops CI env 而不是本地

How to run some tests only in azure devops CI env but not locally

我想在 azure devops CI 管道中指定一些长 运行ning c# 项目 xUnit 测试仅 运行,但在单击 'Run All' 时不会在 Visual Studio 本地 (vs2019)。这种行为有'best practice'吗?

我试着制作了一个测试播放列表,然后 运行 在本地而不是 Run All 设置它,但是每次我添加新测试时都需要更新列表(或多个列表),这是错误的-容易。

假设您的构建管道正在使用 Release 构建配置构建而不是使用 Debug 构建配置,您可以在 [Fact] 周围使用 #IF !DEBUG 预处理器指令属性。

例如:

#if !DEBUG
[Fact]
#endif
public void YourTestMethod()
{
}

FactAttribute 中的 Skip 属性 是可覆盖的 - 您可以制作 FactWhenAttribute 并让它检查环境变量

this post from @Joseph Woodward

中的示例
public sealed class IgnoreOnAppVeyorLinuxFact : FactAttribute
{
    public IgnoreOnAppVeyorLinuxFact() {
        if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && IsAppVeyor()) {
            Skip = "Ignore on Linux when run via AppVeyor";
        }
    }

    private static bool IsAppVeyor()
        => Environment.GetEnvironmentVariable("APPVEYOR") != null;
}

AzureDevOps 定义了一些 environment variables。我会结合使用 TF_BUILD 和 Ruben Bartelinks 的回答。

Set to True if the script is being run by a build task.

This variable is agent-scoped. It can be used as an environment variable in a script and as a parameter in a build task, but not as part of the build number or as a version control tag.