Setting up Pylint tests for a Django Application
Priya SrinivasanIn this article, I will be walking you through the procedure of installing Pylint which is a widely used code analysis tool that helps identify any potential issues in the code, helps to improve the overall quality of the code by enforcing coding conventions. Maintaing code quality and establishing and following coding standards are very important when it comes to developing any software application. Pylint helps us to achieve this.
Two software packages I found while writing this article are:
- pylint-django, a “Pylint plugin for improving code analysis when analysing code using Django.”
- Prospector: a tool that makes use of Pylint and other code analysis tools for a more comprehensive analysis of Python code.
Step 1: Activate your Virtual Environment
It’s always a good practice to isolate the project’s dependencies in a virtual environment. To create one:
python3 -m venv venv
(Depending on your platform, python
might be the command you use to launch
Python 3. But don’t use Python 2; it is no longer supported.)
Once created, the virtual environment can be activated with the following command:
. venv/bin/activate
Step 2: Install Pylint
Pylint is available at the Python Package Index and can be
installed with pip
: pip install pylint
. This will install it into your
virtual environment.
Step 3: Install Dependencies
Any dependencies required for the application may be installed using
requirements.txt
file with the following command:
pip install -r requirements.txt
Since pylint
is useful for testing, but is not required for the
application’s execution, we have a separate file associated with our testing
stuff. This file is tests/requirements.txt
and is loaded in the above
fashion for test runs. It is a good idea to add Pylint to that file.
Tip: As Pylint is updated fairly regularly, new tests are developed which may flag problems in already-existing code. So if your testing starts with installing these dependencies in a clean environment, which is common in CI environments, a new version of Pylint can unexpectedly flag an error in code that was not updated in the pushed commits, which can be a headache. Our practice is to lock the version of Pylint, and then periodically update it and deal with any new errors right then. You can do this in your requirements file with, for example,
pylint==2.13.5
.
Step 4: Configure Pylint
Create a .pylintrc
configuration file in your project’s root directory. This
file will contain all the configuration settings. Here’s one we’ve used:
[MASTER]
generated-members=objects
python-path=your_project_path
# Disable some Pylint warnings. Keep it minimal and document the reasons.
# 'fixme': We should flag TODOs another way as sometimes they are for revisiting
# in future development, not in this cycle.
disable=fixme
# Customize other sections according to your project's needs
your_project_path
should be replaced with actual path to the Django project.
We use a simple test suite that runs Pylint for us. However, this is not Django-aware and we have had to implement a couple of exceptions that might not be necessary if we used the Django-specific plugin.
Step 5: Run Pylint on Your Django Application
To test the application now with the configured Pylint tests, run:
pylint your_app_name/
Step 6: Integrate with CI/CD Pipeline
The next step is to integrate Pylint tests into your testing pipeline. This helps in ensuring that code quality checks are performed before code is merged or deployed. An example configuration of the Gitlab CI would look like:
stages:
- linting
linting:
stage: linting
script:
- pylint your_app_name/
Step 7: Review and Improve Code Quality
Once the Pylint tests are executed, one should carefully review the errors shown or the output for potential issues or warnings, and resolve them by making necessary changes in the code. This way Pylint helps to enforce coding standards and best practices.
Conclusion
Setting up Pylint tests for your Django application can improve the development process by identifying and rectifying code issues in the beginning itself. By integrating Pylint into the workflow, one can improve the code quality and adhere to the coding standards.