53

I have a test project written in dotnet core. This need to publish the results in an XML or HTML format. Is there a way I can publish the results to a particular directory using the same command?
--result-directory is not working for me

Alex 75
  • 2,798
  • 1
  • 31
  • 48
  • I have an trx/html example over here : https://stackoverflow.com/questions/49049029/how-to-get-code-coverage-report-in-donetcore-2-application/59951055#59951055 – granadaCoder Jan 28 '20 at 18:26

4 Answers4

63

You can see all the dotnet test options by executing dotnet test --help. One of the options is -l, --logger, which gives some great information:

Specify a logger for test results.
Examples:
Log in trx format using a unqiue file name: --logger trx
Log in trx format using the specified file name: --logger "trx;LogFileName=<TestResults.trx>"
More info on logger arguments support:https://aka.ms/vstest-report

That support link https://aka.ms/vstest-report, has the full information.

So to answer your specific question, you can say

dotnet test -l:trx;LogFileName=C:\temp\TestOutput.xml

To publish the results to a particular directory.

Another option is setting MSBuild properties in your test.csproj:

<PropertyGroup>
  <VSTestLogger>trx</VSTestLogger>
  <VSTestResultsDirectory>C:\temp</VSTestResultsDirectory>
</PropertyGroup>

Which tells the logger to put the file in the C:\temp directory.

Eric Erhardt
  • 2,286
  • 15
  • 19
  • I cannot create a fixed report file, this dotnet test always append a trailing timestamp `Results File: C:\myapp\results_2019-04-08_14-24-38-379.trx`. I wanted to be `C:\myapp\results.trx` as expected for `dotnet test myapp.csproj --logger trx;LogFileName=C:\temp\results --no-build --no-restore --configuration Release --verbosity q` – Junior Mayhé Apr 08 '19 at 13:27
  • @Eric Erhardt, where could I find the documentation for setting the MSBuild properties in your test.csproj? I have looked and looked, but cannot find any info. Thanks – Scott Hutchinson May 29 '19 at 19:27
  • I'm not sure if there is official documentation, I can't find it either. However, I found the properties by looking at how the `dotnet test` command invokes the project. See https://github.com/dotnet/cli/blob/a936ffd2056383dfcd2dd028cc1676b5c62c7654/src/dotnet/commands/dotnet-test/TestCommandParser.cs#L55 – Eric Erhardt May 30 '19 at 16:53
  • 6
    FYI, I had trouble running with `LogFileName` until I wrapped full option in quotation marks, e.g. `-l:"trx;LogFileName=testresult.xml"`. Also of note: when directory is not specified, the file goes into a `TestResults` folder. – sfuqua Jan 24 '20 at 15:42
  • What does trx stand for? – Jim Aho Dec 22 '21 at 22:58
  • https://fileinfo.com/extension/trx. I'm not 100% certain, but my guess would be "test result xml". – Eric Erhardt Dec 25 '21 at 20:47
34

After stumbling on the same problem (I wanted to publish test results in JUnit format), I ended up finding the JUnitTestLogger NuGet package.

It was a matter of installing it:

dotnet add package JUnitTestLogger --version 1.1.0

And then running the tests as:

dotnet test --logger "junit;LogFilePath=path/to/your/test/results.xml"

Edit: Check the more recent answer below, which uses JunitXml.TestLogger by Shane Rich.

Andre Albuquerque
  • 1,881
  • 1
  • 18
  • 15
  • 5
    Tax, I needed the xunit xml logger and since the "dotnet xunit" command is not longer supported as an external command line tool. Your post led me to find "XunitXml.TestLogger", thanks much, now I am using dotnet test --logger:"xunit;LogFilePath=test_result.xml" to which works for what I needed. – Christian Quirós May 28 '19 at 21:59
  • 2
    I always get the following error: `No test logging was found with AssemblyQualifiedName, URI, or FriendlyName "junit"` Do you know how to fix it? – Dominic Jonas Dec 02 '19 at 13:34
  • 2
    Make sure to run dotnet restore in the environment where you are executing the tests so that the junit assembly is available on the path. – Leo Hinojosa Dec 04 '19 at 18:26
  • works good, thanks for sharing.how to output all unit tests projects under a solution file, seems like the output junit report always be last unit test project. – Ethan Wu May 14 '20 at 02:54
6

I couldn't get this to work using the syntax provided in the answer of Eric Erhardt.

Using the TRX Logger examples here I was able to recreate the correct syntax.

dotnet test --logger:"trx;LogFileName=C:\Temp\TestResults.xml" MyLibraryToTest.dll

$ dotnet test --logger:"trx;LogFileName=C:\Temp\TestResults.xml" MyLibraryToTest.dll
Microsoft (R) Test Execution Command Line Tool Version 16.8.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Starting ChromeDriver 87.0.4280.88 (89e2380a3e36c3464b5dd1302349b1382549290d-refs/branch-heads/4280@{#1761}) on port 51459
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Results File: C:\Temp\TestResults.xml

Passed!  - Failed:     0, Passed:     1, Skipped:     0, Total:     1, Duration: 7 s - MyLibraryToTest.dll (net5.0)
Niels R.
  • 7,260
  • 5
  • 32
  • 44
3

I had success today with the nuget package JunitXml.TestLogger, version 3.0.114: https://www.nuget.org/packages/JunitXml.TestLogger/

To add as a dependency:
dotnet add package JunitXml.TestLogger --version 3.0.114

Once added as a dependency it was simply a matter of executing:
dotnet test --logger:"junit;LogFilePath=dotnet-test-result.xml"

At today's date JunitXml.TestLogger has four times as many stars as the nuget package JUnitTestLogger mentioned in another answer. JunitXml.TestLogger is being actively maintained, whereas JUnitTestLogger has not been touched for 2 years.

Shane Rich
  • 63
  • 6