如何使用Jenkins Pipeline发布.NET Core测试?

5

我将尝试使用Jenkins流水线发布测试结果,

使用此流水线脚本,我成功完成了构建并生成了“xml”输出:

node {
stage 'Checkout'
    checkout scm

stage '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"
    nunit testResultsPattern: 'Tests\\*Test*\\TestResults\\unit_tests.xml'
}

但是在"UnitTest"阶段,我遇到了这个错误:

"发布NUnit测试结果报告无法读取XSL XML文件。 请向插件作者报告此问题"

当我使用“普通”构建步骤(不使用管道框架)进行测试时,测试得到发布。
使用:
  • DotNet Core 2.0
  • Jenkins 2.111
  • NUnit插件0.23
附上其中一个XML:

<?xml version="1.0" encoding="UTF-8"?>
<TestRun id="201fdc5c-9a47-461b-a56c-1aec8778053d" name="golano1@VM-CCT-BUILD3 2018-03-28 17:36:50" runUser="CORP\golano1" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
  <Times creation="2018-03-28T17:36:50.5380380+03:00" queuing="2018-03-28T17:36:50.5380497+03:00" start="2018-03-28T17:36:38.1418862+03:00" finish="2018-03-28T17:36:55.6498820+03:00" />
  <TestSettings name="default" id="3e9b0993-3888-4428-bd08-e217e3b54831">
    <Deployment runDeploymentRoot="golano1_VM-CCT-BUILD3 2018-03-28 17_36_50" />
  </TestSettings>
  <Results>
    <UnitTestResult executionId="77a19978-2bbb-4bcb-afaa-d5be4384e0d1" testId="a08b2ec5-a68d-434b-a35f-6c65159eee8f" testName="TestSentinalOneBigFile_Failed" computerName="VM-CCT-BUILD3" duration="00:00:05.0340000" startTime="2018-03-28T17:36:45.0000000+03:00" endTime="2018-03-28T17:36:50.0000000+03:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="77a19978-2bbb-4bcb-afaa-d5be4384e0d1" />
    <UnitTestResult executionId="49fb4bd0-d650-4ed4-b0f2-40241b138f40" testId="15590544-6d25-4b07-9736-91fc3089eb1d" testName="TestSentinalOneFullScanOfSingleFile_Success" computerName="VM-CCT-BUILD3" duration="00:00:05.0130000" startTime="2018-03-28T17:36:50.0000000+03:00" endTime="2018-03-28T17:36:55.0000000+03:00" testType="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b" outcome="Passed" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" relativeResultsDirectory="49fb4bd0-d650-4ed4-b0f2-40241b138f40" />
  </Results>
  <TestDefinitions>
    <UnitTest name="TestSentinalOneBigFile_Failed" storage="c:\jenkins\workspace\testfolder\pg_branchbuild\tests\sentinalonetests\bin\debug\netcoreapp2.0\sentinalonetests.dll" id="a08b2ec5-a68d-434b-a35f-6c65159eee8f">
      <Execution id="77a19978-2bbb-4bcb-afaa-d5be4384e0d1" />
      <TestMethod codeBase="C:\Jenkins\workspace\TestFolder\PG_BranchBuild\Tests\SentinaloneTests\bin\Debug\netcoreapp2.0\SentinaloneTests.dll" executorUriOfAdapter="executor://nunit3testexecutor/" className="SentinaloneTests.SentinaloneEngineConnectorTest" name="TestSentinalOneBigFile_Failed" />
    </UnitTest>
    <UnitTest name="TestSentinalOneFullScanOfSingleFile_Success" storage="c:\jenkins\workspace\testfolder\pg_branchbuild\tests\sentinalonetests\bin\debug\netcoreapp2.0\sentinalonetests.dll" id="15590544-6d25-4b07-9736-91fc3089eb1d">
      <Execution id="49fb4bd0-d650-4ed4-b0f2-40241b138f40" />
      <TestMethod codeBase="C:\Jenkins\workspace\TestFolder\PG_BranchBuild\Tests\SentinaloneTests\bin\Debug\netcoreapp2.0\SentinaloneTests.dll" executorUriOfAdapter="executor://nunit3testexecutor/" className="SentinaloneTests.SentinaloneEngineConnectorTest" name="TestSentinalOneFullScanOfSingleFile_Success" />
    </UnitTest>
  </TestDefinitions>
  <TestEntries>
    <TestEntry testId="a08b2ec5-a68d-434b-a35f-6c65159eee8f" executionId="77a19978-2bbb-4bcb-afaa-d5be4384e0d1" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
    <TestEntry testId="15590544-6d25-4b07-9736-91fc3089eb1d" executionId="49fb4bd0-d650-4ed4-b0f2-40241b138f40" testListId="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
  </TestEntries>
  <TestLists>
    <TestList name="Results Not in a List" id="8c84fa94-04c1-424b-9868-57a2d4851a1d" />
    <TestList name="All Loaded Results" id="19431567-8539-422a-85d7-44ee4e166bda" />
  </TestLists>
  <ResultSummary outcome="Completed">
    <Counters total="2" executed="2" passed="2" failed="0" error="0" timeout="0" aborted="0" inconclusive="0" passedButRunAborted="0" notRunnable="0" notExecuted="0" disconnected="0" warning="0" completed="0" inProgress="0" pending="0" />
    <Output>
      <StdOut>NUnit Adapter 3.8.0.0: Test execution startedRunning all tests in C:\Jenkins\workspace\TestFolder\PG_BranchBuild\Tests\SentinaloneTests\bin\Debug\netcoreapp2.0\SentinaloneTests.dllNUnit3TestExecutor converted 2 of 2 NUnit test casesNUnit Adapter 3.8.0.0: Test execution complete</StdOut>
    </Output>
  </ResultSummary>
</TestRun>

我在哪里做错了?

还尝试过以下方式:

step([$class: 'NUnitPublisher', testResultsPattern: 'Tests\\FilesScannerTests\\TestResults\\unit_tests.xml', debug: false, keepJUnitReports: true, skipJUnitArchiver:false, failIfNoResults: true])

但是它并没有顺利地运作起来。

我不熟悉Jenkins。但是看起来你正在要求dotnet test生成一个trx文件,然后再要求Jenkins发布一个NUnit格式的文件?我的猜测是nunit testResultsPattern...需要改成用于发布trx的命令。 - Chris
3个回答

4
我使用"MSTestPublisher"类实现了这一点,
因此最终的管道是:

node {
stage 'Checkout'
    checkout scm

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

我已经将我制作的一些示例上传到了我的GitHub,供大家使用和贡献,欢迎查看:

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

那些jenkinsfile管道会将此管道模板添加到您的构建中:

Example Jenkins Pipeline|Solid


@ Aviram:我正在使用相同的东西,日志显示正在编写unit_test.xml日志文件。但是,虽然jenkins没有记录测试结果,请问您能否建议我缺失了什么。 [MSTEST-PLUGIN]INFO正在处理文件中的测试结果 **/unit_tests.xml - Gaurav Parek

0

Jenkinsfile 声明性流水线

....
stages {
     stage('UnitTests') {
        steps {
            bat "nunit3-console.exe ${env.WORKSPACE}/<TestProject>/bin/Release/<TestProject>.dll --result=nunit3.xml"
        }
    }
}
post { 
    always {
         nunit testResultsPattern: 'nunit3.xml'
    }
}

不确定您是否可以使用nunit3-console运行.NetCoreApp,我遇到了很多异常,例如:“无法加载文件或程序集'nunit.framework'或其某个依赖项。系统找不到指定的文件”,以及在发布项目后,“无法加载文件或程序集'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'或其某个依赖项。系统找不到指定的文件。” - Aviram Fireberger

0

我使用xUnit插件(https://plugins.jenkins.io/xunit)获得了良好的结果。

运行测试生成trx输出:

sh "dotnet test Tests/unit/ --logger:\"trx;LogFileName=results_Unit.testresults\""
sh "dotnet test Tests/Integration/ --logger:\"trx;LogFileName=results_Integration.testresults\""

为了使用模式运行 MSTest,请使用 xUnit 进行发布:

xunit([MSTest(deleteOutputFiles: true, failIfNotNew: true, pattern: '**/*.testresults', skipNoTestFiles: false, stopProcessingIfError: true)])

我使用声明性语法,因此将此最后一步放在始终后置部分。例如:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接