2012-07-06 104 views
1

我在想我是否正在测试我在此测试中列出的所有平等性,还是我只测试第一个。googleTest继续测试

class SomethingTest : public testing::Test 
{ 
public: 
    SomethingTest() { } 
    virtual ~SomethingTest() { } 
}; 

     TEST_F(SomethingTest, Score) 
    { 
     Computer computer; 
     FourInARowStrategy* strategy = new FourInARowStrategy(); 

     vector<vector<int>> brd; 
     for(int i=0; i<6 ;i++) 
     { 
      vector<int> row ; 
      for(int j=0;j<7;j++) 
       row.push_back(0); 
      brd.push_back(row); 
     } 

     brd[5][5]=1; 
     brd[5][4]=2; 
     brd[5][3]=1; 
     brd[5][2]=1; 
     brd[5][1]=1; 
     brd[4][5]=2; 
     brd[4][4]=2; 
     brd[4][3]=1; 
     brd[4][2]=1; 
     brd[3][2]=2; 
     brd[3][1]=2; 
     brd[4][1]=2; 

     strategy->setGameBoard(brd); 
     strategy->displayBoard(); 
     EXPECT_EQ(9,computer.rowScoreAPlay(2,3,3,strategy)); 
     EXPECT_EQ(9,computer.scoreAPlay(2,3,3,strategy)); 
     EXPECT_EQ(0,computer.colScoreAPlay(2,3,3,strategy)); 
     EXPECT_EQ(5,computer.colScoreAPlay(1,3,3,strategy)); 
    } 

    //... 
    } 

你会参考谷歌的单元测试和良好的单元测试开发吗?

感谢和问候。

+3

好裁判喜欢自己的文档(http://code.google。 com/p/googletest/wiki/Primer)? – PlasmaHH 2012-07-06 10:07:13

回答

11

无论通过还是失败,您都在测试它们。这是因为您使用的是EXPECT_EQ而不是ASSERT_EQ

the docs

当他们失败了,ASSERT_ *产量从目前的功能致命的失败和回报,而EXPECT_ *产生一个非致命失败,允许函数继续运行。

通常情况下,EXPECT_*是更好的选择,因为该试验的其余可以继续运行,并能提供有用的输出。但是,如果测试不应该继续,则ASSERT_*会更好。

举例来说,如果你有,你希望有“OK”为第一要素std::vector<std::string> results,你可以这样做:

ASSERT_FALSE(results.empty()); // No point continuing if results is empty 
EXPECT_EQ("OK", results[0]); // Check first element 
+0

这是否意味着如果ASSERT_ *失败,它会立即返回?例如,在ASSERT_ *后面有一些类似cleanup()的代码,如果它失败了,那么cleanup()部分将不会被触发? – ratzip 2015-10-01 07:27:45

5

我在想我是否在测试我在这个测试中列出的所有的平等性,还是我只测试第一个。

their introduction page

ASSERT_ *版本产生致命的失败,当他们失败,并中止当前函数。

这意味着第一个失败的断言将停止测试。

但这并不重要,因为所有测试都应该通过。