Parameterized Test in JUnit

Price: INR 8,999.00
Price: INR 15,999.00
Was: INR 16,999.00

Learn about the Parameterized Test in JUnit. Test case parameterization is the technique to execute the same Test Case with different input values. Parameterized tests allow developers to run the same tests over and over again using different values.

 

We will see how to parameterize the Test case in JUnit. There are standard finite steps in JUnit to make the test case parameterized test.

 

The complete use and benefit of parameterization we will see in selenium tutorials.

Let’s see the steps to make a Test case parameterized in JUnit.

 

1. Declare Class Parameterized – The very first step is to declare the class Parameterized with the help of @RunWith annotation in JUnit. Let’s see the @RunWith annotation syntax –

@RunWith(Parameterized.class)

 

2. Declare Instance Variables – The second step is to declare all the instance variable. Suppose we are reading the test data from the external Excel Sheet then declare the excel sheet column name as Instance variable.

 

3. Overload Constructor – In this step we overload the constructor of the class. The number of arguments in the constructor should be equal to number instance variables.

 

4. Use @Parameter Annotation – In this step, we will attach the @Parameter annotation with the method. This method will return the Test Data Array.

  • The @Parameters annotation injects the first value of array[0][1] to the first argument of the constructor , the second value of array[0][1] to the second argument and so on.
  • One row of array creates one test, if there are n number of an array then there are n tests.

 

5. Declare Test Method – Finally make the @Test method which will use the instance variable of the class.

 

Let’s see an example suppose we want to run our login test case with different usernames and passwords. In this example, we are not going to read the data from some external source however we are inserting the data in our parameter method.

JUnit Parametrized Test

Output:

Loginid : Merrilla@seleniumbix.com Password : password1
Loginid : anshul@seleniumbix.com Password : password

 

You can see in above example mytest() Test Class run two times. As we have provided two records from the parameter class. In selenium tutorials, we will see how parameter reads from the external source file and run test on these parameters.

 

Selenium Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

1 Comment
  1. Ulzamir Teahu 5:54 AM / March 14, 2017 - Reply

    cartierbraceletlove Adoro glace lobozinho, o que vc acha de passar ele pra mim??

Leave a Reply