#4 - Setting the stage to write testcases

Now that django is installed, what's next?


3 min read

In my previous post, we saw how to use pydanny's cookie cutter and create a django project. That's how I created mine.

I have my roadmap ready, which means I've decided on the apps that I will need. Apps in django is nothing but a self-contained module or component that encapsulates a specific functionality of a web application. It is meant to perform a specific task or provide a set of related tasks.

Although they are pluggable, I have seldom done that in the past. However generic we write, every project is unique and will have a set of requirements that vary from the others. One exception I can think of is the users app that more or less remains the same across projects. Of course, we can always borrow ideas from apps, or steal the code if it's your own ๐Ÿ˜…

Let's quickly see how to create an app in django.

python manage.py startapp app_name

It's as simple as that. Doing that creates an app with a bunch of files, ready to code. Let's say I created an app called chat. Here is how my folder structure looks like:

Note that urls.py will not be created by default.

After I created all the apps that were required, the next step I generally perform is to set the stage to write testcases on par with the application code. Practically speaking, I may not be able to follow Test driven development 100% by the book, but I do follow it with a bit of dilution.

In general, to write unit testcases, I use pytest. With django, I use pytest-django.Why would I use pytest-django? Here's a quote from their PyPi page

  • Less boilerplate: no need to import unittest, create a subclass with methods. Just write tests as regular functions.

  • Manage test dependencies with fixtures.

  • Run tests in multiple processes for increased speed.

  • There are a lot of other nice plugins available for pytest.

  • Easy switching: Existing unittest-style tests will still work without any modifications.

Let's now install the same using pip. By the way, I use pip for package management or installation, just keeping it simple. On ensuring my virtual environment is activated, I install the package like so.

pip install pytest-django

With pytest-django, you need to create a pytest.ini file as the same level as your manage.py. It needs to have the following

python_files = tests.py test_*.py *_tests.py

When we create a django project using cookie cutter, it also creates a test.py under the settings folder. We keep all test related properties nice and separate.

Note that this property should be config.settings if you just have settings.py

The second property python_files is actually optional as it considers these 3 values (tests.py test_*.py *_tests.py) by default. Still it is better to be explicit than implicit, one of the guiding principles of the Zen of python.

I recently learned that it is better to have an in memory database just for testing. Thus, below is the database property in my test.py

DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}}

Once these are set, I create a folder called tests at the same level as my other apps. I prefer to keep all tests in one place, rather than django's style of having a test.py under each app.

For readability, I create one folder for each app, and keep all test_* files there. Here is a quick snapshot of how it looks in my IDE today

journal is an app, users is another.

Now that the stage is set to write test cases, let's get going ๐Ÿƒโ€โ™€๏ธ