1

グリッドを持つアプリケーションで作業しており、グリッドの一部のポイントのみが有効と見なされます。考えられるすべてのグリッド値、または少なくともすべての境界点を使用して、これを広範囲にテストする必要があります。

パラメータ化されたテストを試しました。ポイントの後にデータが管理不能になるという事実を期待して、それはうまく機能します。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)));
    }
}

私のテストがシンプルで読みやすいままになるように、データをグループ化/管理する方法に関する入力はありますか??

4

2 に答える 2

1

テストを2つのグループに分けます。有効ポイントと無効ポイント。非常に多くのポイントがある場合は、@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]));
    }
}
于 2014-12-27T22:37:04.930 に答える
1

すべての可能な値をハードコーディングする代わりに、データ ジェネレーターを作成します。何かのようなもの:

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

失敗したテストはとにかくパラメータを出力しています。

于 2011-05-24T09:02:10.843 に答える