使用 MSTest 插件生成测试报告

Generating test report using MSTest plugin

我目前在使用 MStest 插件时遇到一个奇怪的效果。我的测试报告只在测试成功时生成,失败时不生成测试报告。感谢这个答案, 我的管道看起来像这样

node {
        stage('Checkout') {
            cleanWs()
            checkout scm

            return
            skipRemainingStages = true
        }


        stage('Restore') {

            sh "dotnet restore  $project1"

        }

        stage('Build') {
            sh "dotnet publish $project1 --output $outputFolder --configuration Release -p:Version=$buildVersion -p:FileVersion=$buildVersion"

       }
        stage ('Unit test') {

               sh  "dotnet restore $UnitTest"
               sh returnStdout: true, script: "dotnet test $UnitTest --logger \'trx;LogFileName=unit_tests.xml\' "

               step ([$class: 'MSTestPublisher', testResultsFile:"**/*.xml", failOnError: true, keepLongStdio: true])

        } 
}

日志:

--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
Results File: /var/jenkins_home/workspace/myproject/Build_master/myproject/TestResults/unit_tests.xml

Total tests: 16. Passed: 8. Failed: 8. Skipped: 0.
Test Run Failed.
Test execution time: 15.9443 Seconds


[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

不知道为什么测试失败后不生成测试报告。 谢谢 enter image description here

您的测试未能通过您的构建,因此您永远无法真正发布测试结果。

你有两种方法来解决这个问题 - 要么将你的测试配置为在失败的测试中不 return 非零状态。

或者忽略它

sh "dotnet test $UnitTest --logger \'trx;LogFileName=unit_tests.xml\' || true"