Skip to content

Support file-based apps in MTP dotnet test - #55701

Merged
Evangelink merged 2 commits into
mainfrom
dev/amauryleve/test-file-based-mtp
Aug 24, 2026
Merged

Evangelink merged 2 commits into
mainfrom
dev/amauryleve/test-file-based-mtp

Conversation

@Evangelink

Copy link
Copy Markdown
Member

Summary

  • support C# file-based test apps in the Microsoft.Testing.Platform dotnet test flow
  • build and evaluate virtual projects for positional and --project file inputs
  • preserve MTP option forwarding, -- application arguments, multi-targeting, no-build/no-restore, and binary logging
  • reject device selection for file-based test apps until virtual projects support that flow
  • update command help and localization resources

Tests

  • build.cmd
  • focused dotnet.Tests coverage for file-based MTP apps, MTP help, parser, and validation (25 passed)

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: 213ea7a8-7f47-4187-a91f-3de3348d19f9
Copilot AI lite review requested due to automatic review settings August 10, 2026 19:03
@Evangelink
Evangelink requested review from a team as code owners August 10, 2026 19:03
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

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 support for running C# file-based test apps (e.g., dotnet test MyTests.cs) through the Microsoft.Testing.Platform (MTP) dotnet test flow. This extends the CLI’s MTP pipeline to recognize file-based entry points, build/evaluate a virtual project, and keep existing option/argument-forwarding behaviors consistent.

Changes:

  • Add file-based entry point detection and virtual-project build/evaluation to the MTP MSBuild orchestration path.
  • Update MTP argument parsing/validation so -- application arguments don’t interfere with positional project/file detection, and reject device selection for file-based apps.
  • Add end-to-end dotnet.Tests coverage plus update help output snapshot and localization resources.
Show a summary per file
File Description
test/dotnet.Tests/CommandTests/Test/snapshots/MTPHelpSnapshotTests.VerifyMTPHelpOutput.verified.txt Updates MTP dotnet test help snapshot for --project to mention file-based apps.
test/dotnet.Tests/CommandTests/Test/GivenDotnetTestRunsFileBasedApps.cs Adds end-to-end tests covering file-based MTP execution, options, -- handling, multi-targeting, binlogs, and validation.
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.zh-Hant.xlf Updates localized --project description string (marks translation for review).
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.zh-Hans.xlf Updates localized --project description string (marks translation for review).
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.tr.xlf Updates localized --project description string (marks translation for review).
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ru.xlf Updates localized --project description string (marks translation for review).
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.pt-BR.xlf Updates localized --project description string (marks translation for review).
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.pl.xlf Updates localized --project description string (marks translation for review).
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ko.xlf Updates localized --project description string (marks translation for review).
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.ja.xlf Updates localized --project description string (marks translation for review).
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.it.xlf Updates localized --project description string (marks translation for review).
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.fr.xlf Updates localized --project description string (marks translation for review).
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.es.xlf Updates localized --project description string (marks translation for review).
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.de.xlf Updates localized --project description string (marks translation for review).
src/Cli/Microsoft.DotNet.Cli.Definitions/xlf/CommandDefinitionStrings.cs.xlf Updates localized --project description string (marks translation for review).
src/Cli/Microsoft.DotNet.Cli.Definitions/CommandDefinitionStrings.resx Updates the canonical --project description string to include file-based apps.
src/Cli/dotnet/Commands/xlf/CliCommandStrings.zh-Hant.xlf Adds new localized resource entry for “device options not supported for file-based apps”.
src/Cli/dotnet/Commands/xlf/CliCommandStrings.zh-Hans.xlf Adds new localized resource entry for “device options not supported for file-based apps”.
src/Cli/dotnet/Commands/xlf/CliCommandStrings.tr.xlf Adds new localized resource entry for “device options not supported for file-based apps”.
src/Cli/dotnet/Commands/xlf/CliCommandStrings.ru.xlf Adds new localized resource entry for “device options not supported for file-based apps”.
src/Cli/dotnet/Commands/xlf/CliCommandStrings.pt-BR.xlf Adds new localized resource entry for “device options not supported for file-based apps”.
src/Cli/dotnet/Commands/xlf/CliCommandStrings.pl.xlf Adds new localized resource entry for “device options not supported for file-based apps”.
src/Cli/dotnet/Commands/xlf/CliCommandStrings.ko.xlf Adds new localized resource entry for “device options not supported for file-based apps”.
src/Cli/dotnet/Commands/xlf/CliCommandStrings.ja.xlf Adds new localized resource entry for “device options not supported for file-based apps”.
src/Cli/dotnet/Commands/xlf/CliCommandStrings.it.xlf Adds new localized resource entry for “device options not supported for file-based apps”.
src/Cli/dotnet/Commands/xlf/CliCommandStrings.fr.xlf Adds new localized resource entry for “device options not supported for file-based apps”.
src/Cli/dotnet/Commands/xlf/CliCommandStrings.es.xlf Adds new localized resource entry for “device options not supported for file-based apps”.
src/Cli/dotnet/Commands/xlf/CliCommandStrings.de.xlf Adds new localized resource entry for “device options not supported for file-based apps”.
src/Cli/dotnet/Commands/xlf/CliCommandStrings.cs.xlf Adds new localized resource entry for “device options not supported for file-based apps”.
src/Cli/dotnet/Commands/Test/MTP/ValidationUtility.cs Extends MTP path validation to recognize file-based entry points as valid inputs.
src/Cli/dotnet/Commands/Test/MTP/SolutionAndProjectUtility.cs Refactors project evaluation to support virtual-project evaluation via injected evaluator delegate.
src/Cli/dotnet/Commands/Test/MTP/MSBuildUtility.cs Adds file-based project handling (virtual build + evaluation) and improves -- argument boundary handling for positional parsing.
src/Cli/dotnet/Commands/Test/MTP/MicrosoftTestingPlatformTestCommand.cs Rejects --device / --list-devices when the input is a file-based app.
src/Cli/dotnet/Commands/CliCommandStrings.resx Adds the new error message resource for device options + file-based apps.

