0
0
Jenkinsdevops~5 mins

Testing shared libraries in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Shared libraries in Jenkins help reuse code across multiple pipelines. Testing them ensures your reusable code works correctly before using it in real pipelines.
When you create common functions or steps to use in many Jenkins pipelines.
When you want to avoid repeating the same code in multiple Jenkinsfiles.
When you update shared library code and want to verify it still works.
When you want to catch errors early before running full pipelines.
When you want to write automated tests for your pipeline helper functions.
Config File - vars/mySharedLib.groovy
vars/mySharedLib.groovy
def call(String name) {
    echo "Hello, ${name}!"
}

This file defines a simple shared library function named mySharedLib. It takes a name and prints a greeting. This is the code you want to test.

Commands
Create a directory to hold your test files for the shared library.
Terminal
mkdir -p src/test/groovy
Expected OutputExpected
No output (command runs silently)
Create a simple JUnit test file to test the shared library function. It checks if the function returns the expected greeting.
Terminal
echo 'import org.junit.*\nimport static org.junit.Assert.*\nimport org.jenkinsci.plugins.workflow.cps.*\n\nclass MySharedLibTest {
    @Test
    void testCall() {
        def script = new GroovyShell().parse("""
            def call(name) {
                return "Hello, ${name}!"
            }
        """)
        def result = script.call('World')
        assertEquals('Hello, World!', result)
    }
}' > src/test/groovy/MySharedLibTest.groovy
Expected OutputExpected
No output (command runs silently)
Run the tests using Gradle to verify the shared library code works as expected.
Terminal
gradle test
Expected OutputExpected
BUILD SUCCESSFUL in 2s 3 actionable tasks: 3 executed > Task :test MySharedLibTest > testCall PASSED BUILD SUCCESSFUL
Key Concept

If you remember nothing else, remember: always write and run tests for your shared library code to catch errors early and keep pipelines reliable.

Common Mistakes
Not creating a proper test directory structure for shared library tests.
Jenkins and Gradle expect tests in specific folders; missing this causes tests not to run.
Create tests under src/test/groovy to ensure they are discovered and executed.
Writing tests that do not import necessary Jenkins or Groovy classes.
Missing imports cause compilation or runtime errors in tests.
Always import required classes like org.junit.* and Jenkins pipeline classes.
Running tests without a proper build tool setup like Gradle.
Tests won't run or fail silently without a build tool managing dependencies and execution.
Use Gradle or another build tool configured for Jenkins shared library testing.
Summary
Create your shared library code in vars/ or src/ folders.
Write tests in src/test/groovy using JUnit to verify your shared library functions.
Run tests with Gradle to catch errors before using the library in pipelines.