It’s common practice to perform an extensive testing on a Pull Request before the code is being deployed to production systems.
So far we’ve seen how pipeline can be built and run around a single Git branch (e.g. main
or dev
). Now we would like to create a new pipeline which will be triggered on every PR that is created in GitHub.
For that we will utilize Jenkins multi-branch pipeline.
pull-request-testing
.PullRequest.Jenkinsfile
.main
branch, create the PullRequest.Jenkinsfile
as follows:
pipeline {
agent any
stages {
stage('Unittest') {
steps {
echo "testing"
}
}
stage('Lint') {
steps {
echo "linting"
}
}
stage('Functional test') {
steps {
echo "testing"
}
}
}
}
From now one, we would like to protect branch main
from being merged by non-tested and reviewed branch.
main
branch as follows:
continuous-integration/jenkins/branch
check done by Jenkins.Your main
branch is now protected and no code can be merged to it unless the PR is reviewed by other team member and passed all automatic tests done by Jenkins.
Let’s implement the pull request testing pipeline.
main
create a new branch called sample_feature
which simulates some new feature that a developer is going to develop. Push the branch to remote.sample_feature
into main
.Unittest is a testing framework in Python that allows developers to write and run small, isolated tests for individual units of code to ensure their correctness and detect any potential bugs or issues.
In the projects/app_development_I/yolo5
directory, you are given directory called tests
. This is a common name for the directory containing all unittests files. The directory contains a file called test_allowed_file.py
which implements unittest for the allowed_file
function in utils.py
file.
pip install -r requirements.txt
), check that all tests are passed:
python3 -m pytest --junitxml results.xml tests
Integrate the unittesting in PullRequest.Jenkinsfile
under the Unittest stage.
post
step to display the tests results in the readable form:
post {
always {
junit allowEmptyResults: true, testResults: 'results.xml'
}
}
Pylint is a static code analyser for Python. Pylint analyses your code without actually running it. It checks for errors, enforces a coding standard, and can make suggestions about how the code could be refactored.
pylint
locally if needed..pylintrc
file which is a configuration file defines how Pylint should behave
pylint --generate-rcfile > .pylintrc
python3 -m pylint *.py
How many warnings do you get? If you need to ignore some unuseful warning, add it to disable
list in [MESSAGES CONTROL]
section in your .pylintrc
file.
PullRequest.Jenkinsfile
under the Lint stage.steps {
sh 'python3 -m pylint -f parseable --reports=no *.py > pylint.log'
}
post {
always {
sh 'cat pylint.log'
recordIssues (
enabledForFailure: true,
aggregatingResults: true,
tools: [pyLint(name: 'Pylint', pattern: '**/pylint.log')]
)
}
}
comment: <> (curl -X POST -H "Content-Type: multipart/form-data" -F "file=@11.png" localhost:8081/api
)
Use parallel
directive to run the test stages in parallel, while failing the whole build when one of the stages is failed.