微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

| 我正在开发具有网格的应用程序,并且只有网格的某些点被认为是有效的。我需要使用所有可能的网格值或至少使用所有边界点进行广泛的测试。 我已经尝试了参数化测试。对于一个点之后数据变得不可管理的事实,它的工作效果很好。下面给出了3x3网格的示例测试。
@RunWith(Parameterized.class)
public class GridGameTest {

    @Parameters
    public static Collection<Object[]> data(){
        return Arrays.asList(new Object[][] {
                { 0,false },{ 0,1,2,{ 1,true },{ 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)));
    }
}
关于如何分组/管理数据的任何输入,以便我的测试保持简单易读?     

解决方法

我将创建一个数据生成器,而不必对所有可能的值进行硬编码。就像是:
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);
}
无论如何,失败的测试是打印参数。     ,我会将测试分为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]));
    }
}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。