Jenkins集成了dotnet测试

时间:2017-04-07 18:28:26

标签: jenkins .net-core

我正在使用dotnet测试对dotnet核心库进行单元测试。我像这样对我的Jenkins奴隶进行测试。

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

测试报告看起来像这样。

<?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...

我在Jenkins jobs DSL中使用archiveXUnit块来尝试阅读报告。

archiveXUnit {
  msTest {
    pattern('**/mstest-reports/*.xml')
  }
}

Jenkins似乎看到了这份报告。

记录测试结果

[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.

但它没有解析并将结果纳入其报告中。我没有在Jenkins构建仪表板上看到测试报告。

有什么想法吗?

3 个答案:

答案 0 :(得分:10)

所以我使用xUnit和trx格式,它很有效:

运行测试:

dotnet test test_dir\test_project.csproj --logger "trx;LogFileName=results\unit_tests.xml"

然而,使用它会给我以下问题:

No test discoverer is registered to perform discovery of test cases.
Register a test discoverer and try again.

因此,手动将xunit runner复制到测试项目的bin文件夹会修复此问题(是的,这很hacky):

copy packages\xunit.runner.visualstudio.2.2.0\build\_common\*.dll test_dir\bin\Release /Y

然后我添加了一个Publish xUnit测试步骤,如下所示:

Jenkins xUnit Reporter

然后在项目和构建页面上正确报告测试。

答案 1 :(得分:4)

您可以使用以下管道代码来运行和发布dotnet核心测试结果:

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])
}

我已经上传了一些我在GitHub上发布的示例供大家使用和贡献,请随时查看:

https://github.com/avrum/JenkinsFileFor.NETCore

那些pipline jenkinsfile会将这个pipline模板添加到你的构建中:

Example Jenkins Pipeline|Solid

答案 2 :(得分:0)

感谢Matt和kiml42!

我使用MSpec的dotnet core 2.2作为我的测试框架,不需要hacky复制,可以使用trx格式(https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-test?tabs=netcore21#examples)。

我使用的测试命令是:dotnet test --logger "trx;LogFileName=UnitTests.trx",它将在每个测试项目中运行测试,并将结果写入{ProjectFolder}/TestResults/UnitTests.trx

我确实安装了MSTest插件,该插件将trx转换为junit格式(https://wiki.jenkins.io/display/JENKINS/MSTest+Plugin)。

我还使用 post / always / step 块链接到测试结果,如下所示:

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

我有以下声明性Jenkinsfile管道:

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'
  }
}