Jenkins pipeline groovy testing in shell

4.3k views Asked by At

Can jenkins pipeline scripts be tested using groovysh or groovy scriptname to run tests for validation without using the Jenkins UI

For example for a simple script

pipeline {
  stages {
    stage ('test') {
      steps {
        sh '''
        env
        '''
       }
    }
  }
}

running a test like this, depending on the subset of scripting gives:

No signature of method: * is applicable for argument types

groovysh_evaluate.pipeline()

or for stage('test'){ sh ''' env ''' }

reports:

No signature of method: groovysh_evaluate.stages() 

or simply

   sh '''
   env
   '''

reports:

No signature of method: groovysh_evaluate.sh()

The question may be which imports are required and how to install them outside of a jenkins installation?

Why would anyone want to do this?

Simplify and shorten iterating over test cases, validation of library versions without modifying jenkins installations and other unit and functional test scenarios.

1

There are 1 answers

1
Vitalii Vitrenko On

JenkinsPipelineUnit is what you're looking for.

This testing framework lets you write unit tests on the configuration and conditional logic of the pipeline code, by providing a mock execution of the pipeline. You can mock built-in Jenkins commands, job configurations, see the stacktrace of the whole execution and even track regressions.