This code was developed to allow Jenkins shared libraries to be tested before being pushed to Jenkins. This library allows Spock tests to be written against these Jenkins scripts.
There is an example in the example-shared-lib-structure directory, providing you with a basic outline for beginning to write tests for Jenkins scripts. As you can see in the build.gradle
file, this library is published to JCenter for consumption by any project.
Here is a simple Gradle block that calls out the dependency and loads it for use for tests.
repositories {
jcenter()
}
dependencies {
testCompile 'com.healthpartners.jenkins:jenkins-shared-library-test-helper:VERSION'
}
This library is then meant to be used with Groovy scripts
in the vars/
directory that are loaded into Jenkins as functions.
The special rule is that the script must end with return this
.
def call(args) {
//execution
}
return this
To test these scripts, do the following:
- Construct a binding object
- Initialize the script
def MyScriptSpec extends BasePipelineTest {
def 'this is a spock test'() {
given:
//binding object
def binding = initializeMocks('mockFunction', 'anotherFunction')
//initialize the script
def script = getShell(binding, 'test/testScript.groovy')
when:
//execute the call function on the script
def scriptOutput = script.call()
then:
//do validations
scriptOutput == "great stuff here"
//interact with binding mocks
1*binding.mockFunction.call() >> 2
//binding mocks can throw exceptions too
1*binding.anotherFunction.call() >> { throw new RuntimeException("badness")}
}
}
This library comes with three utility interfaces that can be used as mocks within testing your groovy scripts. Each of them is meant to encompass some of the features that Jenkins would provide in the pipeline interface when executing the script.
- James McShane - HealthPartners
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE.md file for details