2017-03-02 202 views
0

我正在用谷歌单元测试在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(); 
     } 
}; 

回答

1

您可以创建一个包装类,即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()代替SetUp()

class TestExample : public TestWrapper 
{ 
    public: 
     virtual void CustomSetUp() 
     { 
      //code of specific setup 
      //... 
     } 
     virtual void TearDown() 
     { 
      //Code of specific teardown 
      //... 

      my_system::clean_system(); 
     } 
}; 
+0

是的,我这么认为这样的方法,但我问了这个问题,知道是否存在谷歌测试中的一些方法,设置一个指针函数调用之前设置或类似的东西,不要离开程序员跳过这一步。但是,无论如何,谢谢你,我会做包装。^^ – Check