How to test Jenkinsfiles with JenkinsPipelineUnit under Maven

2.1k Views Asked by At

I am pretty new with using Jenkins pipelines and want to test my Jenkinsfiles and libraries. I found the JenkinsPipelinUnit but i am not able to let my tests running.
I am building a microservice with Maven and added the dependency for JenkinsPipelinUnit.

Which maven plugin do I need to add to run the tests? What you have added? Do you have found any good examples or templates?

2

There are 2 best solutions below

4
On

As describes in the readme, add the following dependency to the POM:

<dependency>
  <groupId>com.lesfurets</groupId>
  <artifactId>jenkins-pipeline-unit</artifactId>
  <version>1.0</version>
  <scope>test</scope>
</dependency>
2
On

I was facing the same problem that the tests were not running.

<plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.6</version>
        <executions>
          <execution>
            <goals>
              <goal>addSources</goal>
              <goal>addTestSources</goal>
              <goal>compile</goal>
              <goal>compileTests</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <sources>
            <source>
              <directory>${project.basedir}/src</directory>
              <includes>
                <include>**/*.groovy</include>
              </includes>
            </source>
            <source>
              <directory>${project.basedir}/vars</directory>
              <includes>
                <include>**/*.groovy</include>
              </includes>
            </source>
          </sources>
          <testSources>
            <testSource>
              <directory>${project.basedir}/src/test/groovy</directory>
              <includes>
                <include>*.groovy</include>
              </includes>
            </testSource>
          </testSources>
        </configuration>
      </plugin>

I am using gmavenplus plugin to run those tests and I had to also add this dependency

<dependency>
  <groupId>org.codehaus.groovy</groupId>
  <artifactId>groovy-all</artifactId>
  <version>2.4.11</version>
  <scope>test</scope>
</dependency>

After adding the two above, I could run the tests. HTH