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.
How to call a feature file inside another feature file?
Use of embedded expressions to pass payload with request keyword and do assertion along with match keyword
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.
Subscribe to my newsletter
Read articles from cyril george directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
cyril george
cyril george
I am a software test engineer with more than 7 years of work experience in product and service based companies. I have experience in ui automation, backend API and mobile testing. I am a conscientious person who works hard and pays attention to detail. I'm flexible, quick to pick up new skills and eager to learn from others. I also have lots of ideas and enthusiasm. AUTOMATION FRAMEWORK & DEVOPS SKILLS: Data Driven, Key Word(Robot Framework), Hybrid, Page Object Model, Selenium WebDriver with Java & Python , BDD (Cucumber-JVM), TestNG, Junit, Pytest, Extent Report, Allure Report, Apache Log4j, SVN, GIT, GitHub, Bit Bucket, Gitlab, MAVEN, Jenkins, Docker, Zalenium, TestProject, Karate Ui Testing, Playwright with Java & Python, Selenide, SeleniumBase with Python. API (BACKEND) WEBSERVICES AUTOMATION: SOAP, REST, Rest Assured, Postman, Newman, Swagger, Karate, Insomnia, Python Requests, Robot framework, Hoppscotch. DATABASE: Knowledge in My SQL , SQL Server. Knowledge in writing and executing SQL statements. Database testing using JDBC. JAVASCRIPT TOOLS: Protractor, Taiko, Webdriver IO. PERFORMANCE TESTING: Knowledge in Jmeter, Karate with Gatling, Locust. PROJECT/TEST MANAGEMENT TOOLS: TestLink, SquashTM, Redmine, Qmetry, Youtrack, Assembla, Jira, Confluence, Waterfall, Agile(Scrum). MOBILE TEST AUTOMATION: Knowledge in Appium and TestProject. PROGRAMMING LANGUAGES: Java, PHP, JavaScript, Python. IDE: Eclipse, Visual Studio Code, PyCharm, Intellij IDEA