首页 > 技术文章 > JUnit Parametrized Tests

woniu123 2017-06-13 18:29 原文

Junit allows us to create parametrized tests. Parametrized test class has to be annotated with the @RunWith(Parameterized.class). The parametrized test class also meets below requirements:

  • It is annotated with @RunWith(Parameterized.class).
  • It has a single constructor that contains the test data.
  • It has a static method which annotated with @parameters annotation and generates and returns test data. Each test data is used as parameter for the test method.
  • It needs a test method that annotated with @test annotation.

Class Under Test: Addition.java

public class Addition {
    public int AddOperation (int a, int b) {
        int result = a+b;
        System.out.println("Addition with: " + a + " + " + b  + " = " + result);
        return result;
    }
}
package junitexamples.ParametrizedTests;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
 
import java.util.Arrays;
import java.util.Collection;
 
import static org.junit.Assert.assertEquals;
 
/**
 * Created by ONUR on 20.09.2015.
 */
@RunWith(Parameterized.class)
public class ParametrizedTestWithConstructor {
    //Private variables
    private int v1;
    private int v2;
    private int summation;
 
    //Constructor
    public ParametrizedTestWithConstructor(int p1, int p2, int p3) {
        this.v1 = p1;
        this.v2 = p2;
        this.summation = p3;
    }
 
    //Creating test data
    @Parameterized.Parameters
    public static Collection<Object[fusion_builder_container hundred_percent="yes" overflow="visible"][fusion_builder_row][fusion_builder_column type="1_1" background_position="left top" background_color="" border_size="" border_color="" border_style="solid" spacing="yes" background_image="" background_repeat="no-repeat" padding="" margin_top="0px" margin_bottom="0px" class="" id="" animation_type="" animation_speed="0.3" animation_direction="left" hide_on_mobile="no" center_content="no" min_height="none"][]> data() {
        Object[][] dataValues = new Object[][] { { 4 , 3, 7 }, { 7, 8, 15 }, { 2, 9, 11} };
        return Arrays.asList(dataValues);
    }
 
 
    //Test addoperation method of class Addition
    @Test
    public void testAddition() {
            Addition add = new Addition();
            assertEquals("Addition Failed!", summation, add.AddOperation(v1, v2));
            System.out.println("Test for " + v1 + " and " + v2 + " has been passed!\n");
    }
}

It is also possible to inject data values directly into fields without needing a constructor using the @Parameter annotation.

package junitexamples.ParametrizedTests;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
 
import java.util.Arrays;
import java.util.Collection;
 
import static org.junit.Assert.assertEquals;
 
/**
 * Created by ONUR on 20.09.2015.
 */
@RunWith(Parameterized.class)
public class ParametrizedTestWithParameterAnnotation {
    //Private variables
    //Variables using together with @Parameter must be public
    @Parameterized.Parameter (value=0)
    public int v1;
    @Parameterized.Parameter (value=1)
    public int v2;
    @Parameterized.Parameter (value=2)
    public int summation;
 
    //Creating test data
    @Parameterized.Parameters
    public static Collection<Object[]> data() {
        Object[][] data = new Object[][] { { 4 , 3, 7 }, { 7, 8, 15 }, { 2, 9, 11 } };
        return Arrays.asList(data);
    }
 
    //Test addoperation method of class Addition
    @Test
    public void testAddition() {
        Addition add = new Addition();
        assertEquals("Addition Failed!", summation, add.AddOperation(v1, v2));
        System.out.println("Test for " + v1 + " and " + v2 + " has been passed!\n");
    }
}

Output:

Addition with: 4 + 3 = 7
Test for 4 and 3 has been passed!
 
Addition with: 7 + 8 = 15
Test for 7 and 8 has been passed!
 
Addition with: 2 + 9 = 11
Test for 2 and 9 has been passed!

Summary

  • You learned how to write parametrized tests with constructor and without constructor.
  • You practiced with parametrized test examples.

Keywords

JUnit, parametrized tests, unit testing, test framework

推荐阅读