如何使用dotnet test命令发布dotnet core测试结果至指定目录?
Got it, let's tackle this—you absolutely can get your .NET Core test results out as XML or HTML to a specific directory with dotnet test, but the issue is probably how you're combining parameters (and maybe a tiny spelling mistake!). Here's the step-by-step breakdown:
1. Generating XML Test Results (TRX Format)
The default XML-based test result format for .NET Core is TRX (Visual Studio Test Results File), which is a valid XML format. The --results-directory parameter works, but you need to pair it with the --logger parameter to define the output format and filename.
Note: A super common mistake is using
--result-directory(singular) instead of--results-directory(plural)—that's likely why it wasn't working for you!
Use this command:
dotnet test --logger "trx;LogFileName=my_test_results.trx" --results-directory ./TestOutput
--logger "trx;LogFileName=...": Specifies we want TRX (XML) output, and sets a custom filename for the results.--results-directory ./TestOutput: Tells the command to save the TRX file in theTestOutputdirectory (it will create the directory if it doesn't exist).
You’ll find the XML-formatted test results at ./TestOutput/my_test_results.trx.
2. Generating HTML Test Results
dotnet test doesn’t support HTML output directly, but we can use a popular open-source tool called ReportGenerator to convert TRX/XML results into a clean, navigable HTML report.
Step 1: Install ReportGenerator
First, install the global tool:
dotnet tool install -g dotnet-reportgenerator-globaltool
Step 2: Generate TRX Results First
Run the same dotnet test command from section 1 to get your TRX file:
dotnet test --logger "trx;LogFileName=my_test_results.trx" --results-directory ./TestOutput
Step 3: Convert TRX to HTML
Use ReportGenerator to convert the TRX file to HTML:
reportgenerator -reports:"./TestOutput/**/*.trx" -targetdir:./TestOutput/HTMLReport -reporttypes:Html
-reports: Points to your TRX file (the**wildcard finds all TRX files in subdirectories).-targetdir: Defines where to save the HTML report.-reporttypes:Html: Specifies we want an HTML output.
After running this, you’ll find a fully interactive HTML report in ./TestOutput/HTMLReport.
3. Troubleshooting --results-directory Issues
If it’s still not working, check these common fixes:
- Double-check parameter spelling: It’s
--results-directory(plural), not--result-directory. - Verify directory permissions: Make sure your user has write access to the target directory. Try using an absolute path (e.g.,
C:\TestResultson Windows or/home/user/TestResultson Linux) if relative paths are causing issues. - Specify the test project explicitly: If you’re running the command from a solution root instead of the test project directory, add the
--projectparameter:dotnet test --project ./MyTestProject/MyTestProject.csproj --logger trx --results-directory ./TestOutput
内容的提问来源于stack exchange,提问作者Raghavendra Bankapur

