Django 测试登录失败,除非一个完全不相关的、完全琐碎的函数被注释掉或重命名



我的 Django 测试代码中遇到了一个奇怪的错误。

完整代码:

from .models                    import MIN_BIRTH_YEAR
from .models                    import UserProfile
from django.contrib.auth.models import User
from django.test                import TestCase
import factory
TEST_USERS = []
TEST_PASSWORD = 'password123abc'
class UserProfileFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = UserProfile
    birth_year = factory.Sequence(lambda n: n + MIN_BIRTH_YEAR - 1)
class UserFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = User
    profile = factory.RelatedFactory(UserProfileFactory, 'user')
    username = factory.Sequence(lambda n: 'test_username{}'.format(n))
    first_name = factory.Sequence(lambda n: 'test_first_name{}'.format(n))
    last_name = factory.Sequence(lambda n: 'test_last_name{}'.format(n))
    email = factory.Sequence(lambda n: 'test_email{}@example.com'.format(n))
    password = factory.PostGenerationMethodCall('set_password', TEST_PASSWORD)
def create_insert_test_users():
    for i in range(5):
        TEST_USERS.append(UserFactory.create())
def _test_one_logged_in_user(test_instance, test_user_index):
    """
    In addition to public information, private content for a single
    logged-in user should be somewhere on the page.
    """
    test_instance.client.logout()
    test_user = TEST_USERS[test_user_index]
    print('Attempting to login:')
    profile = test_user.profile
    print('test_user.id=' + str(test_user.id))
    print('   username=' + test_user.username + ', password=' + TEST_PASSWORD)
    print('   first_name=' + test_user.first_name + ', last_name=' + test_user.last_name)
    print('   email=' + test_user.email)
    print('   profile=' + str(profile))
    print('      profile.birth_year=' + str(profile.birth_year))

