Sunday, March 13, 2022

Re: How Important Is Writing Unit Tests For Django Applications?


This topic is always interesting for me.

What is an integration test?

First, I'm always confused about what "integration tests" means. Apparently I'm not the only one. Here's what Edd Yerburgh says in his excellent book, "Testing Vue.js Applications" (p. 9):

People define integration tests differently, especially on the frontend. Some think tests that run in a browser environment are integration tests. Some think any test that tests a unit with module dependencies is an integration test. Some think that any fully rendered component is an integration test.

His definition of end-to-end tests is also relevant (p. 5):

In frontend applications, end-to-end tests automate a browser to check that an application works correctly from the user's perspective.

An additional complication is that people often use the term "unit testing" as all encompassing, that is for any kind of automated testing. This is especially true in Python, where the module for automating testing is called "unittest", and even more so in Django, where its TestCase subclasses contain functionality which some people would consider inappropriate for "unit tests" and appropriate only for "end-to-end" tests or "integration" tests, whatever that means.

On the other hand, why is a Django view NOT a unit of functionality? I think it is. Therefore why testing a Django view isn't unit testing? I don't really know, this is just food for thought. I think I'd talk about low-level testing and high-level testing, this labeling of end-to-end vs. unit doesn't always work well I think.

How to do a high level test

def add(a, b):
    return a + b

def multiply(i, j):
    result = 0
    while i:
        result = add(result, j)
        i -= 1
    return result

These are very stupid and I haven't tried them at all but it doesn't matter, the point I'm trying to make is that "multiply" is my high level function here, because it is calling "add", which is a lower level function.

How do you test "multiply"? A purist might say that you have tested "add" with some unit tests, and that when testing "multiply" you should mock "add". This way, if a unit test for "multiply" fails, you know the problem was in "multiply" and not in "add".

But, frankly, mocking is such a pain that I avoid doing it unless doing it without mocking would be harder. That is, my rule of thumb is to mock or not to mock based on how easy it will be to write a test. If you don't mock, an error in "add" should cause some "add" unit tests AND some "multiply" unit tests to fail. Usually it won't be hard to locate and fix the error.

At other times, I don't do low level testing at all. If the low-level function is just an implementation detail of the high level function, then often I don't test the lower level.


The purpose of writing tests is usually to save time. It's really hard, if not impossible, to measure how much time and money you saved (or lost) by working one way vs. another. Ultimately we just have a feeling guide us. I feel ok with my testing experience, but I always wonder whether I'm testing insufficiently or whether I'm testing too much.

I didn't expect this reply to become that long, neither that I would not answer the question in the end. I hope it was helpful anyway. Good luck!

Antonis Christofides  +30-6979924665 (mobile)

On 12/03/2022 20.00, Daniel Coker wrote:
I understand the importance of unit testing. However, if we are going to write integration tests (testing the views), this test will step through all of the modules that we would otherwise unit test.

So, I'm wondering, it it really necessary to write unit test for Django Applications when we will still test the views?
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

No comments:

Post a Comment