How to run filters on dotnet tests
Asked Answered
G

1

7

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!

Greg answered 29/8, 2018 at 14:27 Comment(0)
V
9

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.

Vicinity answered 30/8, 2018 at 11:14 Comment(5)
Using the command --filter seems to work but when I try a command that worked in VSTest (filter=Unit), it doesn't find my unit tests :-\ any idea why that would be? Should the command that follows --filter be different than "=Unit"? I also tried "=IsUnit" but no joy.Greg
@RobMcCabe Well, they have different syntax, please see Filter option details for dotnet test. And here is an example to use the --filter in dotnet test for your reference: DotNet Test With NUnitVicinity
For anyone else wanting to filter on Category name in Xunit, this was the argument that worked for me: --filter "Category=Unit"Greg
This worked for me to filter on one [TestMethod]. Just use the class name like so: dotnet test --filter "CanPost_Add_New_Card_Date"Fourteenth
The "DotNet Test With NUnit" link now seems to be dead. Any alternative recommendations?Tigress

© 2022 - 2024 — McMap. All rights reserved.