继续。这是我正在谈论的登录行。此_test_one_logged_in_user函数由下面的倒数第二行(_test_one_logged_in_user(self, 0)(调用:

    did_login_succeed = test_instance.client.login(
        username=test_user.username,
        password=TEST_PASSWORD)
    test_instance.assertTrue(did_login_succeed)
    ##########################################
    # GET PAGE AND TEST ITS CONTENTS HERE...
    ##########################################
class MainPageTestCase(TestCase):
    """Tests for the main page."""
    def setUp(self_ignored):
        """Insert test users."""
        create_insert_test_users()
    def test_true_is_true(self):
        """Public information should be somewhere on the page."""
        self.assertTrue(True)
    def test_logged_in_users(self):
        """
        In addition to public information, private content for logged in
        users should also be somewhere on the page.
        """
        _test_one_logged_in_user(self, 0)
        _test_one_logged_in_user(self, 1)

这工作正常。一切都过去了。但是将test_true_is_true的名称更改为test_content_not_logged_in

def test_content_not_logged_in(self):
    """Public information should be somewhere on the page."""
    self.assertTrue(True)

test_instance.client.login现在返回False...这导致它下面的断言

test_instance.assertTrue(did_login_succeed)

失败:AssertionError: False is not true 。但是,如果您注释掉整个函数,它将成功(登录返回True(。

# def test_content_not_logged_in(self):
#     """Public information should be somewhere on the page."""
#     self.assertTrue(True)

如果您取消注释并将其重命名为以下任何一种,它将起作用:

  • test_xcontent_not_logged_in
  • test__content_not_logged_in
  • test_not_logged_in

其中任何一个,它都失败了:

  • test_ctrue_is_true
  • test_cxontent_not_logged_in
  • test_contentnot_logged_in
  • test_contennot_logged_in
  • test_contenot_logged_in
  • test_contnot_logged_in
  • test_connot_logged_in
  • test_cnot_logged_in
  • test_c

(我搜索了test_c,发现了一些东西,但没有发现任何特别的东西。

这似乎意味着setUp函数为test_content_not_logged_in(平凡函数(运行一次,然后再次运行一次test_logged_in_users。而这种运行两次会导致问题。所以我更改了它,以便仅在TEST_USER数组为空时才创建用户:

def create_insert_test_users():
    if  len(TEST_USERS) == 0:
        for i in range(5):
            TEST_USERS.append(UserFactory.create())

但它仍然失败,我可以确认它失败了,用户的 id 为 1:

$ python -Wall manage.py test auth_lifecycle.test__view_main2
/home/jeffy/django_files/django_auth_lifecycle_venv/lib/python3.4/site.py:165: DeprecationWarning: 'U' mode is deprecated
  f = open(fullname, "rU")
/home/jeffy/django_files/django_auth_lifecycle_venv/lib/python3.4/imp.py:32: PendingDeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  PendingDeprecationWarning)
Creating test database for alias 'default'...
.Attempting to login:
test_user.id=1
   username=test_username1, password=password123abc
   first_name=test_first_name1, last_name=test_last_name1
   email=test_email1@example.com
   profile=test_username1
      profile.birth_year=1887
F
======================================================================
FAIL: test_logged_in_users (auth_lifecycle.test__view_main2.MainPageTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/jeffy/django_files/django_auth_lifecycle/auth_lifecycle/test__view_main2.py", line 74, in test_logged_in_users
    _test_one_logged_in_user(self, 0)
  File "/home/jeffy/django_files/django_auth_lifecycle/auth_lifecycle/test__view_main2.py", line 53, in _test_one_logged_in_user
    test_instance.assertTrue(did_login_succeed)
AssertionError: False is not true
----------------------------------------------------------------------
Ran 2 tests in 0.385s
FAILED (failures=1)
Destroying test database for alias 'default'...

models.py:

"""Defines a single extra user-profile field for the user-authentication
    lifecycle demo project:
    - Birth year, which must be between <link to MIN_BIRTH_YEAR> and
    <link to MAX_BIRTH_YEAR>, inclusive.
"""
from datetime                   import datetime
from django.contrib.auth.models import User
from django.core.exceptions     import ValidationError
from django.db                  import models
OLDEST_EVER_AGE     = 127  #:Equal to `127`
YOUNGEST_ALLOWED_IN_SYSTEM_AGE = 13   #:Equal to `13`
MAX_BIRTH_YEAR      = datetime.now().year - YOUNGEST_ALLOWED_IN_SYSTEM_AGE
"""Most recent allowed birth year for (youngest) users."""
MIN_BIRTH_YEAR      = datetime.now().year - OLDEST_EVER_AGE
"""Most distant allowed birth year for (oldest) users."""
def _validate_birth_year(birth_year_str):
    """Validator for <link to UserProfile.birth_year>, ensuring the
        selected year is between <link to OLDEST_EVER_AGE> and
        <link to MAX_BIRTH_YEAR>, inclusive.
        Raises:
            ValidationError: When the selected year is invalid.
        https://docs.djangoproject.com/en/1.7/ref/validators/
        I am a recovered Hungarian Notation junkie (I come from Java). I
        stopped using it long before I started with Python. In this
        particular function, however, because of the necessary cast, it's
        appropriate.
    """
    birth_year_int = -1
    try:
        birth_year_int = int(str(birth_year_str).strip())
    except TypeError:
        raise ValidationError(u'"{0}" is not an integer'.format(birth_year_str))
    if  not (MIN_BIRTH_YEAR <= birth_year_int <= MAX_BIRTH_YEAR):
        message = (u'{0} is an invalid birth year.'
                   u'Must be between {1} and {2}, inclusive')
        raise ValidationError(message.format(
            birth_year_str, MIN_BIRTH_YEAR, MAX_BIRTH_YEAR))
    #It's all good.
class UserProfile(models.Model):
    """Extra information about a user: Birth year.
        ---NOTES---
        Useful related SQL:
            - `select id from auth_user where username <> 'admin';`
            - `select * from auth_lifecycle_userprofile where user_id=(x,x,...);`
    """
    # This line is required. Links UserProfile to a User model instance.
    user = models.OneToOneField(User, related_name="profile")
    # The additional attributes we wish to include.
    birth_year = models.IntegerField(
        blank=True,
        verbose_name="Year you were born",
        validators=[_validate_birth_year])
    # Override the __str__() method to return out something meaningful
    def __str__(self):
        return self.user.username

更改测试的名称时,也会更改测试的运行顺序。test_logged_in_users 方法在 test_true_is_true 之前运行,但在test_c_whatever之后运行(大概是因为它以 alpha 或某种顺序运行它们(。这就是为什么你看到名字变化的奇怪之处。

如您所了解,您的 setUp 方法针对每个测试用例运行。 首次运行 setUp 时,将创建用户并将其保存到数据库和TEST_USERS。运行第二个测试时,将刷新数据库,并删除所有用户。由 TEST_USERS 表示的用户(仍在列表中,因为全局变量跨测试用例保留(不再存在于数据库中。

您可以通过重置原始代码来使测试通过原始代码TEST_USERS,如下所示:

def create_insert_test_users():
    # global tells python to use the TEST_USERS above, not create a new one
    global TEST_USERS
    TEST_USERS = []
    # Your code here...

现在,TEST_USERS表示与数据库中的用户匹配的新真实用户。不过,一般来说,全局变量是一个坏主意(出于多种原因,您在其中遇到的困惑(。即时创建它们(正如您在最新更新中所做的那样(是一个更好的解决方案。

TestCase 将通过查找以 test 开头的方法识别所有测试

从文档:

各个测试是使用名称以开头的方法定义的 用字母测试。此命名约定通知测试运行程序 关于哪些方法表示测试。

因此,当您重命名a_trivial_function时,它是否会将其视为测试。

原始代码在本地存储TEST_USERS,并且在测试之间共享时,静态持有的对象似乎会导致问题。我天真地认为在本地存储对象很重要,以便将数据库值与它进行比较。这意味着我不相信 Django 或 Factory Boy 会正确地将它们插入数据库,他们可以很好地处理这个问题。

下面是更新的代码,它仅将对象存储在数据库中。我还将包含login的内容子函数直接移动到底部函数中。

from .models                    import MIN_BIRTH_YEAR
from .models                    import UserProfile
from django.contrib.auth.models import User
from django.test                import TestCase
import factory
TEST_PASSWORD = 'password123abc'
TEST_USER_COUNT = 5
class UserProfileFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = UserProfile
    birth_year = factory.Sequence(lambda n: n + MIN_BIRTH_YEAR - 1)
class UserFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = User
    profile = factory.RelatedFactory(UserProfileFactory, 'user')
    username = factory.Sequence(lambda n: 'test_username{}'.format(n))
    #print('username=' + username)
    first_name = factory.Sequence(lambda n: 'test_first_name{}'.format(n))
    last_name = factory.Sequence(lambda n: 'test_last_name{}'.format(n))
    email = factory.Sequence(lambda n: 'test_email{}@example.com'.format(n))
    password = factory.PostGenerationMethodCall('set_password', TEST_PASSWORD)
class MainPageTestCase(TestCase):
    """Tests for the main page."""
    def setUp(self_ignored):
        """Insert test users."""
        #print('a User.objects.count()=' + str(User.objects.count()))
        #http://factoryboy.readthedocs.org/en/latest/reference.html?highlight=create#factory.create_batch
        UserFactory.create_batch(TEST_USER_COUNT)
        #print('b User.objects.count()=' + str(User.objects.count()))

继续:

    def test_ctrue_is_true(self):
        """Public information should be somewhere on the page."""
        self.assertTrue(True)
    def test_some_logged_in_users(self):
        """
        In addition to public information, private content for logged in
        users should also be somewhere on the page.
        """
        for  n in range(2):
            self.client.logout()
            test_user = UserFactory()
            print('Attempting to login:')
            profile = test_user.profile
            print('test_user.id=' + str(test_user.id))
            print('   username=' + test_user.username + ', password=' + TEST_PASSWORD)
            print('   first_name=' + test_user.first_name + ', last_name=' + test_user.last_name)
            print('   email=' + test_user.email)
            print('   profile=' + str(profile))
            print('      profile.birth_year=' + str(profile.birth_year))
            did_login_succeed = self.client.login(
                username=test_user.username,
                password=TEST_PASSWORD)
            self.assertTrue(did_login_succeed)
            ##########################################
            # GET PAGE AND TEST ITS CONTENTS HERE...
            ##########################################

最新更新