我是 googletest 的新手,我正在尝试使用 googletest 框架在 C++ 中编写单元测试用例,代码如下所示:
class TestInterface : public ::testing::Test
{
protected:
static void SetUpTestCase()
static void TearDownTestCase()
} ;
class Algo1Interface : public TestInterface
{
public:
virtual loadConfig(Inputs, Outputs);
protected:
virtual void SetUp() { /* Small per test set up */ }
virtual void TearDown() { /* Small per test cleanup */ }
};
现在我需要从 Algo1Interface
派生另一个 Algo...
测试接口(interface),这样我就可以使用 Algo1Interface
的公共(public)函数一个单独的测试夹具。
例如:
class Algo2Interface : public Algo1Interface
{
public:
virtual void SetUp() { /* Small per test set up */ }
virtual void TearDown() { /* Small per test cleanup */ }
};
以便像这样编写夹具测试:
TEST_F( Algo1Interface, test1_1 )
{
// Do tests
}
TEST_F( Algo1Interface, test1_2 )
{
// Do tests
}
TEST_F( Algo2Interface, test2_1 )
{
// Use the public functions of Algo1Interface class(loadConfig)
// Do tests
}
问题:
- 这可以做到吗?
- 如果是这样,谁能解释一下怎么做?
我曾尝试研究这个问题,但找不到合适的解决方案。
最佳答案
是的,这可以以显而易见的方式完成。任何类(class)
派生自 ::testing::Test
是一个 googletest fixture;所以任何类(class)
派生自夹具也是夹具。
main.cpp
#include <string>
#include <gtest/gtest.h>
class TestInterface : public ::testing::Test
{
protected:
static void SetUpTestCase() {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
static void TearDownTestCase() {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};
class Algo1Interface : public TestInterface
{
public:
virtual void loadConfig(std::string const & Inputs, std::string & Outputs) {
Outputs = Inputs;
}
protected:
void SetUp() override {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
void TearDown() override {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};
class Algo2Interface : public Algo1Interface
{
public:
void SetUp() override {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
void TearDown() override {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
};
TEST_F( Algo1Interface, test1_1 )
{
ASSERT_EQ(1,1);
}
TEST_F( Algo2Interface, test2_1 )
{
std::string outputs;
loadConfig("Config",outputs);
ASSERT_EQ("Config",outputs);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
编译、链接和运行:
$ g++ -Wall -Wextra -o gtester main.cpp -lgtest -pthread
$ ./gtester
[==========] Running 2 tests from 2 test suites.
[----------] Global test environment set-up.
[----------] 1 test from Algo1Interface
static void TestInterface::SetUpTestCase()
[ RUN ] Algo1Interface.test1_1
virtual void Algo1Interface::SetUp()
virtual void Algo1Interface::TearDown()
[ OK ] Algo1Interface.test1_1 (0 ms)
static void TestInterface::TearDownTestCase()
[----------] 1 test from Algo1Interface (0 ms total)
[----------] 1 test from Algo2Interface
static void TestInterface::SetUpTestCase()
[ RUN ] Algo2Interface.test2_1
virtual void Algo2Interface::SetUp()
virtual void Algo2Interface::TearDown()
[ OK ] Algo2Interface.test2_1 (0 ms)
static void TestInterface::TearDownTestCase()
[----------] 1 test from Algo2Interface (0 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 2 test suites ran. (0 ms total)
[ PASSED ] 2 tests.
关于c++ - 从googletest中的派生夹具派生夹具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55478722/