开发者

How to group test data in a parameterized test?

开发者 https://www.devze.com 2023-03-08 04:07 出处:网络
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

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??


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.


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]));
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消