Unit-test Jenkins pipeline code with Spock
Utility classes to help with testing Jenkins pipeline scripts and functions written in Groovy.
Add this library to
pom.xmlin the
testscope:
com.homeaway.devtools.jenkins jenkins-spock test
Check the CHANGELOG.md to find details about the available versions.
examplesdirectory contains working sample projects that show off the major kinds of project this library can be used with. Check them out and try building them yourself!
This library provides a
JenkinsPipelineSpecificationclass that extends the Spock testing framework's
Specificationclass. To test Jenkins pipeline Groovy code, extend
JenkinsPipelineSpecificationinstead of
Specification. Please see the GroovyDoc for
JenkinsPipelineSpecificationfor specific usage information and the Spock Framework Documentation for general usage information.
During the tests of a
JenkinsPipelineSpecificationsuite,
@StepDescriptors) will be globally callable, e.g. you can just write
sh( "echo hello" )anywhere.
@Symbols and
GlobalVariables) will be globally accessible, e.g. you can just write
docker.inside(...)anywhere
/varsdirectory) will be globally accessible, so you can just use them anywhere.
Jenkinsfileor Shared Library variable) to unit-test it in isolation.
Jenkinssingleton instance will exist as a Spock mock object.
CpsScriptexecution will exist as a Spock spy object (you should never need to interact with this, but it's there).
There are some dependencies of this library that are marked with Maven's
providedscope. This means that Maven will pull them in for building and testing this library, but when you use this library you must pull those libraries in as dependencies yourself.
This is done because these dependencies - things like the Jenkins Pipeline API, JUnit, etc - are things that
The dependencies that should already be in your project in order for using this library to make any sense are:
org.jenkins-ci.main jenkins-core ${jenkins.version} test org.jenkins-ci.plugins.workflow workflow-step-api ${jenkins.workflow.step.version} test org.jenkins-ci.plugins.workflow workflow-cps ${jenkins.workflow.cps.version} test org.jenkins-ci symbol-annotation ${jenkins.symbol.version} test junit junit ${junit.version} test javax.servlet javax.servlet-api ${jenkins.servlet.version} test
Depending on your parent pom, some of the
${jenkins.version}properties may already be defined. Be sure you define any that are not.
If your code actually writes code against classes in any of these dependencies, remove the
testentry for the corresponding block(s).
The build of jenkins-spock is built with Maven. Normal Maven lifecycle phases apply. As long as you have a contemporary (1.8+) JDK and Maven (3.3+), it should build fine.
Unit tests of
jenkins-spockwill happen automatically during the
testphase of the Maven build.
There is an
itMaven Profile that can be activated to run integration tests:
mvn verify -Pit
The integration tests will run
mvn verifyon some of the Working Example Projects, using the current
jenkins-spockcode.
jenkins-spock should be released by the
maven-release-plugin:
mvn clean release:prepare release:perform
In order for this to succeed, the user running this must
pushaccess to this repository.
com.homeawaygroupId.
maven-gpg-plugincan sign artifacts.