具有多个测试的参数化 junit 测试
Parameterized junit testing with more than one test
参数化测试有利于将不同的数据输入到您的测试中。但是,我创建了一个示例计算器,我想为其创建参数化测试。但是,我发现您只能为单个测试创建一组参数化数据。
我已经创建了参数化测试,用于将 2 个数字相加并获得预期结果。此数据不适用于减法,因为预期结果会有所不同。
是否可以为加减乘除的每个测试提供参数化数据?
非常感谢您的任何建议,
@RunWith(Parameterized.class)
public class CalculatorModelPresenterTest {
private CalculatorModel mCalculatorModel;
/* Array of tests */
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{3.0, 4.0, 7.0},
{4.0, 3.0, 7.0},
{8.0, 2.0, 10.0},
{-1.0, 4.0, 3.0},
{3256.0, 4.0, 3260.0}
});
}
private double mNumberOne;
private double mNumberTwo;
private double mExpectedResult;
/* CONSTRUCTOR THAT ASSIGNS THE FIELDS WITH THE TEST DATA */
public CalculatorModelPresenterTest(double numberOne, double numberTwo, double expectedResult) {
mNumberOne = numberOne;
mNumberTwo = numberTwo;
mExpectedResult = expectedResult;
}
/* THIS TEST WILL PASS AS THE TEST DATA IS FOR ADDING */
@Test
public void testAdd() throws Exception {
final double actualResult = mCalculatorModel.add(mNumberOne, mNumberTwo);
assertEquals(actualResult, mExpectedResult, 0);
}
/* HOWEVER, THIS TEST WILL ALWAYS FAIL AS THE TEST DATA IS CUSTOMIZED FOR THE ADD */
@Test
public void testSub() throws Exception {
final double actualResult = mCalculatorModel.sub(mNumberOne, mNumberTwo);
assertEquals(actualResult, mExpectedResult, 0);
}
@Before
public void setUp() throws Exception {
mCalculatorModel = new CalculatorModel();
}
@After
public void tearDown() throws Exception {
mCalculatorModel = null;
}
}
在 JUnit 4 中,您不能有多个 @Parameters
方法。您需要为每个使用新参数的操作添加一个新的 class。
只需像这样添加另一个参数:
return Arrays.asList(new Object[][]{
{3.0, 4.0, 7.0, -1.0},
{4.0, 3.0, 7.0, 1.0},
{8.0, 2.0, 10.0, 6.0},
{-1.0, 4.0, 3.0, -5.0},
{3256.0, 4.0, 3260.0, 3252.0}
});
...
private double mExpectedSubResult;
/* CONSTRUCTOR THAT ASSIGNS THE FIELDS WITH THE TEST DATA */
public CalculatorModelPresenterTest(double numberOne, double numberTwo, double expectedResult, double expectedSubResult) {
mNumberOne = numberOne;
mNumberTwo = numberTwo;
mExpectedResult = expectedResult;
mExpectedSubResult = expectedSubResult;
}
...
/* THIS TEST WILL NOW PASS */
@Test
public void testSub() throws Exception {
final double actualResult = mCalculatorModel.sub(mNumberOne, mNumberTwo);
assertEquals(actualResult, mExpectedSubResult, 0);
}
您可以为每个测试编写一个内部 class,每个测试都有一组不同的参数,并且 运行 它带有 Enclosed
运行ner。
@RunWith(Enclosed.class)
public class CalculatorModelPresenterTest {
@RunWith(Parameterized.class)
public static class Sum {
@Parameterized.Parameters
public static Object[][] data() {
return new Object[][]{
{3.0, 4.0, 7.0},
{4.0, 3.0, 7.0},
{8.0, 2.0, 10.0},
{-1.0, 4.0, 3.0},
{3256.0, 4.0, 3260.0}
};
}
@Parameterized.Parameter(0)
private double mNumberOne;
@Parameterized.Parameter(1)
private double mNumberTwo;
@Parameterized.Parameter(2)
private double mExpectedResult;
@Test
public void testAdd() throws Exception {
CalculatorModel calculatorModel = new CalculatorModel();
double actualResult = calculatorModel.add(mNumberOne, mNumberTwo);
assertEquals(actualResult, mExpectedResult, 0);
}
}
@RunWith(Parameterized.class)
public static class Difference {
@Parameterized.Parameters
public static Object[][] data() {
return new Object[][]{
{3.0, 4.0, -1.0},
{4.0, 3.0, 1.0},
{8.0, 2.0, 6.0},
{-1.0, 4.0, -5.0},
{3256.0, 4.0, 3252.0}
};
}
@Parameterized.Parameter(0)
private double mNumberOne;
@Parameterized.Parameter(1)
private double mNumberTwo;
@Parameterized.Parameter(2)
private double mExpectedResult;
@Test
public void testSub() throws Exception {
CalculatorModel calculatorModel = new CalculatorModel();
double actualResult = calculatorModel.sub(mNumberOne, mNumberTwo);
assertEquals(actualResult, mExpectedResult, 0);
}
}
}
另一种方法是使用 JUnitParams 运行ner.
@RunWith(JUnitParamsRunner.class)
public class CalculatorModelPresenterTest {
@Test
@Parameters({
"3.0, 4.0, 7.0",
"4.0, 3.0, 7.0",
"8.0, 2.0, 10.0",
"-1.0, 4.0, 3.0",
"3256.0, 4.0, 3260.0" })
public void testAdd(double first, double second, double expectedResult)
throws Exception {
CalculatorModel calculatorModel = new CalculatorModel();
double actualResult = calculatorModel.add(first, second);
assertEquals(actualResult, expectedResult, 0);
}
@Test
@Parameters({
"3.0, 4.0, -1.0",
"4.0, 3.0, 1.0",
"8.0, 2.0, 6.0",
"-1.0, 4.0, -5.0",
"3256.0, 4.0, 3252.0" })
public void testSub(double first, double second, double expectedResult)
throws Exception {
CalculatorModel calculatorModel = new CalculatorModel();
double actualResult = calculatorModel.sub(first, second);
assertEquals(actualResult, expectedResult, 0);
}
}
参数化测试有利于将不同的数据输入到您的测试中。但是,我创建了一个示例计算器,我想为其创建参数化测试。但是,我发现您只能为单个测试创建一组参数化数据。
我已经创建了参数化测试,用于将 2 个数字相加并获得预期结果。此数据不适用于减法,因为预期结果会有所不同。
是否可以为加减乘除的每个测试提供参数化数据?
非常感谢您的任何建议,
@RunWith(Parameterized.class)
public class CalculatorModelPresenterTest {
private CalculatorModel mCalculatorModel;
/* Array of tests */
@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{3.0, 4.0, 7.0},
{4.0, 3.0, 7.0},
{8.0, 2.0, 10.0},
{-1.0, 4.0, 3.0},
{3256.0, 4.0, 3260.0}
});
}
private double mNumberOne;
private double mNumberTwo;
private double mExpectedResult;
/* CONSTRUCTOR THAT ASSIGNS THE FIELDS WITH THE TEST DATA */
public CalculatorModelPresenterTest(double numberOne, double numberTwo, double expectedResult) {
mNumberOne = numberOne;
mNumberTwo = numberTwo;
mExpectedResult = expectedResult;
}
/* THIS TEST WILL PASS AS THE TEST DATA IS FOR ADDING */
@Test
public void testAdd() throws Exception {
final double actualResult = mCalculatorModel.add(mNumberOne, mNumberTwo);
assertEquals(actualResult, mExpectedResult, 0);
}
/* HOWEVER, THIS TEST WILL ALWAYS FAIL AS THE TEST DATA IS CUSTOMIZED FOR THE ADD */
@Test
public void testSub() throws Exception {
final double actualResult = mCalculatorModel.sub(mNumberOne, mNumberTwo);
assertEquals(actualResult, mExpectedResult, 0);
}
@Before
public void setUp() throws Exception {
mCalculatorModel = new CalculatorModel();
}
@After
public void tearDown() throws Exception {
mCalculatorModel = null;
}
}
在 JUnit 4 中,您不能有多个 @Parameters
方法。您需要为每个使用新参数的操作添加一个新的 class。
只需像这样添加另一个参数:
return Arrays.asList(new Object[][]{
{3.0, 4.0, 7.0, -1.0},
{4.0, 3.0, 7.0, 1.0},
{8.0, 2.0, 10.0, 6.0},
{-1.0, 4.0, 3.0, -5.0},
{3256.0, 4.0, 3260.0, 3252.0}
});
...
private double mExpectedSubResult;
/* CONSTRUCTOR THAT ASSIGNS THE FIELDS WITH THE TEST DATA */
public CalculatorModelPresenterTest(double numberOne, double numberTwo, double expectedResult, double expectedSubResult) {
mNumberOne = numberOne;
mNumberTwo = numberTwo;
mExpectedResult = expectedResult;
mExpectedSubResult = expectedSubResult;
}
...
/* THIS TEST WILL NOW PASS */
@Test
public void testSub() throws Exception {
final double actualResult = mCalculatorModel.sub(mNumberOne, mNumberTwo);
assertEquals(actualResult, mExpectedSubResult, 0);
}
您可以为每个测试编写一个内部 class,每个测试都有一组不同的参数,并且 运行 它带有 Enclosed
运行ner。
@RunWith(Enclosed.class)
public class CalculatorModelPresenterTest {
@RunWith(Parameterized.class)
public static class Sum {
@Parameterized.Parameters
public static Object[][] data() {
return new Object[][]{
{3.0, 4.0, 7.0},
{4.0, 3.0, 7.0},
{8.0, 2.0, 10.0},
{-1.0, 4.0, 3.0},
{3256.0, 4.0, 3260.0}
};
}
@Parameterized.Parameter(0)
private double mNumberOne;
@Parameterized.Parameter(1)
private double mNumberTwo;
@Parameterized.Parameter(2)
private double mExpectedResult;
@Test
public void testAdd() throws Exception {
CalculatorModel calculatorModel = new CalculatorModel();
double actualResult = calculatorModel.add(mNumberOne, mNumberTwo);
assertEquals(actualResult, mExpectedResult, 0);
}
}
@RunWith(Parameterized.class)
public static class Difference {
@Parameterized.Parameters
public static Object[][] data() {
return new Object[][]{
{3.0, 4.0, -1.0},
{4.0, 3.0, 1.0},
{8.0, 2.0, 6.0},
{-1.0, 4.0, -5.0},
{3256.0, 4.0, 3252.0}
};
}
@Parameterized.Parameter(0)
private double mNumberOne;
@Parameterized.Parameter(1)
private double mNumberTwo;
@Parameterized.Parameter(2)
private double mExpectedResult;
@Test
public void testSub() throws Exception {
CalculatorModel calculatorModel = new CalculatorModel();
double actualResult = calculatorModel.sub(mNumberOne, mNumberTwo);
assertEquals(actualResult, mExpectedResult, 0);
}
}
}
另一种方法是使用 JUnitParams 运行ner.
@RunWith(JUnitParamsRunner.class)
public class CalculatorModelPresenterTest {
@Test
@Parameters({
"3.0, 4.0, 7.0",
"4.0, 3.0, 7.0",
"8.0, 2.0, 10.0",
"-1.0, 4.0, 3.0",
"3256.0, 4.0, 3260.0" })
public void testAdd(double first, double second, double expectedResult)
throws Exception {
CalculatorModel calculatorModel = new CalculatorModel();
double actualResult = calculatorModel.add(first, second);
assertEquals(actualResult, expectedResult, 0);
}
@Test
@Parameters({
"3.0, 4.0, -1.0",
"4.0, 3.0, 1.0",
"8.0, 2.0, 6.0",
"-1.0, 4.0, -5.0",
"3256.0, 4.0, 3252.0" })
public void testSub(double first, double second, double expectedResult)
throws Exception {
CalculatorModel calculatorModel = new CalculatorModel();
double actualResult = calculatorModel.sub(first, second);
assertEquals(actualResult, expectedResult, 0);
}
}