Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I produce JUnit test report for groovy tests, suitable for consumption by Jenkins/Hudson?

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.

like image 750
Scott Bennett-McLeish Avatar asked Feb 16 '12 14:02

Scott Bennett-McLeish


2 Answers

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.

like image 135
Scott Bennett-McLeish Avatar answered Oct 06 '22 12:10

Scott Bennett-McLeish


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.

like image 29
Eric Wendelin Avatar answered Oct 06 '22 12:10

Eric Wendelin