如何在参数化测试中对测试数据进行分组?

发布于 2024-11-09 12:34:02 字数 1003 浏览 1 评论 0原文

我正在开发一个具有网格的应用程序,并且只有网格的某些点被认为是有效的。我需要使用所有可能的网格值或至少使用所有边界点进行广泛的测试。

我尝试过参数化测试。除了数据在某一点之后变得难以管理之外,它工作得很好。下面给出了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三生一梦 2024-11-16 12:34:02

我将创建一个数据生成器,而不必对所有可能的值进行硬编码。类似于:

public static Collection<Object[]> data(){
    Object[][] result = new Object[3][3];
    for (Boolean flag : new Boolean[]{Boolean.FALSE, Boolean.TRUE})
    {
      for (int i = 0; i < 3; i++)
      {
        for (int j = 0; j < 3; j++)
        {
          Object[] row = new Object[] {j, i, flag};
          result[i][j] = row;
        }
      }
    }
    return Arrays.asList(result);
}

失败的测试无论如何都会打印参数。

I would create a data generator instead of having to hardcode all possible values. Something like:

public static Collection<Object[]> data(){
    Object[][] result = new Object[3][3];
    for (Boolean flag : new Boolean[]{Boolean.FALSE, Boolean.TRUE})
    {
      for (int i = 0; i < 3; i++)
      {
        for (int j = 0; j < 3; j++)
        {
          Object[] row = new Object[] {j, i, flag};
          result[i][j] = row;
        }
      }
    }
    return Arrays.asList(result);
}

Failed tests are anyway printing parameters.

め七分饶幸 2024-11-16 12:34:02

我会将测试分为两组。有效点和无效点。如果确实有很多点,那么使用 @Parameterized 来生成它们而不是列出它们。或使用 JunitParams 从文件中读取它们。如果您希望将所有点保留在源文件中,那么我建议使用 zohhak

import static java.lang.Integer.parseInt;
import static junit.framework.Assert.*;
import org.junit.runner.RunWith;
import com.googlecode.zohhak.api.Coercion;
import com.googlecode.zohhak.api.TestWith;
import com.googlecode.zohhak.api.runners.ZohhakRunner;

@RunWith(ZohhakRunner.class)
public class MyTest {

    Grid grid = new Grid(3,3);

    @TestWith({
        "1-1"
    })
    public void should_be_valid_point(Point point) {
        assertTrue(grid.isPointValid(point));
    }

    @TestWith({
        "0-0",
        "1-0",
        "2-0",
        "2-1"
    })
    public void should_be_invalid_point(Point point) {
        assertFalse(grid.isPointValid(point));
    }

    @Coercion
    public Point parsePoint(String input) {
        String[] split = input.split("-");
        return new Point(parseInt(split[0]), parseInt(split[1]));
    }
}

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:

import static java.lang.Integer.parseInt;
import static junit.framework.Assert.*;
import org.junit.runner.RunWith;
import com.googlecode.zohhak.api.Coercion;
import com.googlecode.zohhak.api.TestWith;
import com.googlecode.zohhak.api.runners.ZohhakRunner;

@RunWith(ZohhakRunner.class)
public class MyTest {

    Grid grid = new Grid(3,3);

    @TestWith({
        "1-1"
    })
    public void should_be_valid_point(Point point) {
        assertTrue(grid.isPointValid(point));
    }

    @TestWith({
        "0-0",
        "1-0",
        "2-0",
        "2-1"
    })
    public void should_be_invalid_point(Point point) {
        assertFalse(grid.isPointValid(point));
    }

    @Coercion
    public Point parsePoint(String input) {
        String[] split = input.split("-");
        return new Point(parseInt(split[0]), parseInt(split[1]));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文