Skip to content

--treenode-filter pattern matching is at least non obvious, potentially errored #7300

Closed as not planned

Description

Describe the bug

I wanted to run two tests at once, easy enough assumed the OR operator was straightforward but couldn't get it to work.

In short the following filter patterns don't work for selecting two tests (named MyTest1/MyTest2) (and the first one doesn't even work selecting just the one):

/*/*/*/(MyTest1)
/*/*/*/(MyTest1)|(MyTest2)
/*/*/*/(MyTest1|MyTest2)
(/*/*/*/MyTest1)|(/*/*/*/MyTest2) # this one crashes as well

These all work to select both, even though some are a bit non-obvious to me as to why:

/*/*/*/(MyTest1)|(MyTest2*)
/*/*/*/(MyTest1*)|(MyTest2)
/*/*/*/(MyTest1*)|(MyTest2*)
/*/*/*/(MyTest1*)|(MyTest2*)
/*/*/*/(MyTest1*|MyTest2)
/*/*/*/(MyTest1*|MyTest2*)
/*/*/*/MyTest*
/*/*/*/(MyTest1)|(*MyTest2)
/*/*/*/(*MyTest1)|(MyTest2)
/*/*/*/*(MyTest1)|(MyTest2)
/*/*/*/(MyTest1*)|MyTest2
/*/*/*/(MyTest1)|MyTest2*

Here is a short powershell script. I tried to repro without tunit with the TestingPlatformExamples\TestingPlatformExplorer however in VS 2026 while the tests pass they don't have their status picked up and it looks like they haven't run.

Image

Command line works but oddly the command line doesn't show (or accept) the --treenode-filter or --filter to dotnet run -- only --filter-uid even though it looks like the CLI should be included. I tried manually shoehorning it in with

var cliOpts = new TestingFrameworkCommandLineOptions();
testApplicationBuilder.CommandLine.AddProvider(() => cliOpts);
testApplicationBuilder.AddTreeNodeFilterService(cliOpts);

but while it builds still doesnt have the option. I wasn't sure originally if it was a tunit or vstest thing (thomhurst/TUnit#4580) but it seems vstest.

ccdb0ee

Steps To Reproduce

Set-PSDebug -Trace 1
dotnet new console --name YourTestProjectNameHere
cd YourTestProjectNameHere
dotnet add package TUnit --prerelease
rm Program.cs
"namespace YourTestProjectNameHere;`npublic class MyTests {`n    [Test]`n    public async Task MyTest1() => await Assert.That(true).IsTrue();`n    [Test]`n    public async Task MyTest2() => await Assert.That(true).IsTrue();`n}" | Out-File -FilePath MyTests.cs
dotnet run --disable-logo --list-tests
Write-Host -Foreground Red "These Filters fail"
dotnet run --disable-logo --treenode-filter "/*/*/*/(MyTest1)"
dotnet run --disable-logo --treenode-filter "/*/*/*/(MyTest1)|(MyTest2)"
dotnet run --disable-logo --treenode-filter "/*/*/*/(MyTest1|MyTest2)"
# this fails but crashes dotnet run --disable-logo --treenode-filter "(/*/*/*/MyTest1)|(/*/*/*/MyTest2)"


Write-Host -Foreground Green "These filters work"
dotnet run --disable-logo --treenode-filter "/*/*/*/(MyTest1)|(MyTest2*)"
dotnet run --disable-logo --treenode-filter "/*/*/*/(MyTest1*)|(MyTest2)"
dotnet run --disable-logo --treenode-filter "/*/*/*/(MyTest1*)|(MyTest2*)"
dotnet run --disable-logo --treenode-filter "/*/*/*/(MyTest1*)|(MyTest2*)"
dotnet run --disable-logo --treenode-filter "/*/*/*/(MyTest1*|MyTest2)"
dotnet run --disable-logo --treenode-filter "/*/*/*/(MyTest1*|MyTest2*)"
dotnet run --disable-logo --treenode-filter "/*/*/*/MyTest*"
dotnet run --disable-logo --treenode-filter "/*/*/*/(MyTest1)|(*MyTest2)"
dotnet run --disable-logo --treenode-filter "/*/*/*/(*MyTest1)|(MyTest2)"
dotnet run --disable-logo --treenode-filter "/*/*/*/*(MyTest1)|(MyTest2)"
dotnet run --disable-logo --treenode-filter "/*/*/*/(MyTest1*)|MyTest2"
dotnet run --disable-logo --treenode-filter "/*/*/*/(MyTest1)|MyTest2*"

Log of said script attached

out.log

also, with 3 tests it is no more obvious this doesn't work: /*/*/*/(MyTest1|MyTest2|MyTest3) but /*/*/*/(MyTest1|MyTest2*|MyTest3) does.

Activity

  1. added
    area/mtp
    and removed
    needs/triage
    on Jan 28, 2026
  2. Evangelink commented on May 28, 2026

    @Evangelink
    Member

    Diagnosis

    After a deep investigation, the remaining cases here turned out to be a bug in TUnit's pre-filter, not in MTP. Posting the full root-cause analysis for posterity:

    Cases 1-3 (/*/*/*/(MyTest1), /*/*/*/(MyTest1|MyTest2), /*/*/*/MyTests/(MyTest1|MyTest2))

    Microsoft.Testing.Platform.Requests.TreeNodeFilter actually matches all three. A reflection probe against Microsoft.Testing.Platform 2.0.2 (the version TUnit 1.12.65 bundles):

    Filter MatchesFilter returns
    /*/*/*/(MyTest1) True
    /*/*/*/(MyTest1|MyTest2) True
    /*/*/*/MyTests/(MyTest1|MyTest2) True

    against /YourTestProjectNameHere/YourTestProjectNameHere/MyTests/MyTest1 (the path TUnit's TestFilterService.BuildPath produces for the repro). The existing OrExpression_WorksForSinglePathSegmentInsideParentheses unit test confirms the same.

    The reason no tests actually run is that TUnit's MetadataFilterMatcher.ExtractFilterHints pre-filters all discovered descriptors with AotTestDataCollector.CouldDescriptorMatch before MTP's TreeNodeFilter ever gets a chance. The pre-filter does:

    var parts = filterString.Split('/');
    ...
    if (parts.Length > 4 && !IsWildcard(parts[4])) { methodName = parts[4]; }
    ...
    private static bool IsWildcard(string value)
        => string.IsNullOrEmpty(value) || value == "*"
           || value.Contains('*') || value.Contains('?');

    So for /*/*/*/(MyTest1) → parts[4] is "(MyTest1)" → IsWildcard returns false → TUnit decides "the user is filtering on a literal method named (MyTest1)" and rejects every descriptor. The asymmetry observed in this issue matches perfectly: every "failing" pattern has no */? inside the parenthesised last segment; the working workaround /*/*/*/(MyTest1*) only succeeds because the * makes IsWildcard return true and disables the broken hint.

    Filed upstream as thomhurst/TUnit#6026 with a suggested fix.

    Case 4 ((/*/*/*/MyTest1)|(/*/*/*/MyTest2))

    This was the only true MTP-side issue and was fixed in #7415 — it now throws an actionable diagnostic with the original filter and a syntax suggestion (/A/B/C/(X|Y)).

    Follow-up housekeeping

    #7415 also added a LiteralSegment_RequiresWildcardToMatchNodesWithAdditionalSuffix unit test based on a hypothesis that turned out to be wrong (TUnit's node IDs don't append () or any suffix). I'm removing it in #8663.

    Resolution

    I'll close this once TUnit ships their fix for #6026, or earlier — feel free to close as "not a testfx bug" if you'd rather.

  3. Evangelink commented on May 28, 2026

    @Evangelink
    Member

    Closing as not planned on the testfx side:

    Please reopen if you hit another --treenode-filter symptom that doesn't reduce to either of the above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area/mtp

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions