Invoking Pylint Programmatically for Unit Testing

Invoking Pylint Programmatically for Unit Testing

21 May 2024 Stephan Petzl Leave a comment Tech-Help

Incorporating static code analysis tools like Pylint into your unit testing can significantly enhance the quality of your code by catching errors early. This guide will walk you through how to invoke Pylint programmatically to check for errors within your codebase.

Understanding the Basics

Pylint is a popular static code analysis tool that can identify errors, enforce coding standards, and suggest improvements. While Pylint is typically run from the command line, you can also invoke it programmatically within your Python scripts. This is particularly useful for integrating Pylint checks into your automated testing pipeline.

Simple Invocation Example

The most straightforward way to invoke Pylint programmatically is to use the pylint.lint.Run class. Here’s a minimal example:

from pylint.lint import Run
Run(['--errors-only', ''])

This snippet will run Pylint on and limit the output to errors only.

Advanced Usage

For more advanced usage, such as capturing Pylint’s output programmatically, you can use a custom reporter. Below is an example of how to achieve this:

class WritableObject:
    def __init__(self):
        self.content = []
    def write(self, st):
    def read(self):
        return self.content

def run_pylint(filename):
    from pylint import lint
    from pylint.reporters.text import TextReporter
    ARGS = ["-r", "n", "--rcfile=rcpylint"]
    pylint_output = WritableObject()
    lint.Run([filename] + ARGS, reporter=TextReporter(pylint_output), exit=False)
    for l in


This example defines a WritableObject class to capture Pylint’s output and then uses it within the run_pylint function. This method is more efficient and allows for further processing of the output.

Alternative Approach Using StringIO

Instead of creating a custom writable object, you can use Python’s StringIO to capture the output:

import sys
from io import StringIO
from pylint import lint

stdout = sys.stdout
sys.stdout = StringIO()

ARGS = ["-r", "n", "--rcfile=rcpylint"]
r = lint.Run([''] + ARGS, exit=False)

output = sys.stdout.getvalue()
sys.stdout = stdout


This approach simplifies the process by leveraging existing libraries to capture and manipulate the output.

Handling Pylint Interface Changes

Be aware that Pylint’s interface can change over time. For instance, the exit parameter was replaced with do_exit in newer versions. Ensure you are using the correct parameters for your version of Pylint.

Integrating with CI/CD Pipelines

To integrate Pylint into your CI/CD pipeline, you can create a wrapper script that checks the linting score and exits with an appropriate status code. Here’s an example:

import sys
from pylint import lint

ARGS = ["--fail-under=8"]
run = lint.Run(ARGS, do_exit=False)

score = run.linter.stats['global_note']
if score < 8:

This script ensures that your CI/CD pipeline will fail if the Pylint score is below a certain threshold.

Enhancing Testing Efficiency with Repeato

While integrating tools like Pylint can significantly improve your code quality, managing and maintaining these tools can be cumbersome. This is where Repeato can make a difference. Repeato is a no-code test automation tool for iOS and Android that allows you to create, run, and maintain automated tests for your apps efficiently. Its intuitive test recorder and scripting interface make it an excellent choice for both novice and advanced testers.

Moreover, Repeato’s ability to test websites inside an Android emulator or device, with explicit web testing support coming soon, can streamline your testing process even further. For more details, visit our documentation page.

Like this article? there’s more where that came from!