~/python/how-maintain-quality-build-process-pylint-unittest-coverage-threshold.md

Python Build Quality Gates with Pylint and Unittest Coverage Thresholds

2 min readMay 10, 2022by Gorav Singal#python#pytest#unittest
// TL;DR

Set minimum pylint score and unittest coverage percentage thresholds in your Python build pipeline to fail builds that don't meet quality standards.

Python Build Quality Gates with Pylint and Unittest Coverage Thresholds

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 **/*.py

Above 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} **/*.py

Unittests 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} **/*.py

Summary

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.

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

A Jenkins Groovy Pipeline for Building Python Projects and Pushing to Artifactory

A Jenkins Groovy Pipeline for Building Python Projects and Pushing to Artifactory

Introduction In this post, we will see a sample Jenkin Pipeline Groovy script…

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

Introduction In this post, we will see how to fetch multiple credentials and…

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Introduction I have an automation script, that I want to run on different…

Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Introduction I had to write a CICD system for one of our project. I had to…

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction There are some cases, where I need another git repository while…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…