使用数据库测试Django GraphQL API

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

我正在使用

from graphene_django.utils.testing import GraphQLTestCase

实施我的测试。但是,

的结果
class PeopleTests(GraphQLTestCase):
  GRAPHQL_SCHEMA = schema

  def test_all_person_query_admin_token(self):

    response = self.query(
        '''
       query getPersons{
            persons{
            firstName,
            lastName
            }
        }
        ''',
        op_name='persons',
        headers={'HTTP_AUTHORIZATION': f'JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiZXhwIjoxNTY2MTk4Mzg5LCJvcmlnSWF0IjoxNTY2MTk4MDg5fQ.P4u7PNKPLFsc4dvhBLw-EfuN8jg2d-lqdZjWruEDlpc'}
    )

总是返回一个空的Person列表。我在Django Test Database looks empty while test is runnin中读到,这是GraphQLTestCase继承自TestCase的结果。这篇文章很老(2012),但我似乎找不到任何有关如何使用适当的测试数据库的文档。

很抱歉,如果我错过了什么,答案很明显。

谢谢

python django unit-testing graphene-python graphene-django
1个回答
0
投票

这是我如何使用Pytest测试Graphene-Django API。

@pytest.mark.django_db
class TestBlogSchema(TestCase):

    def setUp(self):
       self.client = Client(schema)
       self.blog = mixer.blend(Blog)

    def test_single_blog_query(self):
       response = self.client.execute(single_blog_query, variables={"id": self.blog.id})
       response_blog = response.get("data").get("blog")

       assert response_blog["id"] == str(self.blog.id)

您可以从here中查看完整的示例

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