python - 如何使用 pytest 测试 Django 模型?

标签 python django pytest django-testing pytest-django

我正在开始使用 pytest。我已经配置了 pytest,无论如何我找不到关于使用 pytest 进行 Django 特定测试的资源。如何使用 pytest_django 测试模型?

我已经问了一个关于单元测试的问题,

how do I efficiently test this Django model?

我想知道如何使用 py.test 编写相同的测试?

在模型下方添加用 unittest 编写的测试。

被测模型是,

class User(AbstractBaseUser, PermissionsMixin):
    username = models.CharField(max_length=25, unique=True, error_messages={
        'unique': 'The username is taken'
    })
    first_name = models.CharField(max_length=60, blank=True, null=True)
    last_name = models.CharField(max_length=60, blank=True, null=True)
    email = models.EmailField(unique=True, db_index=True, error_messages={
        'unique': 'This email id is already registered!'
    })

    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)

    date_joined = models.DateTimeField(auto_now_add=True)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username',]


    objects = UserManager()

    def get_full_name(self):
        return ' '.join([self.first_name, self.last_name])

    def get_short_name(self):
        return self.email

    def __unicode__(self):
        return self.username

和编写的unittest

class SettingsTest(TestCase):    
    def test_account_is_configured(self):
        self.assertTrue('accounts' in INSTALLED_APPS)
        self.assertTrue('accounts.User' == AUTH_USER_MODEL)


class UserTest(TestCase):
    def setUp(self):
        self.username = "testuser"
        self.email = "testuser@testbase.com"
        self.first_name = "Test"
        self.last_name = "User"
        self.password = "z"

        self.test_user = User.objects.create_user(
            username=self.username,
            email=self.email,
            first_name=self.first_name,
            last_name=self.last_name
        )

    def test_create_user(self):
        self.assertIsInstance(self.test_user, User)

    def test_default_user_is_active(self):
        self.assertTrue(self.test_user.is_active)

    def test_default_user_is_staff(self):
        self.assertFalse(self.test_user.is_staff)

    def test_default_user_is_superuser(self):
        self.assertFalse(self.test_user.is_superuser)

    def test_get_full_name(self):
        self.assertEqual('Test User', self.test_user.get_full_name())

    def test_get_short_name(self):
        self.assertEqual(self.email, self.test_user.get_short_name())

    def test_unicode(self):
        self.assertEqual(self.username, self.test_user.__unicode__())

感谢您的任何意见。

最佳答案

您可以使用这个将 pytest 与 Django 集成的插件: https://pytest-django.readthedocs.org/en/latest/tutorial.html

这里描述了 pytest.ini 的设置和更改: http://www.johnmcostaiii.net/2013/django-projects-to-django-apps-converting-the-unit-tests/

您会在此处找到您的示例,从幻灯片 57 开始。 该平台将有助于测试 View 以及模型和特定于测试模型的设置:https://speakerdeck.com/pelme/testing-django-applications-with-py-dot-test-europython-2013

具体查看此处记录的@pytest.mark.django_db helper:http://pytest-django.readthedocs.org/en/latest/helpers.html

在上面的卡片中也提到了允许在测试中访问数据库的示例复制到这里。如果没有 mark.django_db,测试将失败。

import pytest
@pytest.mark.django_db
def test_user_count():
    assert User.objects.count() == 0

关于python - 如何使用 pytest 测试 Django 模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36446585/

相关文章:

python - 为了从两个 3 位数字的乘积中获得最大的回文数,我的计算哪里出错了?

python - 使用名称来声明没有值的 python 枚举

python - 如何在django中存储复杂的csv数据?

python - Apache /Django : ImportError: No module named 'my_project'

python - 如何通过 python click.version_option 装饰器的 Travis CI 测试?

python - 如何在具有模拟装饰器的测试中使用 pytest capsys?

python - 为什么我的 Django 代码不能是 'Standalone Django scripts'

python - 在 OpenERP 的薪资部分中扣除休假

python - 如何在Django Rest框架中为list_route利用内置分页功能?

python - 如何使用 pytest 检查函数返回的 False 值