Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run and publish .NETCore Xunit tests on VSTS (Vs2017)?

I had two build steps in VSTS:

  1. To run tests (VSTS cmd task): DOTNET test -xml TEST-results.xml
  2. To publish test results step (VSTS test publish task): format=XUnit and the file name from previous step

But after I upgraded to VS2017 the -XML tag is not working anymore. I changed step one to use this: test --logger "trx;LogFileName=TEST-results.xml"

but the second step throws an error "Invalid results file. Please make sure the Test Result Format field in the task matches the result format of the file"

Is there another way to run .NetCore tests on VSTS? or am I doing something wrong?

Thanks,

like image 479
MuazzamAli Avatar asked Apr 10 '17 21:04

MuazzamAli


People also ask

How do I create a test report in xUnit?

One way to do it: add " --logger=trx" to the "dotnet test" command and then use the build step "Process xUnit test result report" from the "xUnit plugin". Use the option "MSTest-Version N/A (default) Pattern" and set pattern to "*/. trx". This is the answer!


2 Answers

starain-MSFT's answer will work, unless you want/need the xunit tests to be logged using an xunit logger. In that case, you'll need to do two things.

  1. Add https://www.nuget.org/packages/XunitXml.TestLogger/1.0.2-pre-rtm as package ref to your test project, either through 'Manage NuGet Packages' in VS, or by adding the ref in your csproj file manually, i.e.

<PackageReference Include="xunitxml.testlogger" Version="1.0.2-pre-rtm" />

  1. Modify the VSTS dotnet test build step to use this logger: dotnet test -a:. -l:xunit
    The -a:. switch, which specifies the adapter path, is only necessary for CLI tools V15.0, in 15.1 that can be removed (as discussed here). As of today, the VS2017 Hosted Queue is using 15.0, so you'll need the -a:. on VSTS for now. The -l:xunit uses the friendlyname, which I think isn't so friendly since you have to dig into the source code for the particular logger to find the attribute where it is specified (as seen here for xunit and here for trx)

The docs for the -l switch are spotty to say the least, but in the github for vstest, there is a document which talks about test loggers and links to their repositories and nuget packages, which after you look at the source for the friendlyname, gets you all the way there for whichever logger you need. If you need a custom logger, those are great examples to help understand how to implement.

Finally, the publish step that you used originally should be fine, since the output file is still called TestResults.xml

like image 180
Erikest Avatar answered Jan 04 '23 16:01

Erikest


Change "Test Result Format" to "VSTest of Publish Test" result step/task, it reads the result file correctly.

like image 24
starian chen-MSFT Avatar answered Jan 04 '23 16:01

starian chen-MSFT