From 8d6947abb54e21befe33279e01df12f772b46a4d Mon Sep 17 00:00:00 2001 From: Dmitry Dygalo Date: Tue, 3 May 2022 16:07:42 +0200 Subject: [PATCH] chore: Update Schemathesis usage instructions --- src/content/code/tools/schemathesis.md | 47 ++++++++++++++------------ 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/content/code/tools/schemathesis.md b/src/content/code/tools/schemathesis.md index 19cc336747..ae938f1336 100644 --- a/src/content/code/tools/schemathesis.md +++ b/src/content/code/tools/schemathesis.md @@ -5,33 +5,38 @@ url: https://github.com/schemathesis/schemathesis github: schemathesis/schemathesis --- -Install Schemathesis via `pip`: +Run Schemathesis via Docker against your GraphQL endpoint: ```bash -pip install schemathesis +docker run schemathesis/schemathesis \ + run https://your.app.com/graphql ``` -Then, create a file `test_api.py` with the content below and replace the `URL` value with your own GraphQL endpoint URL: +Schemathesis will generate queries matching your GraphQL schema and catch server crashes automatically. +Generated queries have arbitrary depth and may contain any subset of GraphQL types defined in the input schema. +They expose edge cases in your code that are unlikely to be found otherwise. -```python -from hypothesis import settings -import schemathesis +Note that you can write your app in any programming language; the tool will communicate with it over HTTP. -URL = "https://your.app.com/graphql" -schema = schemathesis.graphql.from_url(URL) +For example, running the command above against `https://bahnql.herokuapp.com/graphql` uncovers that running the `{ search(searchTerm: "") { stations { name } } }` query leads to a server error: -@schema.parametrize() -@settings(deadline=None) -def test_api(case): - response = case.call() - case.validate_response(response) ``` - -Then run `pytest test_api.py`. Note that you can write your app in any programming language; the tool will communicate with it over HTTP. - -Schemathesis will generate valid queries automatically based on the schema and will minimize failing cases. -For example, running the code above against `https://bahnql.herokuapp.com/graphql` uncovers that running the `{ search(searchTerm: "") { stations { name } } }` query leads to a server error: - -``` -{"errors":[{"message":"Cannot read property \'city\' of undefined","locations":[{"line":1,"column":28}],"path":["search","stations"]}],"data":null} +{ + "errors": [ + { + "message": "Cannot read property 'city' of undefined", + "locations": [ + { + "line": 1, + "column": 28 + } + ], + "path": [ + "search", + "stations" + ] + } + ], + "data": null +} ```