schemathesis / schemathesis

Supercharge your API testing, catch bugs, and ensure compliance

Home Page:https://schemathesis.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] documentation how to use graphql schemas in unittests

devkral opened this issue · comments

Describe the bug

How can I use a graphql schema in unittests (not pytest)? I think it is nearly the same with openapi schemas, except you could extract portions of it and feed it to given. There is no method to convert a graphql schema into search strategy.

class Foo(TestCase):
  @given(dcase=schema)
  def test(self, dcase):
     dcase.call_and_validate()

fails

class Foo(TestCase):
  @given(dcase=schema["POST"]["/graphql/"])
  def test(self, dcase):
     dcase.call_and_validate()

does only extract either queries or mutations but there is no control what kind of API Operations are extracted, it seems to be random.

Ah yeah and the call_and_validate() does not check if the response is actually json and has errors but this is a different issue ;)

I am going to update the Unittest support section of the docs on this matter, but it is similar to Open API - you need to use the as_strategy method to convert API operation to a strategy.

Another component of it is the way operations are accessed. In Open API it is schema[path][method], and now GraphQL follows the same pattern, but as GraphQL has just a single path & method, the access is bugged. I.e. Schemathesis breaks down a GraphQL API by query / mutation + field name, but when schema[path][method] happens, it always returns the latest one (in terms of iterating over query, mutation and fields definition order).

So, this is a bug. I'll fix it, to it works like schema["<query-type-name>"]["<field-name>"].

Thank you for pointing this out