Search code examples
djangounit-testingdjango-viewsdjango-testingdjango-tests

for loop in django unittest


I want to test register view in django project,

so I build some fake test cases(self.correct_samples)

after register successfully, it should redirect to home page which means the status code should be 302.

from django.test import TestCase
from django.urls.base import reverse


class RegisterTests(TestCase):

    def setUp(self):
        url = reverse('account:register')
        self.response = self.client.get(url)

        self.correct_samples = (
            ('[email protected]', 'testuser', 'test112233', 'test112233'),
            ('[email protected]', 'fake123', 'fakeuser111', 'fakeuser111'),
            ('[email protected]', 'Jack', 'myfavorite', 'myfavorite'),

            ('failemail', 'Jack', 'myfavorite', 'myfavorite'),  # fail for purpose
        )

    def test_register_form(self):

        for test_case in self.correct_samples:

            email, username, password1, password2 = test_case
            self.response = self.client.post(reverse('account:register'), data={
                'email': email,
                'username': username,
                'password1': password1,
                'password2': password2,
            })

            self.assertEqual(self.response.status_code, 302)
            self.assertRedirects(
                self.response, expected_url='/', status_code=302, target_status_code=200)

The fourth data in self.correct_samples which is ('failemail', 'Jack', 'myfavorite', 'myfavorite') should be a fail case.

but after python manage.py test. It passed.

(env) C:\Users\User\myblog>python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
C:\Users\User\myblog\env\lib\site-packages\whitenoise\base.py:115: UserWarning: No directory at: C:\Users\User\myblog\staticfiles\
  warnings.warn(u"No directory at: {}".format(root))
.
----------------------------------------------------------------------
Ran 1 test in 0.195s

OK
Destroying test database for alias 'default'...

Here comes the tricky thing,

it failed after switching order from fourth to first.

from django.test import TestCase
from django.urls.base import reverse


class RegisterTests(TestCase):

    def setUp(self):
        ...

        self.correct_samples = (
            ('failemail', 'Jack', 'myfavorite', 'myfavorite'),  # fail for purpose

            ('[email protected]', 'testuser', 'test112233', 'test112233'),
            ('[email protected]', 'fake123', 'fakeuser111', 'fakeuser111'),
            ('[email protected]', 'Jack', 'myfavorite', 'myfavorite'),
        )

    def test_register_form(self):
        ...

result:

(env) C:\Users\User\myblog>python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
C:\Users\User\myblog\env\lib\site-packages\whitenoise\base.py:115: UserWarning: No directory at: C:\Users\User\myblog\staticfiles\
  warnings.warn(u"No directory at: {}".format(root))
F
======================================================================
FAIL: test_register_form (account.tests.RegisterTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\User\myblog\account\tests.py", line 34, in test_register_form
    self.assertEqual(self.response.status_code, 302)
AssertionError: 200 != 302

----------------------------------------------------------------------
Ran 1 test in 0.026s

FAILED (failures=1)
Destroying test database for alias 'default'...

Why this happened?

I have searched for related keywords like unittest in forloop, multiple testcases in forloop.

but it seems no answers for it, or maybe search through other keywords?

or something I missed or misunderstood?

thanks for helping.


Solution

  • Problem has been solved!

    According to this questions How do you generate dynamic (parameterized) unit tests in Python?

    Solution: parameterized