从googletest中的派生夹具派生夹具

Deriving a fixture from derived fixture in googletest

我是 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... 测试接口,以便我可以在单独的测试夹具中使用 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
}

问题:

  1. 这能做到吗?
  2. 如果是这样,谁能解释一下怎么做?

我已经尝试研究这个问题,但找不到合适的解决方案。

是的,这可以以显而易见的方式完成。任意 class 来自 ::testing::Test 的是一个 googletest 装置;所以任何 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();
}

编译,link和运行:

$ 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.