Karate with Gatling

Reading Time: 2 minutes

Hey!

As a tester, we may have come across a situation where we might have do functional and performance testing of the API. Using two different framework for functional and performance testing is quite troublesome. What if we have an unified framework which can do functional as well as load testing, wouldn’t it be great?

We can use Karate for API functional testing and can use the same test for performance testing. In this blog, I will try to demonstrate the integration of Gatling with Karate framework.

Karate along with Gatling

The additional capability to re-use functional tests as performance-tests via integration with the Gatling tool was released in 2018. Following are the capabilities of Karate-Gatling framework.

  • Re-uses Karate tests as performance tests executed by Gatling.
  • Uses Gatling (and Scala) only for defining the load-model, everything else can be in Karate.
  • Karate assertion failures appear in Gatling report, along with the line-numbers that has failed.

Setting up Gatling with Karate

For maven, we would have to add the karate-gatling dependency in our pom.xml. We may find the latest version from here https://mvnrepository.com/artifact/com.intuit.karate/karate-gatling.

<dependency>
    <groupId>com.intuit.karate</groupId>
    <artifactId>karate-gatling</artifactId>
    <version>${karate.version}</version>
    <scope>test</scope>
</dependency>  

We also need to add karate-apache dependency as well if it is not already there in our pom.xml.

    <dependency>
        <groupId>com.intuit.karate</groupId>
        <artifactId>karate-apache</artifactId>
        <version>${karate.version}</version>
        <scope>test</scope>
    </dependency>

Other than these, we will also be required to add Gatling maven plugin. We can find the latest one form here https://search.maven.org/search?q=g:io.gatling%20AND%20a:gatling-maven-plugin&core=gav.

I personally recommend to add scala-maven plugin as well, since it is not mentioned in the Karate documentation, because Gatling simulation scripts are written in scala but don’t panic!. We can use all the basic functions of Gatling without knowing much about scala. In most situations, the DSL will cover most of our needs and we’ll be able to build our scenarios.

    <plugins>
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <version>${scala.plugin.version}</version>
        </plugin>
        <plugin>
            <groupId>io.gatling</groupId>
            <artifactId>gatling-maven-plugin</artifactId>
            <version>${gatling.plugin.version}</version>
            <configuration>
                <simulationsFolder>src/test/java</simulationsFolder>
                <includes>
                    <include>Performance.GatlingTest</include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

<execution> phase is defined because we can just run mvn clean test and it will work for Gatling tests as well. If we don’t want to run Gatling tests as part of the normal Maven “test” lifecycle, we can avoid the <executions> section and instead manually invoke the Gatling plugin from the command-line.

mvn clean test-compile gatling:test

And in case we have multiple Gatling simulation files and we want to choose only one to run, we may use the following command.

mvn clean test-compile gatling:test -Dgatling.simulationClass=Performance.GatlingTest

Gatling script with Karate feature file

Let’s have a look over the a very simple and plane gatling script which uses Karate feature file.

package Performance

import com.intuit.karate.gatling.PreDef._
import io.gatling.core.Predef._
import io.gatling.core.scenario.Simulation


class GatlingTest extends Simulation{

  val getTest = scenario("get the test feature file").exec(karateFeature("classpath:Performance/sample.feature"))

  setUp(getTest.inject(atOnceUsers(users = 1)))

}

Let’s have a look at the sample feature file for API’s functional testing. I’ve used a sample API from https://reqres.in/.

Feature:  Demo test

  @Demo
  Scenario: Sample Api to be tested
    Given url 'https://reqres.in/api/users?page=2'
    When method GET
    Then status 200
    And print response
    And match $.page == '#present'


karateFeature():

This declares a whole Karate feature as a “flow”. Note how we can have concurrent flows in the same Gatling simulation.

Tag Selector

A single scenario (or multiple) can be “chosen” by appending the tag name to the feature path. This allows us to re-use only selected tests out of our existing functional or regression test suites for composing a performance test-suite. For example

val delete = scenario("delete").exec(karateFeature("classpath:Performance/sample.feature@name=delete"))

This is pretty much the basic that we need to know for integrating Gatling with Karate framework. I hope you may have found this useful.

Thanks.

Reference:

https://github.com/intuit/karate

https://github.com/intuit/karate/tree/master/karate-gatling

Knoldus-blog-footer-image

Leave a Reply