微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

pytest :: python:如何解开这段代码中如何调用闭包

如何解决pytest :: python:如何解开这段代码中如何调用闭包

我从 https://realpython.com/django-pytest-fixtures/#injecting-fixtures-into-other-fixtures

读到了这段代码
from typing import List,Optional

import pytest
from django.contrib.auth.models import User,Group,Permission

@pytest.fixture
def app_user_group(db) -> Group:
    group = Group.objects.create(name="app_user")
    change_user_permissions = Permission.objects.filter(
        codename__in=["change_user","view_user"],)
    group.permissions.add(*change_user_permissions)
    return group

@pytest.fixture
def app_user_factory(db,app_user_group: Group):
    # Closure
    def create_app_user(
        username: str,password: Optional[str] = None,first_name: Optional[str] = "first name",last_name: Optional[str] = "last name",email: Optional[str] = "foo@bar.com",is_staff: str = False,is_superuser: str = False,is_active: str = True,groups: List[Group] = [],) -> User:
        user = User.objects.create_user(
            username=username,password=password,first_name=first_name,last_name=last_name,email=email,is_staff=is_staff,is_superuser=is_superuser,is_active=is_active,)
        user.groups.add(app_user_group)
        # Add additional groups,if provided.
        user.groups.add(*groups)
        return user
    return create_app_user

@pytest.fixture
def user_A(db,app_user_factory) -> User:
    return app_user_factory("A")

这里我无法理解语法 app_user_factory("A")

#Generally as per my understanding the flow is when one run the test is

db = some_test_db_from_pytest
group = app_user_group(db)
closure_function = app_user_factory(db,group) # returns create_app_user function

#then

closure_function('A')

# But here we see directly

app_user_factory("A")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。