How to run filters on dotnet tests

Rob McCabe picture Rob McCabe · Aug 29, 2018 · Viewed 8.8k times · Source

I have a build in VSTS, as follows:

enter image description here

You can see from the screen shot there's a test step to "Test and generate code coverage". It uses this command:

/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:CoverletOutput=$(Build.SourcesDirectory)\TestResults\Coverage\coverage

Which allows the code coverage report to be generated. I have added "Categories" to my xUnit tests with my defined Trait (such as integration or unit), to allow me to filter tests during build/release. Example would be:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Xunit.Abstractions;
    using Xunit.Sdk;

    /// <summary>
    /// Decorates a test as a Unit Test, so that it runs in Continuous Integration builds.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public sealed class IsUnitAttribute : AICategoryAttribute
    {
        /// <summary>
        /// Initializes a new instance of <see cref="IsUnitAttribute"/>
        /// </summary>
        public IsUnitAttribute() : base("Unit") { }
    }

    /// <summary>
    /// Decorates a test as an Integration Test, so that it runs in Continuous Integration builds.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public sealed class IsIntegrationAttribute : AICategoryAttribute
    {
        /// <summary>
        /// Initializes a new instance of <see cref="IsIntegrationAttribute"/>
        /// </summary>
        public IsIntegrationAttribute() : base("Integration") { }
    }

I only know how to apply a filter in VS-Test step, as follows:

But not when I'm testing using dotnet:

enter image description here

I only know how to build code coverage using dotnet (and not VS-Test)… I want to do both! How do I:

a) Add the commands to the VS-Test to generate the code coverage, the same as I do for dotnet using the command above.

OR

b) Apply the filter to the dotnet Test step?

Any pointers much appreciated!

Answer

Andy Li-MSFT picture Andy Li-MSFT · Aug 30, 2018

There isn't the Test Filter criteria filed for the dotnet test step. However you can try running the dotnet test with the arguments --filter in command line.

Please see Filter option details

Filters out tests in the current project using the given expression. For more information, see the Filter option details section. For more information and examples on how to use selective unit test filtering, see Running selective unit tests.