In this chapter, we will cover:
The data-driven testing approach is a widely used methodology in software test automation. We can use the same test script to check different test conditions by passing set of data to the test script.
We will use the BMI calculator application as an example to understand the data-driven testing approach.
When testing whether the BMI calculator application indicates BMI categories correctly, instead of having a separate test script for each category, we can have one script that will enter the height and weight by referring to a set of values and checking the expected values.
We can use the following combinations of test conditions to test the BMI calculator application:
Height (centimeters) |
Weight (kilograms) |
BMI |
Category |
---|---|---|---|
160 |
45 |
17.6 |
Underweight |
168 |
70 |
24.8 |
Normal |
181 |
89 |
27.2 |
Overweight |
178 |
100 |
31.6 |
Obesity |
In the simplest form, the tester supplies inputs from a row in the table and expected outputs, which occur in the same row.
In the data-driven approach, we can maintain the test data in form tables in a variety of formats, such as CSV files, Excel spreadsheets, and databases.
We implement test scripts after reading input and output values from data files row by row, then passing the values to the main test code. Then, the test code navigates through the application, executing the steps needed for the test case using the variables loaded with data values.
Data-driven tests are great for applications involving calculations for testing ranges of values, boundary values, and corner cases.
The benefits of data-driven testing are as follows:
Selenium WebDriver, being a pure browser automation API, does not provide built-in features to support data-driven testing. However, we can add support for data-driven testing using various options in Selenium WebDriver. In this chapter, we will create some basic data-driven tests in JUnit and TestNG. Later, we will build some advanced data-driven tests using different data sources in JUnit, and a data-driven test in Ruby using Roo and Python.
We will also create data driven tests for .NET bindings using NUnit and MSTest.
3.22.61.30