JUnit 参数化测试 - 多维数组混合类型
JUnit Parameterized Test - multidimensional array mixed types
我在使用 JUnit 4.x 进行参数化时遇到问题。我的参数化测试包含 1 个数组混合类型 {{integer multidimensional array} 和 1 个 double} 作为参数,我很难声明它们。请参阅下面的代码。
Class 测试机器人
public class Robot {
public static double companyBotStrategy(int[][] trainingData) {
double botTime = 0;
double isCorrect = 0;
for (int i = 0; i < trainingData.length; i++) {
int[] v = trainingData[i];
if (v[1] == 1) {
botTime += v[0];
isCorrect++;
}
}
return botTime / isCorrect;
}
}
JUnit 测试参数化
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runners.Parameterized.Parameters;
public class RobotPrmtTest {
private double expected;
private int[][] trainingData;
public RobotPrmtTest(int[][] trainingData, double expected) {
this.trainingData = trainingData;
this.expected = expected;
}
@Parameters(name = "{index}")
public static Iterable<Object[]> getLoadTest() {
return Arrays.asList(new Object[][] { });
/*loadTest array mix type
* {int [][] trainingData, double expected}
* looks like it
{
{
{{ 6, 1 }, { 4, 1 }},4.5
},
{
{{4,1},{4,-1}, {0,0}, {6,1}},5.0
}
}
*/
}
@Test
public void validateParamaters() {
assertEquals("divergente", expected, Robot.companyBotStrategy(trainingData));
}
}
@Parameters(name = "{index}")
public static Iterable<Object[]> getLoadTest() {
return Arrays.asList(new Object[][] {
{
new int[][]{{6, 1}, {4, 1}}, 4.5
},
{
new int[][]{{4, 1}, {4, -1}, {0, 0}, {6, 1}}, 5.0
}
});
}
旁注:您必须在断言方法中提供增量:
double delta = 0.1; // choose something appropriate here
assertEquals("divergente", expected, Robot.companyBotStrategy(trainingData), delta);
我在使用 JUnit 4.x 进行参数化时遇到问题。我的参数化测试包含 1 个数组混合类型 {{integer multidimensional array} 和 1 个 double} 作为参数,我很难声明它们。请参阅下面的代码。
Class 测试机器人
public class Robot {
public static double companyBotStrategy(int[][] trainingData) {
double botTime = 0;
double isCorrect = 0;
for (int i = 0; i < trainingData.length; i++) {
int[] v = trainingData[i];
if (v[1] == 1) {
botTime += v[0];
isCorrect++;
}
}
return botTime / isCorrect;
}
}
JUnit 测试参数化
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runners.Parameterized.Parameters;
public class RobotPrmtTest {
private double expected;
private int[][] trainingData;
public RobotPrmtTest(int[][] trainingData, double expected) {
this.trainingData = trainingData;
this.expected = expected;
}
@Parameters(name = "{index}")
public static Iterable<Object[]> getLoadTest() {
return Arrays.asList(new Object[][] { });
/*loadTest array mix type
* {int [][] trainingData, double expected}
* looks like it
{
{
{{ 6, 1 }, { 4, 1 }},4.5
},
{
{{4,1},{4,-1}, {0,0}, {6,1}},5.0
}
}
*/
}
@Test
public void validateParamaters() {
assertEquals("divergente", expected, Robot.companyBotStrategy(trainingData));
}
}
@Parameters(name = "{index}")
public static Iterable<Object[]> getLoadTest() {
return Arrays.asList(new Object[][] {
{
new int[][]{{6, 1}, {4, 1}}, 4.5
},
{
new int[][]{{4, 1}, {4, -1}, {0, 0}, {6, 1}}, 5.0
}
});
}
旁注:您必须在断言方法中提供增量:
double delta = 0.1; // choose something appropriate here
assertEquals("divergente", expected, Robot.companyBotStrategy(trainingData), delta);