How to publish dotnet core tests with jenkins pipeline?
I managed to do so using the "MSTestPublisher" class,
So the final pipeline is:
node {stage 'Checkout' checkout scmstage 'Build' bat "\"C:/Program Files/dotnet/dotnet.exe\" restore \"${workspace}/MyProg.sln\"" bat "\"C:/Program Files/dotnet/dotnet.exe\" build \"${workspace}/MyProg.sln\""stage 'UnitTests' bat returnStatus: true, script: "\"C:/Program Files/dotnet/dotnet.exe\" test \"${workspace}/MyProg.sln\" --logger \"trx;LogFileName=unit_tests.xml\" --no-build" step([$class: 'MSTestPublisher', testResultsFile:"**/unit_tests.xml", failOnError: true, keepLongStdio: true])}
I have uploaded some examples that I made to my GitHub for everyone to use and contribute, feel free to take a look:
https://github.com/avrum/JenkinsFileFor.NETCore
Those pipline jenkinsfile will add this pipline template to your build:
Jenkinsfile Declarative Pipeline
....stages { stage('UnitTests') { steps { bat "nunit3-console.exe ${env.WORKSPACE}/<TestProject>/bin/Release/<TestProject>.dll --result=nunit3.xml" } }}post { always { nunit testResultsPattern: 'nunit3.xml' }}
I have good results using the xUnit plugin (https://plugins.jenkins.io/xunit).
Run the tests generating trx output:
sh "dotnet test Tests/unit/ --logger:\"trx;LogFileName=results_Unit.testresults\""sh "dotnet test Tests/Integration/ --logger:\"trx;LogFileName=results_Integration.testresults\""
And to publish run xUnit with MSTest and the pattern:
xunit([MSTest(deleteOutputFiles: true, failIfNotNew: true, pattern: '**/*.testresults', skipNoTestFiles: false, stopProcessingIfError: true)])
I use declarative syntax so I put this last step in a post always
section. For example:
pipeline { agent any stages { stage('Build') { steps { sh "dotnet build" } } stage("run tests") { steps { sh "dotnet test Tests/unit/ --logger:\"trx:LogFileName=unit.testresults\"" } } } post { always { xunit([MSTest(deleteOutputFiles: true, failIfNotNew: true, pattern: '**/*.testresults', skipNoTestFiles: false, stopProcessingIfError: true)]) } }}