15

As part of our ASP.NET Core 2.0 build process I have added a dotnet test command which I have added as a Windows batch file.

Here is my command.

dotnet test "MyProject.csproj" --no-restore --results-directory "MyProject\TestResults" --verbosity minimal

And here is the output when run from the command line.

enter image description here

So it all appears to work correctly, yet no test results / test output is created.

DomBurf
  • 2,452
  • 5
  • 36
  • 71
  • Can you create the test results folder when you run the dotnet test on the agent machine directly? I mean run in command line locally. – Andy Li-MSFT Mar 21 '18 at 06:13
  • The above screenshot was taken from the build server. Do you mean try this on my local dev machine? – DomBurf Mar 21 '18 at 07:03
  • Yes, you can have a try on you local dev machine to check if the test results folder can be created. Also, you can try specify a logger for test results (`-l|--logger `) to check if that works. – Andy Li-MSFT Mar 21 '18 at 07:30

2 Answers2

35

To output the test results using the dotnet test option --results-directory you have to also set --logger.

The -xml and --work options no longer work as they are not part of the options provided by the test CLI. I remember have used -xml in the past and it worked but it doesn't anymore.

You can see all the options for CLI .NET Core 2.x here

To publish the tests results into a specific folder, you should use the command below:

dotnet test --logger "trx;logfilename=mytests.trx" --results-directory ./somefolder/subfolder

Or

dotnet test --logger "trx;LogFileName=./somefolder/subfolder/mytests.trx"

The trx file is a XML file, so you could name it as mytests.xml instead of mytests.trx.

If you use VSTS, you can publish your tests to be shown in your build page using the command above in '.NET Core' task for test and the 'Publish Test Result' task.

The '.NET Core' task explains where it publishes the results as per screenshot below:

enter image description here

Once all done, your build page would look like this:

enter image description here

Alexz S.
  • 2,366
  • 4
  • 21
  • 34
  • 2
    I'm using SonarQube, and by setting the logger (to nunit as I'm using NUnit3 for unit testing) the default file name TestResults.xml was correctly picked up and processed by SonarQube. NOTE: I needed to install the NuGet package NUnitXML.TestLogger in my Tests project to get this to activate. – From Orbonia Jun 18 '19 at 14:07
15

To output the test results from dotnet test, you can try pass -xml /some/path/out.xml or use the work parameter, like this: dotnet test --work:"mypath/myresult.xml". See below threads for details:


Besides, generally you need to specify the argument -l|--logger <LoggerUri/FriendlyName> which specifies a logger for test results.

e.g.:

dotnet test "myproject.csproj" --logger "trx;LogFileName=path\to\tests\folder\results.trx" or dotnet test "myproject.csproj" -l:"trx;LogFileName=path\to\tests\folder\results.trx"

To make the generated trx files available as test results in VSTS/TFS, you can use the "Publish Test Results" task:

enter image description here

jcc
  • 1,137
  • 1
  • 12
  • 31
Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55
  • 3
    dotnet test doesn't recognse either the --xml or --work parameters. Presumably to publish my test results I need to execute the unit tests first which is why I am trying to get dotnet test to work. – DomBurf Mar 21 '18 at 08:45
  • Following the first link I came across the following syntax which works. dotnet test "myproject.csproj" --logger "trx;LogFileName=path\to\tests\folder\results.trx" – DomBurf Mar 21 '18 at 09:08
  • `--logger` parameter shall be included in quotes. – Mike Jan 07 '19 at 21:19