Creating a data-driven test using JUnit

JUnit is a popular testing framework used to create Selenium WebDriver tests in Java. We can create data-driven Selenium WebDriver tests using the JUnit 4 parameterization feature. This can be done by using the JUnit parameterized class runner.

In this recipe, we will create a simple JUnit test case to test our BMI calculator application. We will specify the test data within our JUnit test case class. We will use various JUnit annotations to create a data-driven test.

Getting ready

The following are the things we need to do:

  • Set up a new project and add JUnit4 to the project's build path. You can set up the project in an IDE of your choice.
  • Identify the set of values that need to be tested.

How to do it...

Let's create a data-driven test using JUnit by following these steps:

  1. Create a new JUnit test class that uses a parameterized runner using @RunWith(Parameterized.class):
    package com.secookbook.examples.chapter07;
    
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.By;
    import org.junit.*;
    import org.junit.runner.RunWith;
    import org.junit.runners.Parameterized.Parameters;
    import org.junit.runners.Parameterized;
    
    import static org.junit.Assert.*;
    
    import java.util.Arrays;
    import java.util.List;
    
    @RunWith(Parameterized.class)
    public class SimpleDDT {
    
      private static WebDriver driver;
    
    }
  2. Declare instance variables for the parameterized values in the SimpleDDT class:
    private String height;
    private String weight;
    private String bmi;
    private String bmiCategory;
  3. Define a method that will return the collection of parameters to the SimpleDDT class by using the @Parameters annotation:
    @Parameters
    public static List<String[]> testData() {
      return Arrays.asList(new String[][] {
          { "160", "45", "17.6", "Underweight" },
          { "168", "70", "24.8", "Normal" },
          { "181", "89", "27.2", "Overweight" },
          { "178", "100", "31.6", "Obesity" } });
    }
  4. Add a constructor to the SimpleDDT class, which will be used by the test runner to pass the parameters to the SimpleDDT class instance:
    public SimpleDDT(String height, String weight, String bmi, String bmiCategory)
    {
      this.height = height;
      this.weight = weight;
      this.bmi = bmi;
      this.bmiCategory = bmiCategory;
    }
  5. Finally, add the test case method testBMICalculator() that uses parameterized variables. Also, add the setup()and teardown() methods to the SimpleDDT class:
    @Test
    public void testBMICalculator() throws Exception {
      // Get the Height element and set the value using height variable
      WebElement heightField = driver.findElement(By.name("heightCMS"));
      heightField.clear();
      heightField.sendKeys(height);
    
      // Get the Weight element and set the value using weight variable
      WebElement weightField = driver.findElement(By.name("weightKg"));
      weightField.clear();
      weightField.sendKeys(weight);
    
      // Click on Calculate Button
      WebElement calculateButton = driver.findElement(By.id("Calculate"));
      calculateButton.click();
    
      // Get the Bmi element and verify its value using bmi variable
      WebElement bmiLabel = driver.findElement(By.name("bmi"));
      assertEquals(bmi, bmiLabel.getAttribute("value"));
    
      // Get the Bmi Category element and verify its value using
      // bmiCategory variable
      WebElement bmiCategoryLabel = driver.findElement(By.name("bmi_category"));
      assertEquals(bmiCategory, bmiCategoryLabel.getAttribute("value"));
    }

How it works...

When the test is executed for each row in the test data collection, the test runner will instantiate the test case class, passing the test data as parameters to the SimpleDDT class constructor. It will then execute all the tests in the SimpleDDT class.

Instance variables are declared to store the test data passed by the test runner, as shown in the following code snippet:

private String height;
private String weight;
private String bmi;
private String bmiCategory;

In the test case class constructor, these variables are assigned with values at runtime by the test runner from the test data collection, using the test method arguments as shown in following code:

public SimpleDDT(String height, String weight, String bmi, String bmiCategory)
{
  this.height = height;
  this.weight = weight;
  this.bmi = bmi;
  this.bmiCategory = bmiCategory;
}

In the testBMICalculator() method, we passed these variables to Selenium WebDriver API. For example, to enter a value in the height field, we used the instance variable of the test case class as follows:

WebElement heightField = driver.findElement(By.name("heightCMS"));
heightField.sendKeys(this.height);

The expected result is also parameterized using the instance variable for BMI value, as shown in following code:

WebElement bmiLabel = driver.findElement(By.name("bmi"));
assertEquals(this.bmi, bmiLabel.getAttribute("value"));

JUnit will display results for each set of test data along with the time taken to execute it, as shown in the following screenshot:

How it works...

This is one of the simplest ways to parameterize a test; however, test data is hardcoded within the test case class, which could become difficult to maintain. It is always recommended that we store the test data in an external source such as a CSV, Excel, or database file for easier maintenance.

See also

  • The Reading test data from a CSV file using JUnit recipe
  • The Reading test data from an Excel file using JUnit and Apache POI recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.37.10