Data driven testing using karate DSL

Reading Time: 3 minutes

In the previous blog, we saw that we can integrate our test cases with Jenkins. Now, in this blog, we will see how we can achieve data-driven testing using karate DSL.

What is DDT?

  • DDT or data-driven testing is an technique in which we read the data from a table or an excel sheet or a CSV file (or any external file), and take them directly as input for our test cases.
  • This data is also verified by checking that the information which is being stored in the database is the same which we are sending in the request.
  • A single test case is run with multiple data set as it becomes tedious to write the same test case again and again.

What is Scenario Outline?

  • As karate follows BDD (Cucumber / Gherkin), we will be using Scenario Outline for that. Let’s see what is Scenario outline.
  • This keyword is used for running the same scenario again and again with multiple combinations of inputs.
  • Each input is taken from a table or an external file.
  • Writing the same test cases multiples times just by changing the values becomes repetitive and very tedious. And also increases the lines of code.
  • Scenario outline must contain an Examples section where the data is stored.

.feature file with Scenario Outline:

Feature: POSTCallReadingDataTable

  Background:
    * url 'https://43db3005-4ed9-4bab-b0a3-bb066e79e816.mock.pstmn.io'

  Scenario Outline: create a user from given details.

    Given path '/POSTUser'
    And request {firstName:<firstName>, lastName:<lastName>,username:<userName>,email:<email>}
    When method post
    Then status 201
    Then print response

    And match $.Status == '#present'
    And match $.Status == 'OK'

    Examples:
      | firstName       | lastName       | username       | email                |
      | John            | Wick           | john123        | johnwick@gmail.com   |
      | Wiz             | Vegas          | wizGamer       | wiz1122@gmail.com    |
      | Post            | Malone         | malone123      | postmalone@gmail.com |
  • In the above example in the request section of our test script we are using <> as a reference.
  • <firstName> is referring to the firstName field in the Examples table.
  • The examples table is divided into 2 parts, the key, and the corresponding values.
  • The first line has the key which is passed in the request section without the <>.
  • In the second line, we have defined the inputs which will replace the values in the request section with <>.
  • As we can see in the above example we have a single scenario which got executed three times as we have defined three inputs in the Examples section.

Reading data from CSV file

  • We can execute the same test by taking the values from an external CSV file as well.

CSV file

  • Content of karateTestData.csv
firstName,lastName,username,email
Mike,,Posner,mike1234@gmail.com
Steve,Aoki,steveyy,steve556677@gmail.com
Afro,Jack,afroJack,afro998877@gmail.com

Reading CSV file stored in the same directory:

Feature: POSTCallReadingDataFromCSVFile

  Background:
    * url 'https://43db3005-4ed9-4bab-b0a3-bb066e79e816.mock.pstmn.io'

  Scenario Outline: create a user from given details.

    Given path '/POSTUser'
    And request {firstName:<firstName>, lastName:<lastName>,username:<userName>,email:<email>}
    When method post
    Then status 201
    Then print response

    And match $.Status == '#present'
    And match $.Status == 'OK'

    Examples:
      | read('karateTestData.csv') |
  • | read(‘karateTestData.csv’) |will read the data from the file in JSON format and input it in the service.

So, this was a short blog on how to execute data driven testing using karate. See you in the next one.

References:

https://cucumber.io/docs/gherkin/reference/

Knoldus-blog-footer-image

Written by 

Ankur is a QA Consultant having experience of more than 1.5 years. He is familiar with the core concepts of manual and automation, postman and Newman are his expertise. He is always eager to learn new and advance concepts in order to expand his horizon and apply them in project development with his existing knowledge. His hobbies include watching web series and getting to know about all the latest gadgets that come in the market.

1 thought on “Data driven testing using karate DSL4 min read

Comments are closed.