c++ - 谷歌单元测试默认设置

标签 c++ c++11 googletest

我正在使用 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/

相关文章:

c++ - 带有 ctest "Unexpected format: "错误的 googletest

googletest - TEST、TEST_F 和 TEST_P 之间有什么区别?

c++ - Google Mocks 测试输出到 XML 不起作用

c++ - QCamera 获取分辨率和原始帧

c++ - 在 std::thread 中运行时,C++ 成员变量的生命周期是多少?

c++ - 访问类私有(private)成员中的结构成员?

c++ - 使用模板进行序列数组初始化

c++ - 我在 C++11 中从未见过的代码

android - 错误 : undefined reference to '_jstring* QAndroidJniObject::callStaticMethod<_jstring*>(char const*, char const*)'

c++ - CUDA - 没有 block ,只有未定义维度的线程