使用Catch对BDD样式测试中的多个数据进行一次测试

时间:2017-09-21 13:33:58

标签: c++ testing bdd

我正在使用C ++和Catch框架进行测试。在没有代码重复的情况下,在多个数据上满足一组条件时,使用Catch以BDD样式编写测试用例的正确方法是什么?

举个例子,假设这个测试场景:

SCENARIO( "Test the graph deserialization" ) {
    GIVEN( "A graph" ) {
        Graph g;
        WHEN( "It is loaded by file name" ) {
            g.load( "small_graph.gf" );
            THEN( "It has correct number of nodes and edges" ) {
                REQUIRE( g.node_count() == 210 );
                REQUIRE( g.edge_count() == 306 );
            }
        }
        WHEN( "It is loaded by input stream" ) {
            std::ifstream ifs( "small_graph.gf" );
            g.load( ifs );
            THEN( "It has correct number of nodes and edges" ) {
                REQUIRE( g.node_count() == 210 );
                REQUIRE( g.edge_count() == 306 );
            }
        }
    }
}

可以看出,THEN块是多余的。如果不再重复编写相同的代码,这样做的正确方法是什么。

1 个答案:

答案 0 :(得分:1)

我会提取一个方法,并在需要时调用它,而不是复制功能。