Django 测试用例:无法从 GraphQL 客户端调用执行函数

问题描述 投票:0回答:1

我正在使用 graphene-django 库创建测试用例来验证 graphql api 调用,在尝试执行查询时,我得到 Client obj has no attrexecute ,而 dir 方法显示了执行方法,我很困惑,非常感谢任何帮助:

这是我尝试过的:

from django.test import TestCase
from graphene.test import Client

from app.graphql.schema import schema
from app.graphql.tests import constants


def generate_token(user):
    from app.serializers import TwoFactorAuthenticationSerializer
    return TwoFactorAuthenticationSerializer().get_token(user).access_token

class AssetGraphQLTestCase(TestCase):

    client = Client(schema)
    print("outside------------------", type(client))
    print("outside------------------", dir(client))

    @classmethod
    def setUpTestData(cls):
        print("Setting up test data-------------------------------")
        cls.client = Client(schema)
        cls.user = constants.TEST_USER
        cls.organization = constants.TEST_ORGANIZATION
        cls.asset_1 = constants.TEST_ASSET_1
        cls.headers = {
            "Content-Type": "application/json",
            "HTTP_AUTHORIZATION":generate_token(cls.user)
        }

        print("--------------------------------",type(cls.client))
        print("--------------------------------",dir(cls.client))


    def test_all_asset(self):
        print("Testing-------------------------------------------")
        response_data = self.client.execute(query=constants.GRAPHQL_ASSET_QUERY, varaibles=constants.QUERY_VARIABLES, headers=self.headers)
        print(response_data)
        print(response_data['data'])
        self.assertEqual(response_data.status_code, 200)
        self.assertEqual(response_data['data'], 'Expected value')`

这是输出:

Setting up test data-------------------------------
-------------------------------- <class 'graphene.test.Client'>
-------------------------------- ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'execute', 'execute_async', 'execute_options', 'format_error', 'format_result', 'schema']
Testing-------------------------------------------
E
======================================================================
ERROR: test_all_asset (app.graphql.tests.test_assets_graphql_fix.AssetGraphQLTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/app/graphql/tests/test_assets_graphql_fix.py", line 41, in test_all_asset
    response_data = self.client.execute(query=constants.GRAPHQL_ASSET_QUERY, varaibles=constants.QUERY_VARIABLES, headers=self.headers)
AttributeError: 'Client' object has no attribute 'execute'

----------------------------------------------------------------------
Ran 1 test in 0.009s

FAILED (errors=1)
Destroying test database for alias 'default'...
root@b026e124367f:/app#
python django graphene-python django-tests graphene-django
1个回答
0
投票

您继承自

TestCase
,它使用其客户端变量而不是您定义的类变量,并且此
client
变量没有
execute
方法。

您可以简单地在

test_all_asset
方法中调用您的客户端类变量,如下所示
AssetGraphQLTestCase.client.execute(..)

我建议继承

GraphQLTestCase
而不是 django
TestCase
这是示例

© www.soinside.com 2019 - 2024. All rights reserved.