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 ModelForm
s), 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.
Aucun commentaire:
Enregistrer un commentaire