Review details

  • Files reviewed: 34/34 changed files
  • Comments generated: 0
  • Review effort level: Lite

@Evangelink
Evangelink enabled auto-merge August 24, 2026 09:38
@Evangelink

Copy link
Copy Markdown
Member Author

/ba-g #55258

@JanKrivanek JanKrivanek 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.

Note

This review was generated with AI assistance.

Review of the file-based-app support in the MTP dotnet test flow. The overall approach looks sound - the GetProjectProperties refactor into a Func<string?, ProjectInstance> seam is clean, and TrySplitApplicationArgumentsAtDoubleDash has a safe fallback when the token and argument views disagree.

7 comments: 2 behavioral divergences from the project-based / dotnet run paths, 1 unflagged behavior change to existing project-based dotnet test, 3 test-coverage gaps, and 1 documentation sync.

BuildOptions buildOptions,
MSBuildSession buildSession)
{
var msbuildArgs = SolutionAndProjectUtility.AnalyzeStandardTestMSBuildArgs(buildOptions.MSBuildArgs);

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.

The project/solution path adds -verbosity:quiet when the user did not pass --verbosity (see BuildOrRestoreProjectOrSolution below), but this path does not, and VirtualProjectBuildingCommand falls back to MSBuildForwardingAppWithoutLogging.DefaultVerbosity (= minimal). dotnet run forces quiet for the same reason (RunCommand.SetupSilentBuildArgs).

Net effect: dotnet test app.cs prints MSBuild build output that dotnet test app.csproj suppresses.

Suggested fix:

var msbuildArgs = SolutionAndProjectUtility.AnalyzeStandardTestMSBuildArgs(buildOptions.MSBuildArgs);
if (buildOptions.Verbosity is null)
{
    msbuildArgs = msbuildArgs.CloneWithVerbosity(VerbosityOptions.quiet);
}

Note that PropagateBuildOptions and RepeatedFileAfterDoubleDashDoesNotHideTheTestInput currently assert on that leaked build output ("PassingTests.cs ->", \release\), so they would need to assert on MTP output instead.

string fullEntryPointFilePath = Path.GetFullPath(entryPointFilePath);
var buildCommand = new VirtualProjectBuildingCommand(
fullEntryPointFilePath,
msbuildArgs)

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.

MTPTargetOption forwards --target:_MTPBuild, so Builder.RequestedTargets is ["_MTPBuild"] and Execute runs only that target instead of the [Build, CoreCompile] default. dotnet run explicitly appends CoreCompile for exactly this reason (RunCommand.CreateProjectBuilder).

With no CoreCompile result in the BuildResult, CacheCscArguments logs "No CSC arguments found in targets" but MarkBuildSuccess still writes a cache entry with empty CscArguments and null BuildResultFile. It degrades safely, but it silently disables the csc fast path for a subsequent dotnet run app.cs on the same file.

Suggested fix - append CoreCompile to the build command only:

var buildCommand = new VirtualProjectBuildingCommand(
    fullEntryPointFilePath,
    msbuildArgs.CloneWithAdditionalTargets(Constants.CoreCompile))

ImmutableArray<string> loggerArgs;
ImmutableArray<string> otherArgs;
int positionalArgumentCount;
if (CommonRunHelpers.TrySplitApplicationArgumentsAtDoubleDash(

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.

This is not file-based-specific - it changes dotnet test for projects and solutions too. Positional scanning for .sln/*proj/.dll now stops at --, and logger arguments after -- are no longer hoisted into loggerArgs.

So dotnet test -- foo.csproj no longer selects the project, and dotnet test -- -bl no longer produces a binlog.

Both look like improvements, but they are user-visible changes to an existing flow with no project-based regression test and no mention in the PR description. Suggest adding a project/solution case to the -- tests and calling the change out in the description.

}

[TestMethod]
public void FileAfterDoubleDashIsNotUsedAsTheTestInput()

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.

These -- tests only assert the negative (the file after -- is not consumed as the test input). Since GetBuildOptions now rewrites argument handling for every dotnet test invocation, the assertion that is missing is the positive one: that arguments after -- are actually forwarded to the test application.

Suggest adding a test that passes an MTP option after -- and asserts it took effect.

}

[TestMethod]
public void RunWithNoBuild()

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.

This test (and RunMultipleTargetFrameworks) builds first, so the never-built case is not covered.

With --no-build on a fresh file, GetProjectsFromFile skips buildCommand.Execute() entirely - so no restore either - and goes straight to CreateProjectInstance, which still has to resolve #:sdk MSTest.Sdk@.... Worth a test asserting a sensible error there rather than an evaluation failure.

&& VirtualProjectBuilder.IsValidEntryPointPath(projectOrSolutionPath)
&& (buildOptions.ListDevices || !string.IsNullOrWhiteSpace(buildOptions.Device)))
{
throw new GracefulException(CliCommandStrings.CmdDeviceOptionsNotSupportedForFileBasedApps);

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.

This catches --device/--list-devices but not -p:Device=....

For project-based runs SelectDevicesBeforeBuild treats an explicit Device global property as a device selection, but GetProjectsFromProject now dispatches to GetProjectsFromFile before device selection runs, so for a file-based app -p:Device=x is silently dropped with no diagnostic.

Suggest also checking the Device global property here so the same GracefulException is thrown.


if (VirtualProjectBuilder.IsValidEntryPointPath(projectFilePath))
{
return GetProjectsFromFile(projectFilePath, buildOptions, buildSession);

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.

documentation/general/dotnet-run-file.md needs updating in this PR:

  • the "Other commands" section enumerates the commands that support file-based programs (restore, build, publish, pack, clean) - dotnet test should be added;
  • the "Other possible commands" section still says additional commands "might never" be supported.

Per the repo's docs-in-sync guidance, this should land with the change rather than after it.

@JanKrivanek JanKrivanek 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.

Other then the recommended fixes - the overall feature and directions LGTM
So feel free to use your judgement what to fix

@Evangelink
Evangelink merged commit 05354ff into main Aug 24, 2026
40 of 42 checks passed
@Evangelink
Evangelink deleted the dev/amauryleve/test-file-based-mtp branch August 24, 2026 14:06
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Aug 25, 2026

This branch had an error being deployed

1 failed deployment
copilot-pat-pool — 3d1f463a Deployed Aug 24, 2026 by Evangelink via pre_activation #753
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.

3 participants