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 |

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