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 (将#修改为@)