Skip to content

Add Schemathesis #1001

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
Jan 26, 2021
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
37 changes: 37 additions & 0 deletions src/content/code/tools/schemathesis.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
name: Schemathesis
description: A modern API testing tool for web applications built with Open API and GraphQL specifications.
url: https://github.com/schemathesis/schemathesis
github: schemathesis/schemathesis
---

Install Schemathesis via `pip`:

```bash
pip install schemathesis
```

Then, create a file `test_api.py` with the content below and replace the `URL` value with your own GraphQL endpoint URL:

```python
from hypothesis import settings
import schemathesis

URL = "https://your.app.com/graphql"
schema = schemathesis.graphql.from_url(URL)

@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}
```