Data Driven Testing using table keyword in Karate

Karate is the only open-source tool to combine API test-automation, mocks, performance-testing and even UI automation into a single, unified framework. The syntax is language-neutral, and easy for even non-programmers. Assertions and HTML reports are built-in, and you can run tests in parallel for speed. Today we will be focusing on data driven testing in Karate using table keyword.

The following concepts will be covered along with Data Driven Testing in this blog.

  1. How to call a feature file inside another feature file?

  2. Use of embedded expressions to pass payload with request keyword and do assertion along with match keyword

  3. How to call another scenario within the same feature file using a tag?

For this blog, I will be using fake json server for test APIs. The documentation for setting up the server is given in the above link.

DDTUsingTableKeyword.feature

@ignore
Feature: DDTUsingTableKeyword demo feature

  Background: api end point url
    * url 'http://localhost:3000/posts'

  Scenario: create a new post
    #Given url 'http://localhost:3000/posts'
    #payload data will be provided from TableKeyword.feature file under com.tests package
    #embedded expressions is used to pass payload with request keyword
    Given request {"title": '#(title)',"author": '#(author)'}
    When method post
    Then status 201
    #embedded expressions can also be used with match keyword for assertion
    Then match $ == {title: '#(title)',author: '#(author)',id: #notnull}

In the above feature file @ignore tag is used to ignore the feature file during execution. # symbol is used to add comments in feature file. Embedded expressions is used which will act as a template to pass the payload with request keyword and can be also used to do assertions with match keyword. The above feature file will be called in the TableKeyword.feature using call keyword in Karate.

TableKeyword.feature

Feature: table keyword demo data driven testing feature

  Scenario: table keyword demo data driven testing scenario
    #table keyword can be used for passing different sets of payload to
    #test negative and positive cases. Mixing of data sets with both
    #negative and positive scenarios is not recommended. Always prefer
    #seperate scenarios for negative and positive cases
    * table posts
      | title    | author    |
      | 'title7' | 'author7' |
      | 'title8' | 'author8' |
      | 'title9' | 'author9' |

  #calling a feature file using call keyword
  * call read('DDTUsingTableKeyword.feature') posts

In TableKeyword.feature file a table has been created using table keyword with name ‘posts’ with column names ‘title’ and ‘author.’ Three rows of data are provided in table which will be supplied to ignored feature file DDTUsingTableKeyword.feature using the embedded expressions. The DDTUsingTableKeyword.feature file is called in the TableKeyword.feature using call keyword and table name posts is given as an argument to the feature file. Default HTML report generated after running TableKeyword.feature

So far we have discussed first two concepts along with Data Driven Testing. Now we are going to discuss the last concept, calling another scenario within the same feature file using a tag. To discuss the concept, will be using the same TableKeyword.feature and will be adding a second scenario in the same feature file. The second scenario will be called in the first scenario within the same feature file. The second scenario is been ignored using @ignore tag and a tag @post is also added to the same scenario which will be used while calling the second scenario.

TableKeyword.feature after adding second scenario

Feature: table keyword demo data driven testing feature

  Scenario: table keyword demo data driven testing scenario
    #table keyword can be used for passing different sets of payload to
    #test negative and positive cases. Mixing of data sets with both
    #negative and positive scenarios is not recommended. Always prefer
    #seperate scenarios for negative and positive cases
    * table posts
      | title    | author    |
      | 'title7' | 'author7' |
      | 'title8' | 'author8' |
      | 'title9' | 'author9' |

  #calling a feature file using call keyword
  #* call read('DDTUsingTableKeyword.feature') posts
  #Below two lines are equivalent, since called scenario is from same feature file, it is ok
  #ignore feature file name or can be mentioned as well. Both options are fine.
  #notice the use of tag '@post'while calling the scenario using call keyword
  * call read('@post') posts
  #* call read('TableKeyword.feature@post') posts
  @ignore @post
  Scenario: create a new post
    Given url 'http://localhost:3000/posts'
    #payload data will be provided from TableKeyword.feature file under com.tests package
    #embedded expressions is used to pass payload with request keyword
    Given request {"title": '#(title)',"author": '#(author)'}
    When method post
    Then status 201
    #embedded expressions can also be used with match keyword for assertion
    Then match $ == {title: '#(title)',author: '#(author)',id: #notnull}

Default HTML report generated after running TableKeyword.feature

Github link: https://github.com/thetestinginstinct/KarateAPITest/tree/main/src/test/java/com/tests

Note: I am using Eclipse IDE and using cucumber eclipse plugin to run the feature file.