Django测试登录失败,除非注释掉或重命名完全不相关的,完全无关紧要的函数

时间:2014-10-24 14:30:03

标签: python django unit-testing

我的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

(我' ve searched for 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

3 个答案:

答案 0 :(得分:2)

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

正如您所知,您的setUp方法针对每个测试用例运行。当setUp第一次运行时,将创建用户并将其保存到DB和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代表与数据库中的用户匹配的新的真实用户。一般来说,全局变量是一个坏主意(for several reasons,你在其中经历的混乱)。即时创建它们(正如您在最新更新中所做的那样)是一个更好的解决方案。

答案 1 :(得分:1)

TestCase会通过查找以test

开头的方法来识别所有测试

来自文档:

  

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

因此,当您重命名a_trivial_function时,它会更改是否被视为测试。

答案 2 :(得分:0)

原始代码在本地存储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...
            ##########################################