我如何为常规测试生成适合 Jenkins/Hudson 使用的 JUnit 测试报告?

发布于 2025-01-06 05:09:31 字数 1067 浏览 3 评论 0原文

我已经在 groovy 中编写了几个 XMLUnit 测试(适合 JUnit 框架),并且可以按照 groovy doco 但我不太明白我还需要做什么才能生成所需的 xml 输出Jenkins/Hudson(或其他)显示通过/失败结果(例如 this)以及错误的详细报告等(例如这个)。 (向图像所有者致歉)

目前,我的启动脚本是这样的:

def allSuite = new TestSuite('The XSL Tests')

//looking in package xsltests.rail.*
allSuite.addTest(AllTestSuite.suite("xsltests/rail", "*Tests.groovy")) 

junit.textui.TestRunner.run(allSuite)

这会产生类似这样的内容:

Running all XSL Tests...
....
Time: 4.141

OK (4 tests)

如何创建一个适合 Jenkins/Hudson 读取的 JUnit 测试报告 xml 文件?

我是否需要使用不同的 JUnit 运行器开始测试?

我已经看到这个答案,但希望避免编写自己的测试报告输出。

I've written several XMLUnit tests (that fit in to the JUnit framework) in groovy and can execute them easily on the command line as per the groovy doco but I don't quite understand what else I've got to do for it to produce the xml output that is needed by Jenkins/Hudson (or other) to display the pass/fail results (like this) and detailed report of the errors etc (like this). (apologies to image owners)

Currently, my kickoff script is this:

def allSuite = new TestSuite('The XSL Tests')

//looking in package xsltests.rail.*
allSuite.addTest(AllTestSuite.suite("xsltests/rail", "*Tests.groovy")) 

junit.textui.TestRunner.run(allSuite)

and this produces something like this:

Running all XSL Tests...
....
Time: 4.141

OK (4 tests)

How can I make this create a JUnit test report xml file suitable to be read by Jenkins/Hudson?

Do I need to kick off the tests with a different JUnit runner?

I have seen this answer but would like to avoid having to write my own test report output.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雪花飘飘的天空 2025-01-13 05:09:31

经过一番修改后,我采纳了 Eric Wendelin 的建议并选择了 Gradle。

为此,我已将 groovy 单元测试移至必需的目录结构 src/test/groovy/ 中,并将支持资源(输入和预期输出 XML 文件)移至 /src/test/resources/ 目录中。

所有必需的库都已在 build.gradle 文件中配置,如下所述(完整):

apply plugin: 'groovy'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.+'

    groovy module('org.codehaus.groovy:groovy:1.8.2') {
        dependency('asm:asm:3.3.1')
        dependency('antlr:antlr:2.7.7')
        dependency('xmlunit:xmlunit:1.3')
        dependency('xalan:serializer:2.7.1')
        dependency('xalan:xalan:2.7.1')
        dependency('org.bluestemsoftware.open.maven.tparty:xerces-impl:2.9.0')
        dependency('xml-apis:xml-apis:2.0.2')
    }
}

test {
    jvmArgs '-Xms64m', '-Xmx512m', '-XX:MaxPermSize=128m'

    testLogging.showStandardStreams = true //not sure about this one, was in official user guide

    outputs.upToDateWhen { false } //makes it run every time even when Gradle thinks it is "Up-To-Date"
}

这将应用 Groovy 插件,设置为使用 maven 来获取指定的依赖项,然后向内置“添加一些额外的值”测试”任务。

其中的另一件事是最后一行,它使 Gradle 每次都运行我的所有测试,而不仅仅是它认为是新的/更改的测试,这使得 Jenkins 运行得很好。

我还创建了一个 gradle.properties 文件来通过公司代理/防火墙等:

systemProp.http.proxyHost=10.xxx.xxx.xxx
systemProp.http.proxyPort=8080
systemProp.http.proxyUser=username
systemProp.http.proxyPassword=passwd

通过这个,我在 Jenkins 中创建了一个“自由式”项目,该项目定期轮询我们的 Mercurial 存储库,以及每当有人向存储库提交更新的 XSL 时将运行所有测试。

我最初的目标之一是能够生成标准 Jenkins/Hudson 通过/失败图形和 JUnit 报告,这是成功的:通过/失败JUnit 报告

我希望这对有类似要求的其他人有所帮助。

After a little hackage I have taken Eric Wendelin's suggestion and gone with Gradle.

To do this I have moved my groovy unit tests into the requisite directory structure src/test/groovy/, with the supporting resources (input and expected output XML files) going into the /src/test/resources/ directory.

All required libraries have been configured in the build.gradle file, as described (in its entirety) here:

