— GraphQL, Karate, Faker, API testing — 1 min read
– 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 :
Assertions can be perform with match , contains and assert (from Junit). $ symbol represent response , # allows introduce custom variables
And other assertions can be performed - like check on data type:
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)
Imported files can be written in JavaScript , JSON or like in the example here GraphQL . On a side note Karate is a Maven framework. 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 That get resolved in the feature file
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 . 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