Testing
The GraphiQL playground integrated with Strawberry available at
http://localhost:8000/graphql (if you run the
schema with strawberry server
) 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 to
run our first test:
This test_query
example:
- defines the query we will test against; it accepts one argument,
title
, as input - executes the query and assigns the result to a
result
variable - asserts that the result is what we are expecting: nothing in
errors
and our desired book indata
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:
Testing Mutations
We can also write a test for our addBook
Mutation
example:
Testing Subscriptions
And finally, a test for our count
Subscription :
As you can see testing Subscriptions is a bit more complicated because we want to check the result of each individual result.