在 Django 单元测试中进行循环

for loop in django unittest

我想在 django 项目中测试寄存器视图,

所以我构建了一些假测试用例(self.correct_samples)

注册成功后,应该重定向到主页,即状态码应该是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 = (
            ('testuser1@email.com', 'testuser', 'test112233', 'test112233'),
            ('fakeuser@email.com', 'fake123', 'fakeuser111', 'fakeuser111'),
            ('correct@email.com', '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)

self.correct_samples中的第四个数据('failemail', 'Jack', 'myfavorite', 'myfavorite')应该是失败案例。

但在 python manage.py test 之后。通过了。

(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'...

棘手的事情来了,

将顺序从第四切换到第一后失败。

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

            ('testuser1@email.com', 'testuser', 'test112233', 'test112233'),
            ('fakeuser@email.com', 'fake123', 'fakeuser111', 'fakeuser111'),
            ('correct@email.com', 'Jack', 'myfavorite', 'myfavorite'),
        )

    def test_register_form(self):
        ...

结果:

(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'...

为什么会这样?

我搜索了相关关键字,例如 unittest in forloopmultiple testcases in forloop

但似乎没有答案,或者搜索其他关键字?

或者我错过或误解了什么?

感谢您的帮助。

问题已解决!

根据这个问题How do you generate dynamic (parameterized) unit tests in Python?

解决方案:参数化