Skip to content

Commit 661b395

Browse files
EvangelinkCopilot
andauthored
Document dotnet test (MTP) argument forwarding caveat (#54192)
* Document dotnet test (MTP) argument forwarding caveat When dotnet test consumes a recognized option that appears between an unrecognized option name and a value, the leftover tokens passed to the test application can change meaning. Document this behavior and recommend -- as a separator to mark test application arguments explicitly. Addresses dotnet/sdk#51990. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Move argument-forwarding caveat to dotnet run docs Address review feedback: - @Youssef1313: move the detailed explanation of the parser caveat to `dotnet run` (where the behavior originates) and have `dotnet test` (MTP mode) link to it. This avoids giving the impression that the issue is specific to MTP. - Copilot: split the TIP in unit-testing-with-dotnet-test.md into shorter sentences and remove the repeated `it`. - Copilot: add `ai-usage: ai-assisted` to the unit-testing-with-dotnet-test.md frontmatter. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 81934c5 commit 661b395

3 files changed

Lines changed: 40 additions & 3 deletions

File tree

docs/core/testing/unit-testing-with-dotnet-test.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ title: Testing with 'dotnet test'
33
description: Learn more about how 'dotnet test' works and its support for VSTest and Microsoft.Testing.Platform (MTP)
44
author: Youssef1313
55
ms.author: ygerges
6-
ms.date: 03/26/2025
6+
ms.date: 06/05/2026
7+
ai-usage: ai-assisted
78
---
89

910
# Testing with 'dotnet test'
@@ -158,6 +159,9 @@ For users of MTP that are using the VSTest mode of `dotnet test`, there are few
158159
1. If passing a specific project (or directory containing project), for example, `dotnet test MyProject.csproj`, this should become `dotnet test --project MyProject.csproj`.
159160
1. If passing a specific dll, for example, `dotnet test path/to/UnitTests.dll`, this should become `dotnet test --test-modules path/to/UnitTests.dll`. Note that `--test-modules` also supports globbing.
160161
162+
> [!TIP]
163+
> Even though `--` is no longer required in MTP mode, you can still use it to mark where test application arguments begin. The separator avoids a parser quirk where unrecognized arguments change meaning when interleaved with options that `dotnet test` understands. The separator also protects your scripts if `dotnet test` later starts recognizing one of those tokens. For more information, see [Forward arguments to the test application](../tools/dotnet-test-mtp.md#forward-arguments-to-the-test-application).
164+
161165
## Solutions with mixed test frameworks or extensions
162166
163167
When a solution contains test projects that use different test frameworks (for example, MSTest and xUnit.net) or different sets of extensions, running `dotnet test` with framework-specific or extension-specific command-line options can fail. Options that are valid for one project are unrecognized by another, causing exit code 5 (invalid command-line arguments). For example:

docs/core/tools/dotnet-run.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: dotnet run command
33
description: The dotnet run command provides a convenient option to run your application from the source code.
4-
ms.date: 09/29/2025
4+
ms.date: 06/05/2026
55
---
66
# dotnet run
77

@@ -61,6 +61,29 @@ To run the application, the `dotnet run` command resolves the dependencies of th
6161

6262
Any arguments that aren't recognized by `dotnet run` are passed to the application. To separate arguments for `dotnet run` from arguments for the application, use the `--` option.
6363

64+
## Forward arguments to the application
65+
66+
`dotnet run` forwards any token it doesn't recognize to the application. The forwarded tokens keep their original order, but `dotnet run` first removes the options it understands. When a recognized option appears between an unrecognized option name and its value, removing the recognized option can change the meaning of the leftover tokens.
67+
68+
For example, the following command interleaves the recognized option `--project` between tokens the application is meant to receive:
69+
70+
```dotnetcli
71+
dotnet run --app-flag --app-name --project ConsoleApp.csproj A.txt
72+
```
73+
74+
After `dotnet run` consumes `--project ConsoleApp.csproj`, the application receives `--app-flag --app-name A.txt`. The application then treats `A.txt` as the value of `--app-name`, which doesn't match the original command line.
75+
76+
To avoid this ambiguity, place application arguments after a literal `--`:
77+
78+
```dotnetcli
79+
dotnet run --project ConsoleApp.csproj -- --app-flag --app-name A.txt
80+
```
81+
82+
The `--` separator marks every following token as an application argument, so `dotnet run` doesn't reorder or reinterpret them. The separator also future-proofs scripts against new `dotnet run` options that might later match a token previously forwarded to the application.
83+
84+
> [!NOTE]
85+
> The same behavior applies to `dotnet build` and to `dotnet test` in Microsoft.Testing.Platform (MTP) mode, which forward unrecognized tokens to MSBuild or to the test application respectively. For more information about `dotnet test`, see [Forward arguments to the test application](dotnet-test-mtp.md#forward-arguments-to-the-test-application).
86+
6487
## Options
6588

6689
- **`--`**

docs/core/tools/dotnet-test-mtp.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: dotnet test command with Microsoft.Testing.Platform (MTP)
33
description: The dotnet test command is used to execute unit tests in a given project using MTP.
4-
ms.date: 02/03/2026
4+
ms.date: 06/05/2026
55
ai-usage: ai-assisted
66
---
77
# dotnet test with Microsoft.Testing.Platform (MTP)
@@ -168,6 +168,16 @@ With MTP, `dotnet test` operates faster than with VSTest. The test-related argum
168168
> [!NOTE]
169169
> To enable trace logging to a file, use the environment variable `DOTNET_CLI_TEST_TRACEFILE` to provide the path to the trace file.
170170
171+
## Forward arguments to the test application
172+
173+
`dotnet test` forwards any token it doesn't recognize to the test application. When a recognized option appears between an unrecognized option name and its value, removing the recognized option can change how the leftover tokens bind to options in the test application. To avoid this ambiguity, place test application arguments after a literal `--`:
174+
175+
```dotnetcli
176+
dotnet test --results-directory TestResults -- --report-trx --report-trx-filename A.trx
177+
```
178+
179+
The same parser behavior applies to `dotnet run` and `dotnet build`. For a detailed example, see [Forward arguments to the application](dotnet-run.md#forward-arguments-to-the-application) in the `dotnet run` reference.
180+
171181
## Examples
172182

173183
- Run the tests in the project or solution in the current directory:

0 commit comments

Comments
 (0)