Skip to content

chore: Update Schemathesis usage instructions #1233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 26 additions & 21 deletions src/content/code/tools/schemathesis.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
```