构建xunit.xml文件后,我应该使用xUnitPublisher还是xUnitBuilder?

问题描述 投票:9回答:2

我正在自动化点网核心构建

鉴于我的Jenkins文件中有以下片段,我为每个测试项目生成一个XML文件。在下一步中,我想处理这些XML文件。

詹金斯有两种选择。我很困惑使用哪个选项。我使用“进程”还是“发布”。两者都给出了关于阈值的相同选项,两者似乎都是一样的。它们都标记了构建失败,它们都为Jenkins提供了测试报告。这是遗产吗?或者他们是完全不同的步骤,有自己的目的吗?

顺便说一下,这次FAILURE检查并抛出错误是阻止Jenkins继续构建的唯一方法吗?当构建标记为FAILED以继续其余步骤时,似乎有点奇怪。如果我想继续,我也可以将stopProcessingIfError设置为false,或者我错过了这一点?

stage('Test') {
    def testScript = ""
    def testProjects = findFiles(glob: 'test/**/project.json')

    if (!fileExists('reports/xml')) {
        if (!fileExists('reports')) {
            sh "mkdir reports"
        }
        sh "mkdir reports/xml"
    }

    for(prj in testProjects) {
        println "Test project located, running tests: " + prj.path
        def matcher = prj.path =~ 'test\\/(.+)\\/project.json'

        testScript += "dotnet test --no-build '${prj.path}' -xml 'reports/xml/${matcher[0][1]}.Results.xml' || true\n"
    }

    sh testScript

    step([
        $class: 'XUnitBuilder',
        thresholdMode: 1,
        thresholds: [[$class: 'FailedThreshold', failureThreshold: '1']],
        tools: [[
            $class: 'XUnitDotNetTestType',
            deleteOutputFiles: true,
            failIfNotNew: true,
            pattern: 'reports/xml/*.Results.xml',
            skipNoTestFiles: false,
            stopProcessingIfError: true
        ]]
    ])

    if (currentBuild.result.equals("FAILURE")) {
        throw "Test results did not pass thresholds"
    }
}
.net jenkins asp.net-core continuous-integration
2个回答
1
投票

看一下源代码后,它们的功能似乎相同,除了XUnitPublisher有一个额外的方法,其目的是我不理解(!),并且该类在implements列表中声明了更多的接口。

关键的区别似乎是XUnitPublisher类扩展了hudson.tasks.Recorder类,而XUnitBuilder扩展了hudson.tasks.Builder

我认为面向用户的区别在于构建器中的失败将Jenkins作业标记为“失败”,而发布者中的失败则将作业标记为“不稳定”。 (来源:https://wiki.jenkins.io/display/JENKINS/Terminology

鉴于这一切,我建议使用xUnitPublisher。我设置我的构建命令,如果编译通过但是一些测试失败,则返回0。这样,Jenkins给我一个FAILED状态,用于破解编译和工作编译的UNSTABLE状态但是测试失败。我喜欢这样。

提交历史并不能解释为什么存在这种荒谬的代码重复。我知道如果一个是以另一个方式实现的,就像通常在弃用时所做的那样......可能因为每个都必须有一个不同的超类。

XUnitBuilder.javaXUnitPublisher.java


0
投票

我设法使用这个简单的groovy \ pipline +运行unittests

所以最后的管道是:

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

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

Example Jenkins Pipeline|Solid

© www.soinside.com 2019 - 2024. All rights reserved.