0
0
Jenkinsdevops~10 mins

Testing shared libraries in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Testing shared libraries
Write shared library code
Write unit tests for library
Configure Jenkins pipeline to load library
Run tests in pipeline
Check test results
Fix issues or confirm success
This flow shows writing shared library code, adding tests, running them in Jenkins, and checking results.
Execution Sample
Jenkins
def add(a, b) {
  return a + b
}

// Test
assert add(2, 3) == 5
A simple shared library function add and a test assertion checking its output.
Process Table
StepActionEvaluationResult
1Call add(2, 3)2 + 35
2Assert add(2, 3) == 55 == 5True - Test passes
3Run pipeline stage 'Test'Tests executedAll tests passed
4Check test reportNo failuresSuccess
5Pipeline endsTests passedLibrary verified
💡 All tests passed, pipeline completes successfully
Status Tracker
VariableStartAfter Step 1After Step 2Final
aundefined222
bundefined333
resultundefined555
test_passedfalsefalsetruetrue
Key Moments - 2 Insights
Why does the test fail if the assertion is incorrect?
Because in step 2 of the execution_table, the assertion evaluates to false, causing the pipeline to mark the test as failed and stop or report errors.
How does Jenkins know to load the shared library for testing?
Jenkins pipeline configuration includes a 'library' step that loads the shared library before running tests, as implied in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of add(2, 3) at step 1?
A5
B23
CError
DUndefined
💡 Hint
Check the 'Result' column in row for Step 1 in execution_table
At which step does the test assertion confirm success?
AStep 4
BStep 1
CStep 2
DStep 5
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table for assertion evaluation
If the assertion failed, how would the pipeline result change in the table?
AStep 3 would show 'Tests executed' with all passed
BStep 4 would show 'Failures detected'
CStep 5 would show 'Library verified'
DNo change in any step
💡 Hint
Refer to the 'Check test report' step in execution_table and what happens on test failures
Concept Snapshot
Testing shared libraries in Jenkins:
- Write library code and unit tests
- Configure Jenkins pipeline to load the library
- Run tests inside pipeline stages
- Check test results for pass/fail
- Fix code if tests fail, else confirm success
Full Transcript
Testing shared libraries involves writing reusable code and unit tests for it. Jenkins pipelines load the shared library and run these tests automatically. Each step calls functions, evaluates assertions, and reports results. If tests pass, the pipeline completes successfully confirming the library works. If tests fail, Jenkins reports errors so you can fix the code. This visual trace shows calling a simple add function, asserting its output, running tests in Jenkins, and checking the final success status.