Skip to content

Add dotnet test --results-directory-layout to stop test reports overwriting each other - #55475

Merged
Evangelink merged 6 commits into
mainfrom
dev/amauryleve/test-results-layout
Aug 5, 2026
Merged

Evangelink merged 6 commits into
mainfrom
dev/amauryleve/test-results-layout

Conversation

@Evangelink

Copy link
Copy Markdown
Member

Problem

When dotnet test runs a solution, every test application resolves the same shared TestResults directory. Reports configured with a relative file name therefore overwrite each other and results are silently lost:

dotnet test MySolution.slnx --coverage --coverage-output coverage.cobertura.xml
# 2 test projects -> 1 coverage file

Reported in microsoft/codecoverage#226; the same happens with --report-trx-filename and xUnit's report options. This became visible once MTP started honoring DOTNET_CLI_TEST_COMMAND_WORKING_DIRECTORY, which dotnet test sets to the solution root (#52398 / #52570).

Change

Adds an SDK-side layout policy for the Microsoft.Testing.Platform path:

dotnet test --results-directory-layout <flat|per-module>
  • flat — the default, byte-for-byte today's behavior. When the user passes no --results-directory, no argument is forwarded to the child process, exactly as before.
  • per-module — every test application gets its own directory, modeled on the artifacts output layout:
TestResults
 └──📂 <project>                 # project file name
     └──📂 <tfm>_<rid-or-arch>   # pivot; elements joined by '_'

Real output for a two-project solution:

TestResults\TestProject\net11.0_x64
TestResults\OtherTestProject\net11.0_x64

Multi-targeting nests naturally (MyTests/net9.0_x64, MyTests/net10.0_x64) instead of producing sibling folders with concatenated names.

MTP gains no new option — the SDK just passes an explicit --results-directory per child process. "Per-module" has no meaning for a single test application, so the policy belongs to the aggregating layer that knows the whole module set.

Name collisions

Project names aren't unique within a run, so the whole module set is resolved up front and an identity hash is appended only when two distinct projects share a name:

TestResults\Tests_ac21be0e6a1f4f30\net11.0_x64   # src/Tests
TestResults\Tests_5f0c9d2b77e41a83\net11.0_x64   # samples/Tests

The hash is anchored to the directory common to all modules, so it doesn't change with the directory dotnet test was invoked from. Knowing the module set before anything runs required ITestHandler.EnumerateTestModules(), and TestModulesFilterHandler now builds its module list in Initialize() rather than as items are dequeued.

Tests

  • TestResultsDirectoryResolverTests — flat passthrough, nesting, multi-TFM, disambiguation, CWD-independence, cross-run stability, RID pivots, dotted-name sanitization, --test-modules fallback.
  • Parser tests for default/flat/per-module and rejection of invalid values; help snapshot updated.
  • End-to-end regression for the actual bug, using two projects that write the same relative report file name via MTP's GetTestResultDirectory(): flat yields 1 surviving report, per-module yields 2 with distinct contents.
  • End-to-end disambiguation with two real projects both named Tests.

37 targeted tests pass locally, including the existing --test-modules, globbing, and MSBuild handler suites that cover the refactor.

Notes for reviewers

  • Default stays flat to avoid changing output paths in a patch. Longer term I think per-module is the safer default, since silent data loss on a common flag is a bad failure mode — happy to follow up separately.
  • Collision-dependent naming has a trade-off: adding a second same-named project renames the first project's folder. That's inherent to only disambiguating when needed; worth calling out in docs.
  • --diagnostic-output-directory is still forwarded unchanged and is not scoped per module.
  • Configuration is deliberately not part of the pivot: a single run targets one configuration, so it would only add a constant level to every path.

Copilot AI review requested due to automatic review settings July 27, 2026 11:33
@Evangelink
Evangelink requested review from a team as code owners July 27, 2026 11:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new dotnet test option for Microsoft.Testing.Platform (MTP) that controls how the results directory is laid out across multiple test modules, preventing report files with the same relative name from overwriting each other when running solutions.

Changes:

  • Introduces --results-directory-layout <flat|per-module> (default flat) and wires it through parsing, help text, and localization resources.
  • Adds an SDK-side TestResultsDirectoryResolver and refactors the MTP handler pipeline to enumerate all modules up front so per-module directories can be computed deterministically (including name-collision disambiguation).
  • Adds unit + end-to-end regression tests plus new test assets to validate overwrites in flat and separation in per-module, including duplicate project-name scenarios.