apply plugin: 'groovy'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.+'

    groovy module('org.codehaus.groovy:groovy:1.8.2') {
        dependency('asm:asm:3.3.1')
        dependency('antlr:antlr:2.7.7')
        dependency('xmlunit:xmlunit:1.3')
        dependency('xalan:serializer:2.7.1')
        dependency('xalan:xalan:2.7.1')
        dependency('org.bluestemsoftware.open.maven.tparty:xerces-impl:2.9.0')
        dependency('xml-apis:xml-apis:2.0.2')
    }
}

test {
    jvmArgs '-Xms64m', '-Xmx512m', '-XX:MaxPermSize=128m'

    testLogging.showStandardStreams = true //not sure about this one, was in official user guide

    outputs.upToDateWhen { false } //makes it run every time even when Gradle thinks it is "Up-To-Date"
}

This applies the Groovy plugin, sets up to use maven to grab the specified dependencies and then adds some extra values to the built-in "test" task.

One extra thing in there is the last line which makes Gradle run all of my tests every time and not just the ones it thinks are new/changed, this makes Jenkins play nicely.

I also created a gradle.properties file to get through the corporate proxy/firewall etc:

systemProp.http.proxyHost=10.xxx.xxx.xxx
systemProp.http.proxyPort=8080
systemProp.http.proxyUser=username
systemProp.http.proxyPassword=passwd

With this, I've created a 'free-style' project in Jenkins that polls our Mercurial repo periodically and whenever anyone commits an updated XSL to the repo all the tests will be run.

One of my original goals was being able to produce the standard Jenkins/Hudson pass/fail graphics and the JUnit reports, which is a success: Pass/Fail with JUnit Reports.

I hope this helps someone else with similar requirements.

反目相谮 2025-01-13 05:09:31

我发现引导这些东西的最快方法是使用 Gradle

# build.gradle
apply plugin: 'groovy'

task initProjectStructure () << {
    project.sourceSets.all*.allSource.sourceTrees.srcDirs.flatten().each { dir ->
        dir.mkdirs()
    }
}

然后运行 ​​gradle initProjectStructure 并移动你的将源代码放入 src/main/groovy 并测试 test/main/groovy

看起来很多(实际上工作时间不到 5 分钟),但你可以免费获得很多东西。现在您可以运行 gradle test,它将运行您的测试并生成 JUnit XML,您可以在项目目录中的 build/test-reports 中使用。

I find the fastest way to bootstrap this stuff is with Gradle:

# build.gradle
apply plugin: 'groovy'

task initProjectStructure () << {
    project.sourceSets.all*.allSource.sourceTrees.srcDirs.flatten().each { dir ->
        dir.mkdirs()
    }
}

Then run gradle initProjectStructure and move your source into src/main/groovy and tests to test/main/groovy.

It seems like a lot (really it's <5 minutes of work), but you get lots of stuff for free. Now you can run gradle test and it'll run your tests and produce JUnit XML you can use in build/test-reports in your project directory.

深空失忆 2025-01-13 05:09:31

由于您询问将报告公开给 Jenkins/Hudson 的目的,我假设您有一个可以运行的 Maven/Ant/etc 版本。如果这是真的,那么解决方案就很简单了。

首先,Groovy 和 Java JUnit 测试之间几乎没有区别。因此,您需要做的就是将 Ant/Maven junit 任务/插件添加到您的构建中,并让它执行您的 Groovy junit 测试(就像用 Java 编写的测试一样)。该执行将创建测试报告。从那里,您可以简单地配置 Hudson/Jenkins 构建,以查看在构建过程中创建测试报告的目录。

Since you're asking for the purposes of exposing the report to Jenkins/Hudson, I'm assuming you have a Maven/Ant/etc build that you're able to run. If that's true, the solution is simple.

First of all, there's practically no difference between Groovy and Java JUnit tests. So, all you need to do is add the Ant/Maven junit task/plugin to your build and have it execute your Groovy junit tests (just as you'd do if they were written in Java). That execution will create test reports. From there, you can simply configure your Hudson/Jenkins build to look at the directory where the test reports get created during the build process.

一场春暖 2025-01-13 05:09:31

您可以编写自己的自定义 RunListener (或 SuiteRunListener)。它仍然需要您编写一些代码,但它比您提供的链接的脚本干净得多。如果您愿意,我可以向您发送我用 JavaScript 为 Jasmine 并且您可以将其“翻译”为 Groovy。

You can write your own custom RunListener (or SuiteRunListener). It still requires you to write some code, but it's much cleaner than the script you've provided a link to. If you'd like, I can send you the code for a JUnit reporter I've written in JavaScript for Jasmine and you can 'translate' it into Groovy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文