Simple Django Tip #3

Photo by Alex Chumak on Unsplash

Simple Django Tip #3

Setup pytest for Django

ยท

3 min read

My previous tip was about .env file to store environment variables. This one is about configuring pytest in a Django project so that we can write test cases easily. pytest is an obvious choice when it comes to testing frameworks for pythonistas. A few top advantages:

  • Simple and compact syntax, just use assert statements and compare.

  • Extremely powerful and flexible - Tests can be parameterized as well as skipped. Fixtures can be added to reuse test data.

  • Informative traceback - The test output is verbose so that it becomes easy to understand where exactly the problem is.

  • With pytest-django, it can be seamlessly integrated with Django.

I generally write test cases in tandem with development, neither 100% upfront nor 100% later. It's always a bit of this and a bit of that.

Since this is going to be a short post, let's dive in.

Initial setup

Let's perform the usual steps of creating a virtual environment, followed by installing Django, and creating a project.

py -m venv <name of your virtual environment>
pip install Django
django-admin startproject <your-fancy-project-name-here>

The next step is to install pytest-django

pip install pytest-django

The next step would be to create a configuration file that holds properties related to pytest. Create a file named pytest.ini at the root of the project i.e. at the same level where manage.py exists.

Add the following properties in pytest.ini

[pytest]
DJANGO_SETTINGS_MODULE = <your-fancy-project-name-here>.settings.local
python_files = test_*.py

I always follow the step of creating multiple files for settings, one for each environment. You can map DJANGO_SETTINGS_MODULE to the appropriate settings file. In the above case, it's hardcoded to local.py. This denotes the local development environment.

๐Ÿ’ก
If you use the default settings.py then the value for DJANGO_SETTINGS_MODULE will be <your-project-name>.settings

Another option is to create an environment variable named DJANGO_SETTINGS_MODULE in .env file and invoke using os.environ.get("DJANGO_SETTINGS_MODULE")

The second property python_files denotes the file name format that needs to be identified as test files that need to be executed by pytest. In this case, file names that start with test_ followed by any text will be considered. It is up to the developer to define these name formats. test_* is one standard way of naming a test file.

Creating placeholder for testcases

The next step is to create a folder named tests. There are multiple approaches regarding the location of the tests folder. One option is to create a tests folder under each app and keep adding multiple test_* under it. Eg., one for test_models.py, one for test_e2e.py. This is what the folder structure looks like ๐Ÿ‘‡

Another option is to create tests at the root level of a project. We can then add folders, one for each app followed by multiple test_.py in each of those app folders. This is how the folder structure will look like ๐Ÿ‘‡

That's it when it comes to configuration.

Execute the testcases

To run the tests, we just navigate to the root of the folder and issue the command pytest. This will execute all the test files in the entire project. There are options to specify to execute the test files in a particular folder, even specific test cases.

Read here for different options that pytest provides to invoke execution.

Conclusion

Performing these steps in the initial phases of a Django project will prove to be lot useful. With its concise syntax, powerful fixture system, and extensive plugin support, pytest not only simplifies the test-writing process but also enhances the efficiency and clarity of test suites.

ย