# Simple Django Tip #3

My [previous tip](https://hellosambhavi.com/simple-django-tip-2) 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.

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

### Setup related to pytest

The next step is to install `pytest-django`

```python
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`

```python
[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.

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">If you use the default settings.py then the value for <code>DJANGO_SETTINGS_MODULE</code> will be <code>&lt;your-project-name&gt;.settings</code></div>
</div>

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 👇

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698855229111/1cde6fa9-7ff5-4502-ad42-c002818c4b4b.png align="center")

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 👇

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698855127194/44b15028-cfaf-445e-ae1a-a0197175f2d6.png align="center")

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](https://docs.pytest.org/en/7.1.x/how-to/usage.html#specifying-which-tests-to-run) 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.
