[英]googletest SetUpTestSuite() does not run
From : https://google.github.io/googletest/advanced.html#sharing-resources-between-tests-in-the-same-test-suite .来自: https ://google.github.io/googletest/advanced.html#sharing-resources-between-tests-in-the-same-test-suite。 I am using 1.11 googletest version.
我正在使用 1.11 googletest 版本。
I am trying to utilize this feature in the following tests: Game_test.h我正在尝试在以下测试中使用此功能:Game_test.h
class Game_test : public :: testing :: Test
{
protected:
Game_test() = default;
virtual ~Game_test() = default;
public:
void SetUpTestCase()
{
Field field(8, 8);
Engine engine;
Rules rules;
GameLogic glogic(&engine, &rules, &field);
}
};
cpp : I expected that it will automatically run SetUpTestCase() for each TEST_F, but it does not. cpp :我希望它会为每个 TEST_F 自动运行 SetUpTestCase(),但事实并非如此。 What I am missing?
我错过了什么?
TEST_F(Game_test, apply_rule)
{
field.setStatus(1, 2, true); // use of undeclared identifier.....
}
PS initially I used SetUpTestSuite(), later I tried SetUpTestCase(), which is in the example PS 最初我使用 SetUpTestSuite(),后来我尝试了 SetUpTestCase(),在示例中
Several things:几件事:
SetUpTestSuite
, not SetUpTestCase
.SetUpTestSuite
,而不是SetUpTestCase
。SetUpTestSuite
should be a static member. SetUpTestSuite
应该是一个静态成员。field
should be a static member of the class if used in SetUpTestSuite
.SetUpTestSuite
中使用, field
应该是类的静态成员。SetUpTestSuite
runs once per test suite, not once per test case. SetUpTestSuite
每个测试套件运行一次,而不是每个测试用例运行一次。SetUp
, which is a non-static member function.SetUp
,它是一个非静态成员函数。SetUp
can then manipulate non-static member variables.SetUp
可以操作非静态成员变量。 See this example that shows the usage of both functions:请参阅此示例,该示例显示了这两个函数的用法:
class Game_test : public testing::Test {
protected:
Game_test() = default;
virtual ~Game_test() = default;
public:
static void SetUpTestSuite() {
std::cout << "========Beginning of a test suit ========" << std::endl;
static_field = std::string("AAAA");
}
void SetUp() override {
std::cout << "========Beginning of a test ========" << std::endl;
object_field = std::string("AAAA");
}
static std::string static_field;
std::string object_field;
};
std::string Game_test::static_field;
TEST_F(Game_test, Test1) {
EXPECT_EQ(static_field, std::string("AAAA"));
EXPECT_EQ(object_field, std::string("AAAA"));
// We change object_field, SetUpTestSuite cannot reset it back to "AAAA" because
// it only runs once at the beginning of the test suite.
static_field = std::string("BBBB");
// Although we change object_field here,
// SetUp will reset it back to "AAAA" at the beginning of each test case.
object_field = std::string("BBBB");
}
TEST_F(Game_test, Test2) {
EXPECT_EQ(static_field, std::string("BBBB"));
EXPECT_EQ(object_field, std::string("AAAA"));
}
Live example: https://godbolt.org/z/e6Tz1xMr1现场示例: https ://godbolt.org/z/e6Tz1xMr1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.