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

Viewed 1857

I would like to specify some long-running c# project xUnit tests to only run in azure devops CI pipeline, but not when a click 'Run All' in Visual Studio locally (vs2019). Is there a 'best practice' for this kind of behaviour?

I played around with making a test playlist and running that locally instead of Run All, but that would require updating the list (or lists) everytime i add new tests and this is error-prone.

3 Answers

AzureDevOps defines some environment variables. I would use TF_BUILD in conjunction with Ruben Bartelinks answer.

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.

The Skip property in FactAttribute is overridable - you can make a FactWhenAttribute and have it check an environment variable

Example in 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;
}

Assuming your build pipeline is building using the Release build configuration and not using the Debug build config, you could use the #IF !DEBUG preprocessor directive arround your [Fact] attribute.

For example:

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