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

时间:2019-04-02 15:43:41

标签: c++ unit-testing 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 */  }
 };

现在,我需要从Algo...派生另一个Algo1Interface测试接口,以便可以在单独的测试装置中使用Algo1Interface的公共功能。

例如:

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. 如果是这样,有人可以解释如何做吗?

我尝试研究此问题,但是找不到合适的解决方案。

1 个答案:

答案 0 :(得分:0)

是的,这可以明显的方式完成。任何班级 源自::testing::Test的googletest固定装置;所以任何课 从灯具衍生出来的也是灯具。

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.