我正在使用Google Test对我的C++项目进行单元测试。入门指南说:


SetUp()的拼写正确,但是我仍然无法使SetUp正常工作。有任何想法吗?

#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
输出:
[==========] 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.

最佳答案

TEST更改为TEST_F,因为SetUp方法等是通过TEST_F调用的,但不能仅通过TEST调用。

关于c++ - googletest设置方法未调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31813287/

10-12 17:29