How to capture structured xUnit test output in Gitlab CI?
Asked Answered
F

2

8

I am running automated tests on a .NET Core codebase on Gitlab CI. The only output from these tests is the console which contains a lot of garbage and makes it difficult to see exactly which tests failed.

Is there a way to get structured test output from xUnit in such a way that Gitlab can process the file and display results for specific tests much in the way it can with JUnit?

Ferroconcrete answered 20/8, 2019 at 13:26 Comment(0)
F
9

First, add the JUnitTestLogger nuget package to your test project:

dotnet add package JUnitTestLogger

Next, modify your test command:

dotnet test --logger "junit;LogFileName=MyProject.xml"

Finally, modify your .gitlab-ci.yml file to specify the output path:

test:
  ...
  artifacts:
    reports:
      junit: ./MyProject.xml
Ferroconcrete answered 20/8, 2019 at 14:11 Comment(2)
I'm not sure if it's change or not, but I had to use "juni;LogFilePath=MyProject.xml" - LogFilePATH not LogFileNAME.Stlaurent
The behavior of LogFileName changed in 2019 and seems to do something different now. Use LogFilePath instead.Rathenau
P
5
  1. Add the JUnitTestLogger NuGet package
  2. Use test command like: dotnet test --logger "junit;LogFilePath={path_to_XML_file}"
  3. Modify .gitlab-ci.yml file (look at my example). You have to use the parameters: when, paths, reports
... 
stages: 
  ...
  - tests 

variables:   
 LOGFILEPATH: /builds/myname/myrepository.api.tests/JUnitTestLogFile.xml ...

test_job:
    stage: tests
    script:
    - dotnet test --logger "junit;LogFilePath="$LOGFILEPATH
    artifacts:
        when: always
        paths:
        - $LOGFILEPATH
        reports:
            junit: $LOGFILEPATH
Pyrogenous answered 3/9, 2019 at 11:12 Comment(1)
This will not work if the path is not absolute, you would need to change to LogFilePath=../"$LOGFILEPATH to place the .xml file outside the project file, so you can correctly collect it. Can you modify your comment to warn people of this? A wasted some time figuring it out...Zoosporangium

© 2022 - 2024 — McMap. All rights reserved.