Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

googleTest continue testing

I wonder whether I am testing all the equalities I list in this test, or whether I only test the first.

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

     //... 
     }

Would you have ref for unit tests with google, and good unit test development?

Thanks and regards.

like image 297
kiriloff Avatar asked May 26 '26 14:05

kiriloff


1 Answers

You're testing them all regardless of whether they pass or fail. This is because you're using EXPECT_EQ rather than ASSERT_EQ.

From the docs:

when they fail, ASSERT_* yields a fatal failure and returns from the current function, while EXPECT_* yields a nonfatal failure, allowing the function to continue running.

Normally, EXPECT_* is the better option since the rest of the test can continue to run and can give useful output. However, ASSERT_* is better if the test shouldn't continue.

For example, if you have std::vector<std::string> results in which you expect to have "OK" as the first element, you could do:

ASSERT_FALSE(results.empty());  // No point continuing if results is empty
EXPECT_EQ("OK", results[0]);    // Check first element
like image 199
Fraser Avatar answered Jun 01 '26 09:06

Fraser