1

I want to include tests for my Django application. After reading several postings about unittesting vs. integration testing (especially this SO posting), I'm unsure about the following situation:

Unit tests are the sole tests that tell you where exactly the bug is. To draw this information, they must run the method in a mocked environment, where all other dependencies are supposed to correctly work.

While testing my forms (in fact ModelForms), I rely on Django's feature to provide test-data by a fixture. Therefore, my form tests make use of normal Django methods like Foo.objects.get().

However, the above linked SO posting suggests to perform unittests without relying on external components. So should I drop the fixtures for unittests and use them only in integration tests?

Here's an example of my current testing setup for forms:

from django.test import TestCase

class FooTest(TestCase):
    """Base class for all app related tests"""
    fixtures = ['testdata.json']

class BarFormTest(FooTest):
    """Tests for the BarForm"""

    def test_constructor(self):
        """Tests if the form stores the supplied user"""

        # this is the line I'm unsure of:
        u = User.objects.get(username='TestUser01')
        # is this better?
        # u = User(username='TestUser01')

        form = BarForm(data=None, user=u)

        self.assertEqual(u, form.user)

Don't get me wrong, my tests are working as expected. I'm just unsure if I can rely on external (Django built-in) functions. They are tested aswell, so any error in my tests will most likely originate in my code.

Should I spare the fixtures for integration tests? I hope this question is not too broad, I'm asking about Django best-practices here.

Community
  • 1
  • 1
Mischback
  • 843
  • 5
  • 18

1 Answers1

0

This depends on how certain you want to be, that you can trust your unittests.

If you don't trust django's internal functions you shouldn't use django, but instead use pure python in your entire project. If you don't trust python's internal functions you shouldn't use python, but C. If you don't trust C you should use assembler. …

There are unittests included in django itself that make sure every release works as expected. So you can be safe that the ORM works as expected.

Chris Schäpers
  • 1,238
  • 10
  • 17
  • 1
    I like that chain of trust. You're possibly right: You have to trust some code. +1, but I will leave this question open to encourage some more answers. – Mischback Aug 02 '15 at 14:49