Introduction
It is very important to introduce few process so that your code and build maintain its quality. In Python projects, two of best way is to use:
- Pylint
- Unittest with Coverage
Where Pylint gives you a static analysis around python code best practices, and gives you errors around issues there. Unit-tests are always a good idea to write in your code. They keep your code in healthy state, if you are enforcing to write good number of test cases. With coverage numbers, you can restrict by how many percentage of your code should be covered.
Pylint with Restriction
When you run pylint command normally on a folder or a file/module. It gives you a score. But, it does not fail anything. So, how can you put restrictions.
Pylint provides an option --fail-under
pylint --fail-under=8.0 **/*.pyAbove command will give you failure, if your pylint score falls below 8.0
You can set this score as a variable in shell script as:
export warningsThresholdPylint=8.0
pylint --fail-under=${warningsThresholdPylint} **/*.pyUnittests Coverage Restriction
You need to install a python module: coverage.
This module also gives you a flag --fail-under, below which it will fail. It checks if your code coverage percentage falls below this number.
export warningsThresholdCoverage=70
coverage run -m unittest tests/*.py
coverage report -m --fail-under=${warningsThresholdCoverage} **/*.pySummary
We often tend to ignore these small practices which makes your build process healthy. You can put these scripts in your Jenkins pipeline, and stop build going ahead if it falls below your set standards. These are few of the best practices that we should follow in our python projects.
In next post, I will show how you can write these scripts in Jenkin pipeline groovy script.













