Search code examples
c#azure-devopsxunit.net

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


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.


Solution

  • 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;
    }