With recent advancements in the IT industry coupled with its growing pace, it’s essential for software companies and professionals alike to up their game. And while there have been many architectural changes with respect to development in an effort to increase productivity and reduce cost, there is certainly a need for similar attention to be paid to the testing of software as well. But where to begin? Fortunately, you begin and end with API automation using BDD.
The first thing is to look for a tool that helps you write easy to maintain automated tests, faster. For this article, we’ll focus on automating Rest API testing. I’ve used many tools over the years for this purpose, however, I recently came across one that not only changed my view on automated API testing but on the concept of absolute automation as a whole. It’s called Karate, and if you are involved in automated API testing and don’t have a background in programming, you may want to give it a consideration.
What is Karate?
Well, for starters, it’s easily my favourite form of martial arts. Secondly, and from a more professional perspective, Karate is an open-source test automation tool for tests written using Behaviour Driven Development (BDD) Gherkin syntax. Unlike other BDD frameworks like Cucumber, Karate defines all the step-definitions by default. This enables even non-developers and people with no programming background to write API tests quickly
The following is a perfect depiction of what I mean:
Now from the above image, it’s clear how simple it is to write a REST API test using Karate. Though the test’s readability is not as in-depth as the Cucumber test, it does save you all the hassle of defining the steps, business logic, and POJOs. I’d argue that’s worth it. The other coolest thing about Karate is that it is a single stop shop for your API, Web & Native Application tests and that you can easily implement in your CI/CD Pipeline. As for the cherry on top, you can use the same tests for performance testing.
BDD Framework & Karate
Behaviour-Driven Development (BDD) is a software development and testing approach that bridges the gap between technical and non-technical teams. An offshoot of Test Driven Development, BDD mostly deals with systems user behaviour. In terms of testing and automation, BDD frameworks are especially crucial in small teams where one can’t afford to hire individuals to create and maintain an automation framework. That’s where Karate comes in.
Getting Started with Karate
Let’s look at some operations you would typically perform in Automated API Testing, assuming you already have your environment setup. If not, please refer to Karate’s official GitHub page, which gives you a complete insight of Karate and how to set-up your project.
Karate provides its own DSL (Domain Specific Language), which uses a Gherkin-like language enabling one to write tests without programming knowledge, and write tests in .feature files. Here is an example:
In the first Background section, we’ll be defining the variables needed to access globally and hence the URL and other variables. Now to make the most out of reusability, Karate lets us easily call and read the other feature files as defined in the background section, so we can access the variables from the called feature.
The last thing in the background section is the ‘schema’ variable used for schema validation. From the above example, it’s clear how easy it is to define Karate’s schema instead of using the complex JSON schema.
Now let’s understand all the components under the Scenario section of the feature file:
- Karate follows the “Given When Then And” style for test scripting that helps in understanding and mapping the requirements.
- The first component is the ‘path’ keyword, which defines your complete path (endpoint) and the base URL specified in the background section.
- The second is ‘param’, that is simply the query parameter.
- Thirdly, ‘header’, which is how you define the request headers for your API test.
- ‘method’ is the HTTP request type.
- Then starts the assertion block where you’re validating the status code, schema, response time, and finally, printing the console’s response.
To execute the above feature file, you’ll need a simple Java runner file that will look something like this:
Here we’re using the JUnit 4 Runner. You provide the path to your feature file in @KarateOptions, and that’s pretty much it.
As we’re using the JUnit 4 Runner, Karate can provide very simple yet elegant reports by default for each run, but you can also customize them and attach the failed step’s screenshot. Plus, you can use Cucumber-reporting if you want more detailed reports or if you’re implementing parallel execution.
The following is a screenshot of an actual test report:
Integration with CI/CD Pipeline
Finally, all you need to complete your automation cycle is to integrate your tests with your CI/CD Pipeline. There are a couple of options out there that you can choose from, however, the best in my opinion for web tests are the Jenkins and Saucelabs duo while for API, the Jenkins and docker pair has proven the fittest. You can find ample documentation for docker implementation here.
I’ve been using Karate for some time now and it has proven it’s worth in API automation. As far as I’m concerned, it’s simple syntax and ability to support complex code makes it one of the front runners among all the BDD automation frameworks. And in case you weren’t sold, here are a few points i’d like you to consider before we depart ways:
- Karate can be reused to test any HTTP request.
- It can be used to test data from Excel or JSON files to execute data-driven tests.
- The BDD approach and Gherkin style syntax makes it easier for everyone to understand and even design tests without any programming background.
- “One-click” to execute tests and study the results through reports.
- Easily integrated with dockers and Jenkins for continuous integration.
While I think the benefits of Karate are quite clear at this point, if I had to leave you with one take away, it’d be that if you’re looking for a framework to write easy and fast API tests but lack programming knowledge, Karate might be your best option. Not to mention you could add “Knows Karate” to your resume.
The above was written in collaboration with Ragavan Balasubramaniam, Manager, Automation Services at mobileLIVE.