2011-04-13 39 views
0

我有代碼,我想從一個單元測試中提取,使我的測試方法更加清晰:如何提取測試創建邏輯添加到一個共享的方法

Check check; 
check.Amount = 44.00; 

// unit testing on the check goes here 

我應該如何解壓呢?我應該使用指向檢查或其他結構的指針以確保在使用對象時它仍然被分配了嗎?

我不想使用構造函數,因爲我想將測試創建邏輯與生產創建邏輯隔離開來。

+1

它的單元測試框架找到了這樣的功能,即?你能發佈更多的代碼嗎? – 2011-04-13 21:12:52

回答

0

在現代的單元測試框架,你通常有測試情況下

class MyTest: public ::testing::Test { 
protected: 
    MyTest() {} 
    ~MyTest() {} 
    virtual void SetUp() { 
    // this will be invoked just before each unit test of the testcase 
    // place here any preparations or data assembly 
    check.Amount = 44.00; 
    } 
    virtual void TearDown() { 
    // this will be inkoved just after each unit test of the testcase 
    // place here releasing of data 
    } 
    // any data used in tests 
    Check check; 
}; 

// single test that use your predefined preparations and releasing 
TEST_F(MyTest, IsDefaultInitializedProperly) { 
    ASSERT_FLOAT_EQ(44., check.Amount); 
} 
// and so on, SetUp and TearDown will be done from scratch for every new test 

您可以在谷歌測試框架(https://github.com/google/googletest/