diff --git a/docs/testing.rst b/docs/testing.rst index 6dcb237..23acef2 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -1,6 +1,9 @@ Testing API calls with django ============================= +Using unittest +-------------- + If you want to unittest your API calls derive your test case from the class `GraphQLTestCase`. Your endpoint is set through the `GRAPHQL_URL` attribute on `GraphQLTestCase`. The default endpoint is `GRAPHQL_URL = "/graphql/"`. @@ -78,3 +81,38 @@ Usage: # Add some more asserts if you like ... + +Using pytest +------------ + +To use pytest define a simple fixture using the query helper below + +.. code:: python + + # Create a fixture using the graphql_query helper and `client` fixture from `pytest-django`. + import pytest + from graphene_django.utils.testing import graphql_query + + @pytest.fixture + def client_query(client) + def func(*args, **kwargs): + return graphql_query(*args, **kwargs, client=client) + + return func + + # Test you query using the client_query fixture + def test_some_query(client_query): + response = graphql_query( + ''' + query { + myModel { + id + name + } + } + ''', + op_name='myModel' + ) + + content = json.loads(response.content) + assert 'errors' not in content \ No newline at end of file diff --git a/graphene_django/tests/test_utils.py b/graphene_django/tests/test_utils.py index 9707272..4ed0bec 100644 --- a/graphene_django/tests/test_utils.py +++ b/graphene_django/tests/test_utils.py @@ -71,3 +71,17 @@ def test_graphql_query_case_op_name(post_mock): "operationName", "QueryName", ) in body.items(), "Field 'operationName' is not present in the final request." + + +@pytest.fixture +def client_query(client): + def func(*args, **kwargs): + return graphql_query(*args, **kwargs, client=client) + + return func + + +def test_pytest_fixture_usage(client_query): + response = graphql_query("query { test }") + content = json.loads(response.content) + assert content == {"data": {"test": "Hello World"}}