我正在使用 C++ 中的谷歌单元测试制作单元测试系统。我注意到我所有的单元测试设置都包含相同的行,而我所有的眼泪都包含其他行,对所有人来说都是平等的。
我想知道是否有任何方法可以创建一个默认设置,以便在实际设置任何单元测试之前调用。
#include <gtest.h>
class TestExample : ::testing::Test
{
public:
virtual void SetUp ()
{
//same line for all tests of my system
my_system::clean_system();
//code of specific setup
//...
}
virtual void TearDown ()
{
//Code of specific teardown
//...
my_system::clean_system();
}
};
最佳答案
您可以创建一个 Wrapper 类,即 TestWrapper
,您可以在其中定义默认的 SetUp()
并调用 CustomSetUp()
#include <gtest.h>
class TestWrapper: public ::testing::Test
{
public:
virtual void CustomSetUp() {}
virtual void SetUp ()
{
//same line for all tests of my system
my_system::clean_system();
CustomSetUp(); //code of specific setup
}
};
然后在单元测试中使用 TestWrapper
类而不是 ::testing::Test
并重载 CustomSetUp()
而不是 设置()
class TestExample : public TestWrapper
{
public:
virtual void CustomSetUp ()
{
//code of specific setup
//...
}
virtual void TearDown ()
{
//Code of specific teardown
//...
my_system::clean_system();
}
};
关于c++ - 谷歌单元测试默认设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42565595/