Bharathan Kumaran
Bharathan Kumaran

Reputation: 1056

How to add assertions to Galting test using Karate?

We have an API which will return 200 or 400 based on slots available, We need to write a test, which triggers 20 requests for 10 slots Need to assert 10 success 10 failure for the same test, No sure, This scenario is a right use case to use karate gatling, As our entire project was using the karate for our functional testing,

Problem Statement We have api which will register users for sessions based on the slots available (you can consider these tickets for the movie). We have implemented in such a way if there are 10 slots, 20 requests come in => 10 will get success, 10 will get failure. We wrote a karate test for the above scenario and used Gatling with twenty users, On looking into the Gatling reports, All works perfectly. 10 Success, 10 failure in the report.

Challenge comes when the Gatling build will not fail for the above scenario, it will generate a report. I tried to use Gatling assertions as part of the simulation file, but the assertion object is empty.

 "assertions": []

Sample test

Scenario: Register slot for multiple users to test concurrent booking
    * def bearerAuthorization = 'Bearer ' + accessToken
    * def slotId = 1
    * print slotId
    Given path 'movie/slots/' + slotId + '/registrations'
    And header Authorization = 'Bearer ' + accessToken
    When request {}
    When method POST
    Then status 200

Simulation script

val sessionApi = scenario("sessions-api")
    .feed(users)
    .exec(karateFeature("classpath:featureTests/register-sessions-concurrent.feature"))

setUp(
    sessionApi.inject(rampUsers(10) during (2 seconds)).protocols(protocol)
)

Note: I have tried below approaches, 1. tried to create a variable as successCount at feature level and tried to update the depending upon the status of the req, the global variable gets overridden on each gatling execution and not sure on how to use it for making the build failure 2. Tried using the gatling assertions, for specific using details("Index") but need to know the "group" of the feature test.

Upvotes: 1

Views: 581

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58088

One idea I have is you can design a Java singleton - and update it in your feature files. You just need to take care of concurrency, and a simple way would be to use an AtomicInteger or something like that.

You can easily get a reference to this singleton from the Scala / Gatling test. And then you should be able to figure out a way in Gatling to manually fail the test.

Upvotes: 0

Related Questions