1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
---
title: Testing
---
# Testing
The GraphiQL playground integrated with Strawberry available at
[http://localhost:8000/graphql](http://localhost:8000/graphql) (if you run the
schema with `strawberry dev`) can be a good place to start testing your queries
and mutations. However, at some point, while you are developing your application
(or even before if you are practising TDD), you may want to create some
automated tests.
We can use the Strawberry `schema` object we defined in the
[Getting Started tutorial](../index.md#step-5-create-our-schema-and-run-it) to
run our first test:
```python
import strawberry
@strawberry.type
class Book:
title: str
author: str
def get_books(title: str) -> list[Book]:
if title == "The Great Gatsby":
return [
Book(
title="The Great Gatsby",
author="F. Scott Fitzgerald",
),
]
return []
@strawberry.type
class Query:
@strawberry.field
def books(self, title: str) -> list[Book]:
return get_books(title)
schema = strawberry.Schema(Query)
def test_query():
query = """
query TestQuery($title: String!) {
books(title: $title) {
title
author
}
}
"""
result = schema.execute_sync(
query,
variable_values={"title": "The Great Gatsby"},
)
assert result.errors is None
assert result.data["books"] == [
{
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
}
]
```
This `test_query` example:
1. defines a complete schema with a `Book` type and a `books` query that accepts
a `title` argument
2. defines the query we will test against, using a GraphQL variable `$title`
3. executes the query using `execute_sync`, passing the variable via
`variable_values`
4. asserts that the result is what we are expecting: nothing in `errors` and our
desired book in `data`
As you may have noticed, we explicitly defined the query variable `title`, and
we passed it separately with the `variable_values` argument, but we could have
directly hardcoded the `title` in the query string instead. We did this on
purpose because usually the query's arguments will be dynamic and, as we want to
test our application as close to production as possible, it wouldn't make much
sense to hardcode the variables in the query.
## Testing Async
Since Strawberry supports async, tests can also be written to be async:
```python
@pytest.mark.asyncio
async def test_query_async():
...
resp = await schema.execute(query, variable_values={"title": "The Great Gatsby"})
...
```
## Testing Mutations
We can also write a test for our [`addBook` Mutation](../general/mutations.md)
example:
```python
@pytest.mark.asyncio
async def test_mutation():
mutation = """
mutation TestMutation($title: String!, $author: String!) {
addBook(title: $title, author: $author) {
title
}
}
"""
resp = await schema.execute(
mutation,
variable_values={
"title": "The Little Prince",
"author": "Antoine de Saint-Exupéry",
},
)
assert resp.errors is None
assert resp.data["addBook"] == {
"title": "The Little Prince",
}
```
## Testing Subscriptions
And finally, a test for our [`count` Subscription](../general/subscriptions.md):
```python
import asyncio
import pytest
import strawberry
@strawberry.type
class Subscription:
@strawberry.subscription
async def count(self, target: int = 100) -> int:
for i in range(target):
yield i
await asyncio.sleep(0.5)
@strawberry.type
class Query:
@strawberry.field
def hello() -> str:
return "world"
schema = strawberry.Schema(query=Query, subscription=Subscription)
@pytest.mark.asyncio
async def test_subscription():
query = """
subscription {
count(target: 3)
}
"""
sub = await schema.subscribe(query)
index = 0
async for result in sub:
assert not result.errors
assert result.data == {"count": index}
index += 1
```
As you can see testing Subscriptions is a bit more complicated because we want
to check the result of each individual result.
|