简体   繁体   English

Google测试框架参数化固定装置

[英]Google test framework parametrize fixtures

i have a one question. 我有一个问题。 i use the following structure as fixture: 我使用以下结构作为夹具:

class unvalidSDPTest : public ::testing::Test{
protected:
    virtual void SetUp(){
        std::string Name("name");
        Server=new SipServer(Name);
        std::ifstream offerFile,answerFile;
        offerFile.open("unvalidOfferSDP.txt");
        answerFile.open("unvalidAnswerSDP.txt");
        std::string offerRawSDP,answerRawSDP;
        offerFile>>offerRawSDP;
        answerFile>>answerRawSDP;
        offerSDPSession = Server->MakeSDPSession( const_cast(offerRawSDP.c_str()) ) ;
        answerSDPSession = Server->MakeSDPSession( const_cast(answerRawSDP.c_str()) ) ;
    }
    virtual void TearDown(){
        delete Server;
    }
    pjmedia_sdp_session *offerSDPSession, *answerSDPSession ;
    SipServer *Server;
    SDPNeg Negotiator;
};

and i want to parametrize this class by file name. 我想按文件名参数化这个类。 how make it better? 如何让它变得更好?

There's many ways to do this - I'll show you two: 有很多方法可以做到这一点 - 我会告诉你两个:

Method 1: Create a base class with a virtual functions, then derive from it. 方法1:使用虚函数创建基类,然后从中派生。

class unvalidSDPTestBase : public ::testing::Test{
  virtual const char * offer_filename() const =0;
  virtual const char * answer_filename() const =0;
  protected:
    virtual void SetUp(){
        std::string Name("name");
        Server=new SipServer(Name);
        std::ifstream offerFile,answerFile;
        offerFile.open( offer_filename());
        answerFile.open(answer_filename());
        std::string offerRawSDP,answerRawSDP;
        offerFile>>offerRawSDP;
        answerFile>>answerRawSDP;
        offerSDPSession = Server->MakeSDPSession( const_cast(offerRawSDP.c_str()) ) ;
        answerSDPSession = Server->MakeSDPSession( const_cast(answerRawSDP.c_str()) ) ;
    }
    virtual void TearDown(){
        delete Server;
    }
    pjmedia_sdp_session *offerSDPSession, *answerSDPSession ;
    SipServer *Server;
    SDPNeg Negotiator;
 };

 class unvalidSPDTestBase: public unvalidSDPTestBase
 {
     virtual const char * offer_filename() const { return "..."; }
     virtual const char * answer_filename() const { return "..."; }
 }

Method 2: use a traits like system and templating: 方法2:使用系统和模板等特征:

template<typename FILENAME_SOURCE>
class SDPTest : public ::testing::Test{
  protected:
    virtual void SetUp(){
        std::string Name("name");
        Server=new SipServer(Name);
        std::ifstream offerFile,answerFile;
        offerFile.open( FILENAME_SOURCE::offer_filename());
        answerFile.open( FILENAME_SOURCE::answer_filename());
        std::string offerRawSDP,answerRawSDP;
        offerFile>>offerRawSDP;
        answerFile>>answerRawSDP;
        offerSDPSession = Server->MakeSDPSession( const_cast(offerRawSDP.c_str()) ) ;
        answerSDPSession = Server->MakeSDPSession( const_cast(answerRawSDP.c_str()) ) ;
    }
    virtual void TearDown(){
        delete Server;
    }
    pjmedia_sdp_session *offerSDPSession, *answerSDPSession ;
    SipServer *Server;
    SDPNeg Negotiator;
 };

 struct InvalidFilenames
 {
   static const char * offer_filename()  { return "..."; }
   static const char * answer_filename() { return "..."; }
 };

 typedef SDPTest<InvalidFilenames> unvalidSDPTest; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM