Karate is a behaviour driven development (BDD) API testing framework. Built on top of the popular Cucumber framework. Which makes it easy for even non-programmers to use and implement. Approved by the man himself, Chuck Norris.
It is my hope and intention that after reading this blog you will have a clear understanding of how to implement Karate in a Java project with Maven by looking at some examples of how to write tests and best practices. Of how to run tests and generating reports.
What you need:
- JDK 11
- maven 3.5.0
- A hot, steamy cup of coffee
The source code can be found in GitHub.
Creating the project
As is the case with all modern and top of the line java projects, we will start our project in Spring Boot. Navigate to https://start.spring.io/ and create a base project with the following setup.
- Project: Maven Project
- Language: Java
- Spring Boot: 2.2.2
- Project Metadata: Java 11, packaging: Jar.
- Fill in the rest of the metadata as you wish.
Add the following maven dependencies to your root pom. Don’t forget to put them inside the
<dependencies/> tag without overwriting any of the existing dependencies.
The screenshot below is an example of how to create a project structure. It’s a basic structure where all the magic will happen within the test folder. The ‘resources’ folder is not created by the spring initializer, this will have to be done manually, as is the tests package within. Please take a look at the generated project and update it.
Writing the tests
Let's write some tests for a public Rest API. For these examples we will use the free to use API’s of the Chuck Norris facts database. More information regarding these API’s can be found here!
Create a new file called chucknorris.feature in the resources folder (src/test/resources). In that file we test the first endpoint for retrieving a random fact with a GET method. Each fact contains some values, we don’t know what the values are because it’s a random fact, but we can validate that the values are of the expected type.
Now we will write a Scenario for the retrieval of a fact with an id. This time we are certain of the values which we expect. And for this scenario we want to have a custom validation written in java. We can’t screw things up when it’s about Chuck Norris, so we have to be sure that the fact is in fact a Chuck Norris fact. Let’s take a look at the code.
Write the below java class in the src/test/java folder.
The scenario which implements our custom java validation method is up next. Custom java methods are great when out of the box assertions or match statements are not sufficient.
Running the tests
Karate tests run on top of junit, this makes running the tests in any IDE really easy. All we have to do is add a java class where we specify the location of the tests.
This however presents some problems if you want to integrate it into your development, as a karate test will need to hit up a running instance of the service. In a future blog, we can look at how to use codehaus cargo to setup a fully automated and integrated test.
Now that we can run the tests, we want to have reports of the outcome. To do this we add the following dependency in our pom.
Just adding a dependency is not enough. We have to add the following java methods to the KarateUnitTests Class which we added in the previous section (Running the tests).
The generated reports can be found in target/cucumber-html-reports.
I've shown you how to create a basic project for Karate tests and gave you some examples on how to write the tests, how run the tests and how to generate reports. This is the basis of all Karate projects.
For more information on the many functionalities of the karate framework I suggest you visit the official GitHub.
If you have any further questions please do not hesitate to contact us
Geld en tijd besparen? Meer overzicht?
Doe onze Quick Scan
en ontdek in 2 minuten vrijblijvend welke kansen IT jouw organisatie biedt!