Skip to content

Katarzyna Kmiotek

Testing GraphQL with Karate

GraphQL, Karate, Faker1 min read

" Mistakes are always forgivable, if one has the courage to admit them. "

– Bruce Lee




As part of my learning project, I built the Apollo Server GraphQL endpoint and now use a range of API tools to learn how they can be used for testing GraphQL. A week ago worked with Playground, Postman, Supertest (Jest, Chai) and documented it here. This time decided to learn a new tool called Karate.

Karate is a really impressive solution - the single framework for API, UI, and performance tests. Is very easy to adapt for testers experienced with writing Cucumber scenarios as Karate also uses Gherkin syntax but no Java stepDefinition file is needed. Karate comes with Junit support and has built-in reporting features.
This short post will show how Karate can be used for testing the GraphQL endpoint.

As I mentioned uses ordinary language to describe steps, follows simple Given(), When(), Then() steps. Operates on keywords/variables like response or responseTime :

karate get

Assertions can be perform with match , contains and assert (from Junit). $ symbol represent response , # allows introduce custom variables

karate get one And other assertions can be performed - like check on data type: delete

Those are just simple queries where variables are defined with the file. Karate allows importing them also from files so can be reused in other tests (DRY)

read query Imported files can be written in JavaScript , JSON or like in the example here GraphQL . On a side note Karate is a Maven framework. query variables This gets even more interesting when we can implement Faker to dynamically create variables. Start from adding Maven dependency for Java Faker in the pom.xml file and then import it in karate-config.js

GraphQL query accepts variables add query That get resolved in the feature file faker data in query

I also mentioned a great reporting that comes with Karate. After executing tests report can be found in the path target/karate-report.karate-summary.html . report The repository with code is available here if you want to have a look at how all this works together. I will keep learning Karate .





" I fear not the man who has practiced 10,000 kicks once, but I fear the man who has practiced one kick 10,000 times. "

– Bruce Lee

© 2021 by Katarzyna Kmiotek. All rights reserved.
Theme by LekoArts