How Important Is Writing Unit Tests For Django Applications?

54 views
Skip to first unread message

Daniel Coker

unread,
Mar 12, 2022, 1:44:23 PM3/12/22
to Django users
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?

Mike Dewhirst

unread,
Mar 13, 2022, 3:01:00 AM3/13/22
to django...@googlegroups.com
1.    If you don't unit test your bits and pieces AND you call them in
your views they might pass with wrong values. Subtle errors can creep in
if your views are tested too coarsely.

2.    The more fine-grained the unit tests the easier it is to refactor
your view code.

3.    Do you use Coverage? You will see all the Django code is tested.
Only your own code to test

I do integration testing at a high level as well as down low. Really
does wonders for refactoring confidence.

YMMV

Mike

> --
> 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 django-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/d0cf2d7c-79cb-41a5-801a-4240c3c0c1c1n%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/d0cf2d7c-79cb-41a5-801a-4240c3c0c1c1n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Just
ask and I'll send it to you. Your email software can handle signing.

OpenPGP_signature

Antonis Christofides

unread,
Mar 13, 2022, 5:56:38 AM3/13/22
to django...@googlegroups.com

Hello,

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.

Conclusion

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 django-users...@googlegroups.com.

Carsten Fuchs

unread,
Mar 13, 2022, 8:41:26 AM3/13/22
to django...@googlegroups.com
Am 13.03.22 um 10:53 schrieb Antonis Christofides:
> This topic is always interesting for me.
>
> [...]
> 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!
>

A great text! Thank you!

Best regards,
Carsten

Daniel Coker

unread,
Mar 14, 2022, 2:30:59 AM3/14/22
to Django users
Thanks for your response, Antonis.

Would you recommend testing views using the Client or RequestFactory classes?
Reply all
Reply to author
Forward
0 new messages