DataProvider with TestNG

In the previous example, we have 2 test cases to test against the divide method. Let say that we have more test cases for this method, we should have more test methods with mostly the same code. This separation at logical is a good for the other views. however, at the view of technical we may do some customization to remove the redundant.
All test methods have the same behavior is input 2 params and expect the is result correct. So we should create a test that accept parameters: param1, param2 and expectedResult.
Fortunately, TestNG provides a feature of DataProvider to let us do it easily. The test class now becomes

package calculator;

import aut.Calculator;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import static org.fest.assertions.api.Assertions.assertThat;

/**
 * Created by nhien.truong on 2/28/14.
 */
public class DivideTest {

    Calculator calculator=null;
    @BeforeMethod
    public void beforeMethod(){
        calculator = new Calculator();
    }

    @Test(dataProvider = "tc1_data")
    public void tc01_Valid(double param1, double param2, double expectedResult) {
        double actualResult = calculator.divide(param1, param2);

        assertThat(actualResult)
                .as("result of the divide method")
                .isEqualTo(expectedResult);
    }
    @DataProvider(name="tc1_data")
    public Object[][] makeTestDataForTestCase1(){
        return new Object[][]{
                {1,2,0.5}
                // Invalid case
                ,{1,0,Double.POSITIVE_INFINITY}
                ,{2.1,2.1,1}
        };
    }

}
And the result is
A TestNG Result
A TestNG Result

Không có nhận xét nào:

Đăng nhận xét