ratzip
ratzip

Reputation: 1671

google test functions setup() and teardown() is called for each test case or for the whole testsuit

I am using google test and trying to write test fixtures to test source code, in the test fixture, there are several test cases defined.

There are SetUp() and TearDown() functions, for those functions, if there are defined, will they be called for each test case or only once for the whole test suit?

Upvotes: 4

Views: 11989

Answers (3)

Riccardo Fontana
Riccardo Fontana

Reputation: 21

You can verify they are called for each test by simply adding cout statements:

in SetUp():

cout << "SetUp called\n";

in TearDown():

cout << "TearDown called\n";

Run your tests and look at the output; then you can see if it is called per test or per suite.

Upvotes: 0

Shogun
Shogun

Reputation: 94

Googletest does not reuse the same test fixture object across multiple tests. For each TEST_F, googletest will create a fresh test fixture object, immediately call SetUp(), run the test body, call TearDown(), and then delete the test fixture object. Source

Upvotes: 3

Melkon
Melkon

Reputation: 428

Every test case have it's own fixture, so these are called every time.

Upvotes: 2

Related Questions