如何在参数化测试中对测试数据进行分组?
我正在开发一个具有网格的应用程序,并且只有网格的某些点被认为是有效的。我需要使用所有可能的网格值或至少使用所有边界点进行广泛的测试。
我尝试过参数化测试。除了数据在某一点之后变得难以管理之外,它工作得很好。下面给出了 3x3 网格的示例测试。
@RunWith(Parameterized.class)
public class GridGameTest {
@Parameters
public static Collection<Object[]> data(){
return Arrays.asList(new Object[][] {
{ 0, 0, false }, { 0, 1, false }, { 0, 2, false },
{ 1, 0, false }, { 1, 1, true }, { 1, 2, false },
{ 2, 0, false }, { 2, 1, false }, { 2, 2, false }
} );
}
private final int x;
private final int y;
private final boolean isValid;
public GridGameTest(int x, int y, boolean isValid){
this.x = x;
this.y = y;
this.isValid = isValid;
}
@Test
public void testParameterizedInput(){
Grid grid = new Grid(3,3);
assertEquals(isValid, grid.isPointValid(new Point(x,y)));
}
}
关于如何分组/管理数据的任何输入,以便我的测试保持简单和可读?
I'm working on an application which has a grid and only some points of the grid are considered valid. I need to test this extensively with all possible grid values or at least with all the boundary points.
I've tried out parameterized tests. It works fine expect for the fact that the data becomes unmanageable after a point. Sample test for a 3x3 grid is given below.
@RunWith(Parameterized.class)
public class GridGameTest {
@Parameters
public static Collection<Object[]> data(){
return Arrays.asList(new Object[][] {
{ 0, 0, false }, { 0, 1, false }, { 0, 2, false },
{ 1, 0, false }, { 1, 1, true }, { 1, 2, false },
{ 2, 0, false }, { 2, 1, false }, { 2, 2, false }
} );
}
private final int x;
private final int y;
private final boolean isValid;
public GridGameTest(int x, int y, boolean isValid){
this.x = x;
this.y = y;
this.isValid = isValid;
}
@Test
public void testParameterizedInput(){
Grid grid = new Grid(3,3);
assertEquals(isValid, grid.isPointValid(new Point(x,y)));
}
}
Any inputs on how to group/manage the data, so that my test remains simple and readable??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将创建一个数据生成器,而不必对所有可能的值进行硬编码。类似于:
失败的测试无论如何都会打印参数。
I would create a data generator instead of having to hardcode all possible values. Something like:
Failed tests are anyway printing parameters.
我会将测试分为两组。有效点和无效点。如果确实有很多点,那么使用 @Parameterized 来生成它们而不是列出它们。或使用 JunitParams 从文件中读取它们。如果您希望将所有点保留在源文件中,那么我建议使用 zohhak:
i would separate tests into 2 groups. valid and invalid points. if there is really many points then use
@Parameterized
to generate them instead of listing them. or use JunitParams to read them from file. if you prefer to keep all the points in the source file then i suggest using zohhak: