I need to write a functional test suite (that will test a GraphQl API). The test suite will be in a separate repo and container from the API.
One approach I thought of would be to use a BDD framework within the test suite. The suite would run all the BDD tests after receiving a HTTP request.
I was considering using Cucumber.js as the BDD framework. I know there is npm test
. I am not sure how I will execute the tests. It feels a bit awkward to use a unit testing framework in this way. Does this approach make sense?
What tooling exists to do something like this? I am open to consider various languages and tools.
Using this interface, simply write queries using the GraphQL syntax and specify any variables used in the query in JSON format. You can then run the test step on its own, or add an assertion to verify a particular response using properties like contains, equals, counts and even regular expression matches.
To provide auto-complete and error checking of GraphQL queries, Insomnia automatically fetches the schema by sending an introspection query. This query is performed both when switching to a new GraphQL request in the app or when various properties of the request are modified (eg. URL).
Testing GraphQL using Rest Assured Firstly, convert the GraphQL query into Stringified JSON format. Secondly, pass the converted String as Body parameters to the Request. Then finally, validate the response.
Karate is a relatively new web-services test-automation framework that happens to be well suited for testing GraphQL responses because of 2 specific capabilities
Here is a good example: graphql.feature
with a snippet below:
# you can also read this query from a file
Given text query =
"""
{
pokemon(name: "Pikachu") {
id
number
name
attacks {
special {
name
type
damage
}
}
}
}
"""
And request { query: '#(query)' }
When method post
Then status 200
# json-path makes it easy to focus only on the parts you are interested in
# which is especially useful for graph-ql as responses tend to be heavily nested
* match $.data.pokemon.number == '025'
# the '..' wildcard is useful for traversing deeply nested parts of the json
* def attacks = get[0] response..special
* match attacks contains { name: 'Thunderbolt', type: 'Electric', damage: 55 }
For non-Java teams, Karate provides a binary executable that only requires the JRE, and Visual Studio Code is sufficient as an IDE. JavaScript programmers will especially feel at home because of how Karate embeds a JavaScript runtime and supports 'lenient' JSON (no double-quotes needed, no need to enclose JSON keys in quotes).
EDIT: here's a link to an article by a team using it in production: https://www.codemotion.com/magazine/dev-hub/web-developer/graphql-testing-with-karate/
Disclaimer: dev here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With