Jenkins integration for dotnet test

+2 votes

I'm running a unit test of a dotnet core library using dotnet test. I run the test on my Jenkins slave like this.

dotnet test test/Turbine.Domain.UnitTest -xml mstest-reports/Turbine.Domain.UnitTest.xml

The test report looks like this.

<?xml version="1.0" encoding="utf-8"?>
<assemblies>
  <assembly name="Turbine.Domain.UnitTest.dll" environment="64-bit .NET (unknown version) [collection-per-class, parallel (8 threads)]" test-framework="xUnit.net 2.1.0.3179" run-date="2017-04-07" run-time="13:34:31" total="31" passed="31" failed="0" skipped="0" time="0.170" errors="0">
    <errors />
   <collection total="3" passed="3" failed="0" skipped="0" name="Test collection for Turbine.Domain.Tests.AccumulatePositionsTests" time="0.052">
      <test name="Turbine.Domain.Tests.AccumulatePositionsTests.CanAccumulatePositionsByPortfolioIndex" type="Turbine.Domain.Tests.AccumulatePositionsTests" method="CanAccumulatePositionsByPortfolioIndex" time="0.0402475" result="Pass" />
      <test name="Turbine.Domain.Tests.AccumulatePositionsTests.LotEventsTriggerPositionEventsImmediately" type="Turbine.Domain.Tests.AccumulatePositionsTests" method="LotEventsTriggerPositionEventsImmediately" time="0.0102925" result="Pass" />
      <test name="Turbine.Domain.Tests.AccumulatePositionsTests.CanAccumulatePositionsByDefaultIndex" type="Turbine.Domain.Tests.AccumulatePositionsTests" method="CanAccumulatePositionsByDefaultIndex" time="0.0012357" result="Pass" />
    </collection>
    <collection total="4" passed="4" failed="0" skipped="0" name="Test collection for Turbine.Domain.Tests.Queries.AnalyticsSummaryTests" time="0.087">
      <test name="Turbine.Domain.Tests.Queries.AnalyticsSummaryTests.MarketValueHandlesNegativeAmounts" type="Turbine.Domain.Tests.Queries.AnalyticsSummaryTests" method="MarketValueHandlesNegativeAmounts" time="0.0826806" result="Pass" />
      <test name="Turbine.Domain.Tests.Queries.AnalyticsSummaryTests.CanProduceFirmSummaryFromSnapshot" type="Turbine.Domain.Tests.Queries.AnalyticsSummaryTests" method="CanProduceFirmSummaryFromSnapshot" time="0.0012097" result="Pass" />
      <test name="Turbine.Domain.Tests.Queries.AnalyticsSummaryTests.GrossMarketValueHandlesNegativeAmounts" type="Turbine.Domain.Tests.Queries.AnalyticsSummaryTests" method="GrossMarketValueHandlesNegativeAmounts" time="0.0020873" result="Pass" />
      <test name="Turbine.Domain.Tests.Queries.AnalyticsSummaryTests.FirmSummaryProducesOutputOnQuote" type="Turbine.Domain.Tests.Queries.AnalyticsSummaryTests" method="FirmSummaryProducesOutputOnQuote" time="0.0010767" result="Pass" />
    </collection>
etc...

I use an archiveXUnit block in my Jenkins jobs DSL to try and read in the report.

archiveXUnit {
  msTest {
    pattern('**/mstest-reports/*.xml')
  }
}
Jenkins appears to see the report.
Recording test results
[xUnit] [INFO] - Starting to record.
[xUnit] [INFO] - Processing MSTest-Version N/A (default)
[xUnit] [INFO] - [MSTest-Version N/A (default)] - 1 test report file(s) were found with the pattern '**/mstest-reports/*.xml' relative to '/home/jenkins/workspace/routing/Turbine/build_Turbine' for the testing framework 'MSTest-Version N/A (default)'.
[xUnit] [INFO] - Check 'Failed Tests' threshold.
[xUnit] [INFO] - Check 'Skipped Tests' threshold.
[xUnit] [INFO] - Setting the build status to SUCCESS
[xUnit] [INFO] - Stopping recording.

But it isn't parsing and incorporating the results in its report. I don't see the test report on my Jenkins build dashboard.

Aug 8, 2018 in Jenkins by Hannah
• 16,420 points
1,544 views

