sebastian
sebastian

Reputation: 5414

googletest SetUp Method not called

I'm using Google Test to unit test my C++ project. The getting started guide says:

If necessary, write a default constructor or SetUp() function to prepare the objects for each test. A common mistake is to spell SetUp() as Setup() with a small u - don't let that happen to you.

SetUp() is spelled correctly, but I still can't get SetUp to work. Any ideas?

#include "gtest/gtest.h"

class SampleTest : public ::testing::Test {
 protected:
  virtual void SetUp() { std::cout << "SetUp called." << std::endl; }
};

TEST(SampleTest, OneEqualsOne) {
  int one = 1;
  ASSERT_EQ(1, one);
}

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

g++ -g -Wno-deprecated -I gtest/include SampleTest.cpp gtest/libgtest.a -o SampleTest

Output:

[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from SampleTest
[ RUN      ] SampleTest.OneEqualsOne
[       OK ] SampleTest.OneEqualsOne (1 ms)
[----------] 1 test from SampleTest (1 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (1 ms total)
[  PASSED  ] 1 test.

Upvotes: 5

Views: 6818

Answers (2)

Ron
Ron

Reputation: 61

Change your TEST macro to TEST_F. (It's listing in the documentation right underneath the quote you provided.)

Upvotes: 2

Diego Sevilla
Diego Sevilla

Reputation: 29019

Change TEST to TEST_F, as SetUp methods and such are called with TEST_F, but not with TEST alone.

Upvotes: 15

Related Questions