pytest测试代码详细描述(其四)

以用户下的模型模块测试为例

from pytest import fixture, mark, raises

from antilles.user.models import User

'''
为方便理解,添加了与本模块有关的代码块
def login_fail(self):
        self.fail_chances += 1
        if self.fail_chances >= settings.LOGIN_FAIL_MAX_CHANCE:
            self.fail_chances = 0
            self.effective_time = \
                timezone.now() + settings.LOGIN_FAIL_LOCKED_DURATION

        self.save()

def login_success(self):
    self.last_login = timezone.now()
    self.fail_chances = 0
    self.save()

def is_activate(self):
    return timezone.now() >= self.effective_time

'''

'''
fixture主要的目的是为了提供一种可靠和可重复性的手段去运行那些最基本的测试内容。
比如在测试网站的功能时,每个测试用例都要登录和退出,利用fixture就可以只做一次,
否则每个测试用例都要做这两步也是冗余。
'''
@fixture
def user(username):
    user = User.objects.create(username=username)
    user._passwd = None

    assert user.workspace is None
    assert user.uid is None

    return user

'''
mark主要是用来指定参数的!
个函数都要把 user 作为参数传递进去
mark.django_db 是django 配合的测试模块 pytest-django
使用pytest标记来告诉pytest-django您的测试需求数据库访问
每一次数据库请求都要绑定 mark.django_db
'''

@mark.django_db
def test_login_fail(user, settings):
    #登陆次数测试,遍历到最大尝试次数
    for index in range(settings.LOGIN_FAIL_MAX_CHANCE):
        #遍历最终的结果是,fail_chances=0
        '''
        self.effective_time = \
                timezone.now() + settings.LOGIN_FAIL_LOCKED_DURATION
        '''
        user.login_fail()

    assert user.fail_chances == 0
        '''
         def is_activate(self):
            return timezone.now() >= self.effective_time
        '''
    assert not user.is_activate()


@mark.django_db
def test_login_succes(user):
    '''登陆测试'''
    # 判断是否是最大次数
    user.login_fail()
    #更改最近登陆时间!
    user.login_success()

    assert user.fail_chances == 0
    assert user.is_activate()


@mark.django_db
def test_role(user):
    assert not user.is_anonymous()
    assert user.is_authenticated()
    assert not user.is_admin

    from rest_framework.exceptions import PermissionDenied
    from antilles.user.permissions import AsAdminRole

    with raises(PermissionDenied):
        user.require_role(AsAdminRole)

    assert not user.check_role('admin')
    assert user.check_role('user')


@mark.django_db
def test_ent(username, mocker):
    from collections import namedtuple

    mock_group = namedtuple(
        'mock_group',
        ['gr_name', 'gr_gid', 'gr_passwd', 'gr_mem']
    )
    mock_user = namedtuple(
        'mock_user',
        [
            'pw_name', 'pw_passwd',
            'pw_uid', 'pw_gid',
            'pw_gecos', 'pw_dir',
            'pw_shell'
        ]
    )

    mocker.patch(
        'antilles.user.models.pwd.getpwnam',
    ).return_value = mock_user(
        pw_name='test', pw_passwd='x',
        pw_uid=123, pw_gid=456,
        pw_gecos='test',
        pw_dir='dir', pw_shell='/bin/bash'
    )

    mocker.patch(
        'antilles.user.models.grp.getgrgid',
    ).return_value = mock_group(
        gr_name='group',
        gr_gid=456,
        gr_mem=[],
        gr_passwd='x'
    )

    user = User.objects.create(username=username)

    assert user.workspace == 'dir'
    assert user.uid == 123
    assert user.gid == 456
    assert user.group.gr_gid == 456

 

转载于:https://www.cnblogs.com/shi-qi/articles/9407875.html

你可能感兴趣的:(pytest测试代码详细描述(其四))