Skip to content

Add a typed test filter model and a VSTest-conformant filter parser with legacy fallback - #1553

Open
PanzerFowst wants to merge 15 commits into
nunit:mainfrom
PanzerFowst:feature/new-test-filter-parser
Open

PanzerFowst wants to merge 15 commits into
nunit:mainfrom
PanzerFowst:feature/new-test-filter-parser

Conversation

@PanzerFowst

@PanzerFowst PanzerFowst commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds a typed test filter model and a VSTest-conformant filter parser, and routes the adapter's filter parsing through them. By default, filters keep working as before: the VSTest parser is tried first, and a filter it rejects goes to the existing (now LegacyTestFilterParser) parser.

Related to #505. Went over original design with @OsirisTerje in call.

The design is described in docs/VSTestFilterParserDesign.md (which I will delete before completing the PR).

Pipeline

filter text → ITestFilterParser → typed filter model (ITestFilter) → visitor → NUnit filter XML

What changes

  • Filter model (TestFilterConverter/FilterModel/): expression / term / factor / group / negation / condition nodes.
    • Comparisons are an enum, TestFilterComparison { Exact, Contains }.
    • != and !~ are a negation node around a condition, not separate operators.
    • The model holds no syntax and no output format.
  • Visitor: ITestFilterVisitor<T>, with NUnitFilterXmlWriter and a ToNUnitXml() extension producing NUnit filter XML. This design allows for other outputs, such as JSON or an MTP evaluator, to be added later as new visitors without touching the model or the parsers.
  • Parsers implementing ITestFilterParser:
    • VSTestFilterParser: new. It follows VSTest's escaping and grouping rules, rejects malformed input, and reports an error position.
    • LegacyTestFilterParser: the existing TestFilterParser, renamed. Its behavior is unchanged.
    • DefaultTestFilterParser: tries an ordered list of parsers (VSTest first, then Legacy) and returns the first result.
      • Each rejection is logged at Info.
      • If every parser rejects the filter, the last parser's error is thrown, so users still see the legacy error.
  • Name filters: NUnitTestFilterBuilder.FilterByNames now builds name filters through the model. TestNameFilter is reduced to the internal TestNameRegex. XmlEscaperHelper is removed.
  • Parser injection: NUnitTestFilterBuilder and the discoverer receive the parser instead of creating one. It is chosen once, in NUnitTestAdapter.Initialize.
  • Fix: on a command-line run with the NUnit filter, the filter was parsed once in RunTests and again for every test assembly. Both conversions have existed since 2020. The per-assembly one always produced the same result, so VsTestExecution now uses the filter it is given.

New run setting: FilterParser

Value Behaviour
Default (default) VSTest parser, falling back to Legacy (from conversation with Terje to eliminate 7.0 breaking change in the filter parsing)
VSTest VSTest parser only; a rejected filter is an error
Legacy Legacy parser only (6.x behaviour)
<RunSettings><NUnit><FilterParser>VSTest</FilterParser></NUnit></RunSettings>

dotnet test --filter "..." -- NUnit.FilterParser=Legacy

Under MTP it can currently only be set through a runsettings file (--settings).

Compatibility

  • Behaviour: I ran 17 representative filters through both parsers.

    • 9 gave identical XML.
    • 8 were rejected by the VSTest parser and fall back to Legacy, so they behave as before. Examples: an unescaped ( in a name such as FullyQualifiedName=A.B(1,2), and !(Name=A).
    • None were accepted by both with different output.

    So the new default is not expected to change which tests a filter selects. Making strict VSTest the default would be breaking and is left for a major version.

  • API: these public types change, which is binary-breaking for anyone using them directly:

    • the NUnitTestFilterBuilder constructor now takes an ITestFilterParser;
    • IExecutionContext and IAdapterSettings gain a member each;
    • TestFilterParser is renamed LegacyTestFilterParser.

    The new model and parser types are public. Happy to make them internal if you prefer a smaller public surface.

Testing

  • New unit tests:
    • FilterModelTests: model and XML writer, including every TestFilterComparison value;
    • TestFilterParserConformanceTests: VSTest vs Legacy, intended differences, error positions;
    • DefaultTestFilterParserTests;
    • the FilterParser setting in AdapterSettingsTests;
    • parser injection in NUnitTestFilterBuilderTests;
    • VsTestExecutionTests: the filter is not parsed again for each assembly.
  • build -t test: net462 767 passed / 45 skipped; net8.0 764 passed / 42 skipped.
  • build -t acceptance: 227 passed.
  • Checked manually with dotnet test against the built package. With the default parser, a VSTest-invalid filter logs one Info line and still selects the right test. FilterParser=VSTest reports the VSTest error, and FilterParser=Legacy behaves as before.

Use of AI

I designed this and drove it commit by commit, using GitHub Copilot to draft code, tests, and docs. I reviewed every change and ran the unit and acceptance tests after each step.

Possible follow-ups

  • Route the MTP FullyQualifiedName fast path through ITestFilterParser.
  • Document FilterParser in nunit/docs.
  • Make VSTest the default in a major version.

Review guide

The commits are designed to be read one at a time; each one builds and passes its tests. Suggested order:

  1. 324d9c1, cc53f42: filter model interfaces and types (no behaviour change)
  2. 7cb9614, 3363eae, 4c9eca1: VSTestFilterParser and its tests (not wired in yet)
  3. 329b0c8: rename TestFilterParser → LegacyTestFilterParser (rename only)
  4. bc0eccb, f1cba8f: the legacy parser produces the model; !(...) becomes a negation node
  5. 54424b0, 981767c: comparison enum; XML output moves into a visitor
  6. 236862c: name filters built through the model
  7. 14f644d: parser injection, DefaultTestFilterParser, FilterParser setting
  8. f6fd156: parse the filter once per run instead of once per assembly

In the combined diff, TestFilterParser.cs → LegacyTestFilterParser.cs shows as a delete plus an add. Commit 3 shows it as a rename.

Final Note

This is a large PR. If you would prefer it split (e.g., model + XML writer in one PR and the parsers and the setting in a second), I'm happy to do that.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant