Keyword and Data Driven Test Cases


Advertisements

The workflow of a test-case can be tested using keyword or data driven style. In case you want to test the workflow with different inputs, the same can be done using data driven test cases. We will work on an example to go through the following test case approaches −

  • Keyword Driven style
  • Data Driven style

Keyword Driven Style

We will do a project setup to show the working of Keyword driven style.

Open ride using ride.py from the command line.

Keyword Driven Style

Click on New Project and give a name to your project.

Keyword Driven Style Ex

The name given to the project is keywordstyle. Click OK to save the project. In this project, we will create a user keyword as shown below.

Right-click on the name of the project and click on New User Keyword as shown below

New User Keyword

It will display screen as follows −

display screen Ex

Enter the name of the keyword and the arguments it will take. Here we will give name of the keyword as Display Message. The role of Keyword Display Message is, when it is called, it will log a message. So we need to give an argument to it. Therefore, in the above example the argument will be a scalar variable ${msg}.

log message

Click OK to save the user keyword. Now we need to write the action the keywords need to do. So, it will have tabular format as shown below where we can give the Library keywords or built-in keywords available with Robot Framework.

Here, we will use a simple Log keyword available with Robot Framework as shown below −

log keyword Ex

To get more keywords available with Robot framework, press ctrl + space bar in the table column as shown below −

table column

So the keyword we want to use with our testcase is ready. The name of the user keyword is Display Message and it takes one argument called ${msg}.

Let us now use this keyword in simple keyword driven style test-case. To do that we need to create test case. Right-click on the name of the project created. Now, click New Test Case −

keyword driven

simple keyword

Give name to the test case and click OK.

We are done with the project setup and now will write test cases for the keyword driven style.

In the test case, we have used the user-defined keyword Display Message in the tabular format as shown below −

user defined keyword Ex

We have used the keyword we have created as shown above and passed the value Hello World.

We will execute the test case TC1 and check the output −

execute test case Ex

In the above example, we have written a simple test-case which logs message and the test case is executed with output Hello World. We can see the output Hello World printed in the log. The test case is also passed here.

Data Driven Style

We will create one more test case in the same project. We will give the name of the test-case as TC2.

more test case

To work with data driven style, we need to create template. Template will take the name of the high level keyword, which is a user-defined keyword like the one we created at the start called Display Message. The arguments to that template will be sent in the form of test-cases. We can pass different values to that template keyword. The data driven approach is mostly used when you want to test the scenario with different data to it.

Once the test case is saved. Click on the test case and the display will be as follows −

data driven style

Click on Edit button for Template and add the user-defined keyword.

Edit button

Enter the user keyword for the template and click OK to save the template.

user keyword

Display Message keyword takes one argument called ${msg}. This is a scalar variable. The details passed in this test case will act as arguments to the user-defined keyword Display Message.

keyword Display Message

In TC2, we have added Template Display Message (user-defined keyword). We have given messages in the tabular format.

Let us now execute the test case.

Template Display Message

We can see Run executes both the Test Cases. The output shown for TC1 is Hello World. This was the message we had given to the User Keyword Display Message.

For TC2, we used Display Message as a Template. We passed My First Test Case and Testing Template as values in TC2. As the user keyword Display Message uses internally Log Keyword, it displays the message in the log as shown above.

Conclusion

We have used keyword style and data driven style in this chapter and seen the working of both. Data Driven style takes high-level user-defined keyword as a template and all the test cases act as values to the template.

Advertisements