Show a summary per file
File Description
test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectA/TestProjectA.csproj New MTP test asset project A to reproduce shared relative report name behavior.
test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectA/Program.cs Writes a relative report.txt into MTP’s results directory to validate overwrite/separation.
test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectB/TestProjectB.csproj New MTP test asset project B paired with project A.
test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/TestProjectB/Program.cs Writes a relative report.txt with different content to detect overwrites.
test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/MultiTestProjectSolutionWithSharedReportName.sln New solution containing both test assets for the overwrite regression.
test/TestAssets/TestProjects/MultiTestProjectSolutionWithSharedReportName/global.json Configures the test runner as Microsoft.Testing.Platform for the asset.
test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/src/Tests/Tests.csproj New asset for duplicate project-name disambiguation case (src).
test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/src/Tests/Program.cs Produces a report to verify hashed disambiguation preserves both outputs.
test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/samples/Tests/Tests.csproj New asset for duplicate project-name disambiguation case (samples).
test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/samples/Tests/Program.cs Produces a report to verify both projects with same name are kept distinct.
test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/MultiTestProjectSolutionWithDuplicateProjectNames.slnx Solution file used for duplicate-name disambiguation testing.
test/TestAssets/TestProjects/MultiTestProjectSolutionWithDuplicateProjectNames/global.json Configures MTP runner for the duplicate-name solution asset.
test/dotnet.Tests/CommandTests/Test/TestResultsDirectoryResolverTests.cs New unit tests covering resolver behavior (flat/per-module, pivoting, sanitization, hashing, stability).
test/dotnet.Tests/CommandTests/Test/TestCommandParserTests.cs Parser tests for new --results-directory-layout option and invalid value rejection.
test/dotnet.Tests/CommandTests/Test/snapshots/MTPHelpSnapshotTests.VerifyMTPHelpOutput.verified.txt Updates MTP help snapshot to include new option documentation.
test/dotnet.Tests/CommandTests/Test/GivenDotnetTestBuildsAndRunsTests.cs End-to-end tests for per-module directory creation and overwrite regression/disambiguation scenarios.
src/Cli/Microsoft.DotNet.Cli.Definitions/Commands/Test/TestCommandDefinition.MicrosoftTestingPlatform.cs Adds the new CLI option and integrates it into the MTP command options list.
src/Cli/Microsoft.DotNet.Cli.Definitions/CommandDefinitionStrings.resx Adds localized string resources for the new option help text and metavar name.
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.cs.xlf Adds XLF entries for the new option strings.
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.de.xlf Adds XLF entries for the new option strings.
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.es.xlf Adds XLF entries for the new option strings.
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.fr.xlf Adds XLF entries for the new option strings.
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.it.xlf Adds XLF entries for the new option strings.
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ja.xlf Adds XLF entries for the new option strings.
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ko.xlf Adds XLF entries for the new option strings.
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.pl.xlf Adds XLF entries for the new option strings.
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.pt-BR.xlf Adds XLF entries for the new option strings.
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ru.xlf Adds XLF entries for the new option strings.
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.tr.xlf Adds XLF entries for the new option strings.
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.zh-Hans.xlf Adds XLF entries for the new option strings.
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.zh-Hant.xlf Adds XLF entries for the new option strings.
src/Cli/dotnet/Commands/Test/MTP/Options.cs Introduces ResultsDirectoryLayout enum and threads it into PathOptions.
src/Cli/dotnet/Commands/Test/MTP/MSBuildUtility.cs Parses --results-directory-layout and stores the chosen layout in PathOptions.
src/Cli/dotnet/Commands/Test/MTP/TestResultsDirectoryResolver.cs New resolver implementing the flat vs per-module layout, including collision hashing and sanitization.
src/Cli/dotnet/Commands/Test/MTP/ITestHandler.cs Extends handler contract to enumerate all modules after initialization.
src/Cli/dotnet/Commands/Test/MTP/TestModulesFilterHandler.cs Refactors to build module list in Initialize() and expose it via EnumerateTestModules().
src/Cli/dotnet/Commands/Test/MTP/MSBuildHandler.cs Implements EnumerateTestModules() for MSBuild-discovered modules.
src/Cli/dotnet/Commands/Test/MTP/MicrosoftTestingPlatformTestCommand.cs Creates a TestResultsDirectoryResolver and passes it into the execution pipeline.
src/Cli/dotnet/Commands/Test/MTP/TestApplicationActionQueue.cs Plumbs resolver through the parallel action queue to per-module test application execution.
src/Cli/dotnet/Commands/Test/MTP/TestApplication.cs Uses resolver output to decide whether/how to append --results-directory per test module.

Copilot's findings

  • Files reviewed: 40/40 changed files
  • Comments generated: 0

@Evangelink
Evangelink enabled auto-merge July 27, 2026 11:38
@Evangelink
Evangelink force-pushed the dev/amauryleve/test-results-layout branch from 2a806aa to b09a9c4 Compare July 27, 2026 11:54
When 'dotnet test' runs a solution, every test application resolves the same
shared TestResults directory, so reports configured with a relative file name
(--coverage-output, --report-trx-filename, ...) overwrite each other and
coverage data is silently lost (microsoft/codecoverage#226).

Add an SDK-side layout policy for the Microsoft.Testing.Platform path:

  dotnet test --results-directory-layout <flat|per-module>

'flat' is the default and keeps today's behavior. 'per-module' gives every
test application its own directory, modeled on the artifacts output layout:

  <results-directory>/<project>/<target-framework>_<runtime>

Microsoft.Testing.Platform gains no new option; the SDK simply passes an
explicit --results-directory per child process, so the whole module set is
resolved up front. That also lets the layout append an identity hash to the
project folder only when two distinct projects in the run share a name.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 0cb64c89-128b-4f8f-88cc-2741ffac3767
@Evangelink
Evangelink force-pushed the dev/amauryleve/test-results-layout branch from b09a9c4 to d73fa64 Compare July 27, 2026 16:17
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 00868f6a-f41f-422e-aaf7-0cc9f56142a1
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 00868f6a-f41f-422e-aaf7-0cc9f56142a1
@Evangelink
Evangelink requested review from a team, lbussell and mthalman as code owners August 3, 2026 18:27

@baronfel baronfel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

containers tests changes LGTM

@Evangelink

Copy link
Copy Markdown
Member Author

@mthalman @lbussell can you please approve?

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: cae287b8-f62a-4246-94cd-bb162e1273a6
@Evangelink

Copy link
Copy Markdown
Member Author

@lbussell it seems that I need your approval.

@Evangelink
Evangelink merged commit a86dbf9 into main Aug 5, 2026
21 checks passed
@Evangelink
Evangelink deleted the dev/amauryleve/test-results-layout branch August 5, 2026 15:13
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.

7 participants