Skip to main content
Karate vs. Cucumber: A Comparison of Frameworks for Automated API Testing | Kranio

Finally, we add the Spring dependency:

<dependency>    
  <groupId>io.cucumber</groupId>    
  <artifactId>cucumber-spring</artifactId>    
  <version>6.8.0</version>    
  <scope>test</scope>
</dependency>

Next, you must configure the REST service. For this example, a REST api with a list of available games will be used.

Cucumber Steps Definitions

To run cucumber tests with Junit, it is necessary to define the steps, create an empty class with the @RunWith(Cucumber.class) annotation and @CucumberOptions to define the options we will use.

Then, in the path "src/test/resources/features/"  you must write the files that contain the test features written in Gherkin language.

This scenario makes a GET call to the service that delivers the list of games and validates that the information is displayed correctly.

After having this scenario created in Gherkin, you must code the steps of each feature in Java; for this, create a class that contains the same steps so that the steps are recognized.

NOTE: we must use Rest-Assured to validate the API responses.

Now you have everything necessary to run the test. Run the test with Junit in the RunTest class we have created. If everything is correct, the tests ran successfully.

To see the report you can use cucumber-reporting.

Image: Report generated by cucumber report.

Now we move on to Karate.

Karate BDD

Karate is a simple testing framework just like Cucumber and has built-in tools that make it very effective. This is how its creators describe it: 

“Karate is the only open-source tool that combines API test automation, mocks, performance testing, and even UI automation into a single unified framework. The BDD syntax popularized by Cucumber is a neutral and easy language even for non-programmers. Powerful JSON and XML assertions are built-in, and you can run tests in parallel for speed "

For this example, we will not use all the tools, but several to perform the same exercise as with Cucumber and make the comparison.

First, add the following dependencies to the pom:

<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->        
<dependency>            
    <groupId>org.mockito</groupId>            
    <artifactId>mockito-core</artifactId>            
    <version>${mockito.version}</version>            
    <scope>test</scope>        
</dependency>

<!-- https://mvnrepository.com/artifact/com.intuit.karate/karate-apache -->        
<dependency>            
    <groupId>com.intuit.karate</groupId>            
    <artifactId>karate-apache</artifactId>            
    <version>${karate.version}</version>            
    <scope>test</scope>        
</dependency>

        
            
    com.intuit.karate            
    karate-junit5            
    ${karate.version}            
    test        
  
        
	
    com.intuit.karate	
    karate-junit4
    0.9.5	
    test
  
      
        
                
                        
            src/test/java                
                                
                **/*.java                
                        
                
       
         
                
                        
            org.springframework.boot                
            spring-boot-maven-plugin            
          
                  
                        
            org.apache.maven.plugins                
            maven-surefire-plugin                
            2.22.1                
                                
                                        
                    karate/**/*.java                    
                                    
                                        
                    com/**/*.java                    
                                
                        
                
    

Then create a runner class to add the Karate libraries and later run the tests from there. In the class, the method that runs the tests must have the annotation @Karate.Test:

Then create the feature file that will contain the scenarios and steps to execute:

@Tag games
Feature: Get list of games  
    Background:    
        * def baseUrl = 'https://api-gamelist.herokuapp.com'    
        * def gameBase = '/games'  
    Scenario: Get the game list    
        Given baseUrl+gameBase    
        And header Accept = 'application/json'    
        When method GET    
        Then status 200

Then run the tests. This will automatically generate an HTML report:

Image: Results with Karate

Image: Results with Karate

Now that we know both frameworks, we can draw conclusions! 

Although both frameworks work similarly, Karate is simpler to use than Cucumber. Let's see the advantages of both frameworks:

Advantages of Karate:

Advantages of Cucumber:

Karate or Cucumber: Which is better?

Both tools are powerful and the one you choose will depend on each case and how you need to visualize the results. If you need to deliver information at a managerial level, with a better graphical interface and better interpretation of results, Cucumber is the best alternative if complemented with Serenity BDD or another component to visualize results. If you need to graph results for a technical area, Karate is the more agile alternative because you will get the same results with fewer steps and less complexity.

Ready to optimize your API tests with the best tools?

At Kranio, we have experts in test automation who will help you implement frameworks like Karate and Cucumber, improving the quality and efficiency of your developments. Contact us and discover how we can boost your technology projects.

Previous Posts

Google Apps Scripts: Automation and Efficiency within the Google Ecosystem

Google Apps Scripts: Automation and Efficiency within the Google Ecosystem

Automate tasks, connect Google Workspace, and enhance internal processes with Google Apps Script. An efficient solution for teams and businesses.

Augmented Coding vs. Vibe Coding

Augmented Coding vs. Vibe Coding

AI generates functional code but does not guarantee security. Learn to use it wisely to build robust, scalable, and risk-free software.

Kraneating is also about protection: the process behind our ISO 27001 certification