Skip to content

Add test case filter based on existence of a property #1724

Closed as not planned
@tillig

Description

@tillig

The documentation on TestCase filtering shows this example:

namespace MSTestNamespace
{
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    [TestClass]
    public class UnitTestClass1
    {
        [Priority(2)]
        [TestMethod]
        public void TestMethod1()
        {
        }

        [TestCategory("CategoryA")]
        [Priority(3)]
        [TestMethod]
        public void TestMethod2()
        {
        }
    }
}

And it shows that you can run all the tests marked with [TestCategory("CategoryA")] using this command:

dotnet test --filter TestCategory=CategoryA

However, if there doesn't seem to be a way to run tests that are not marked with any test category.

Same for XUnit:

namespace XUnitNamespace
{
    public class TestClass1
    {
        [Fact]
        public void foo()
        {
        }

        [Trait("Category", "Nightly")]
        [Trait("Priority", "2")]
        [Fact]
        public void bar()
        {
        }
    }
}

I can run the test with a Category=Nightly... but I can't run choose to run tests with no Trait "Category".

I've tried these and failed:

dotnet test --filter Category=''
dotnet test --filter Category=null
dotnet test --filter Category
dotnet test --filter !Category
dotnet test --filter Category!~''

(Some of these come up with TestCaseFilter errors where they aren't considered valid expressions.)

My logic being the value of a non-existent property should be empty/null... but diving a little deeper into the filter code that doesn't seem to be the case. There doesn't seem to be a way to filter based on a property being/not being there.

I might propose one of the following solutions:

  • Properties that don't exist might equal empty string, so Category='' would succeed.
  • Add a $null value so Category=$null would succeed.
  • Allow a property name without a value to indicate existence so !Category would work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions