kiriloff
kiriloff

Reputation: 26335

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.

Upvotes: 2

Views: 8355

Answers (2)

BЈовић
BЈовић

Reputation: 64213

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

From their introduction page :

ASSERT_* versions generate fatal failures when they fail, and abort the current function.

That means that the first failed assert will stop the test.

But that shouldn't matter, because all tests should pass anyway.

Upvotes: 5

Fraser
Fraser

Reputation: 78280

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

Upvotes: 17

Related Questions