4 answers to this question.

+1 vote
Best answer

You can use the following Pipeline code to run and publish the dotnet core test results:

node {
stage 'Checkout'
    cleanWs()
    checkout scm
stage 'Build'
    bat "\"C:/Program Files/dotnet/dotnet.exe\" restore \"${workspace}/YourProject.sln\""
    bat "\"C:/Program Files/dotnet/dotnet.exe\" build \"${workspace}/YourProject.sln\""
stage 'UnitTests'
    bat returnStatus: true, script: "\"C:/Program Files/dotnet/dotnet.exe\" test \"${workspace}/YourProject.sln\" --logger \"trx;LogFileName=unit_tests.xml\" --no-build"
    step([$class: 'MSTestPublisher', testResultsFile:"**/unit_tests.xml", failOnError: true, keepLongStdio: true])
}

answered Aug 8, 2018 by Kalgi
• 42,410 points

selected May 3 by Vardhan
+1 vote

Copy the xunit runner manually to the bin folder of the test project:

copy packages\xunit.runner.visualstudio.2.2.0\build\_common\*.dll test_dir\bin\Release /Y
answered May 3 by Ishaan
+1 vote
Your plugin might be of an older version. make sure all your plugins are up to date upgraded. This was the issue I had when I encountered this error.
answered May 3 by Aryaa
+1 vote

I have a test command which runs test projects and writes the result to {ProjectFolder}/TestResults/UnitTests.trx

dotnet test --logger "trx;LogFileName=UnitTests.trx"

I do have the MSTest plugin installed which converts from trx to junit format 

I've also added a link to the test results using a post/always/step block as follows:

  post {
    always {
      step ([$class: 'MSTestPublisher', testResultsFile:"**/TestResults/UnitTests.trx", failOnError: true, keepLongStdio: true])
    }
  }

My declarative Jenkinsfile pipeline:

pipeline {
  agent any
  stages {
    stage('Restore') {
      steps {
        sh 'dotnet restore'
      }
    }
    stage('Test') {
      steps {
        sh 'dotnet test --logger "trx;LogFileName=UnitTests.trx"'
      }
    }
    stage('Build') {
      steps {
        sh 'dotnet build'
      }
    }
    stage('Stop') {
      steps {
        sh 'sudo systemctl stop core-app.service'
      }
    }
    stage('Deploy') {
      steps {
        sh 'rm -rf /var/www/core-app'
        sh 'cp -R . /var/www/core-app'
      }
    }
    stage('Start') {
      steps {
        sh 'sudo systemctl start core-app.service'
      }
    }
  }
  post {
    always {
      step ([$class: 'MSTestPublisher', testResultsFile:"**/TestResults/UnitTests.trx", failOnError: true, keepLongStdio: true])
    }
  }
  tools {
    msbuild '.NET Core 2.2.103'
  }
  environment {
    ASPNETCORE_ENVIRONMENT = 'Production'
  }
}
answered May 3 by Fatima

Related Questions In Jenkins

0 votes
1 answer

Why is it displaying Jenkins build failure for Android app

I think you should try this. Try Uninstalling ...READ MORE

answered Apr 12, 2018 in Jenkins by shubham
• 6,890 points
265 views
0 votes
2 answers

Is possible for Jenkins to auto start and stop slave nodes?

Try Jenkins Plugin called as Slave SetupPlugin. This ...READ MORE

answered Aug 6, 2018 in Jenkins by Nilesh
• 6,880 points
1,066 views
0 votes
1 answer

Ansible or Jenkins pipelines for bigger jobs

Well, you are indeed well aware of ...READ MORE

answered Jun 16, 2018 in Jenkins by shubham
• 6,890 points
237 views
+1 vote
1 answer

Unable to access Jenkins. I newly installed Jenkins but for some reason I cannot access it.

Try changing the port, Change JENKINS_AJP_PORT="xyz" to JENKINS_AJP_PORT="abc" If changing the ...READ MORE

answered Jul 4, 2018 in Jenkins by Sophie may
• 9,870 points
372 views
+13 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
214 views
0 votes
1 answer
+1 vote
3 answers
0 votes
1 answer

Kubernetes - Jenkins integration

The best practice is to launch you ...READ MORE

answered Aug 6, 2018 in Jenkins by Kalgi
• 42,410 points
159 views