2011-09-05 48 views
0

我想開始使用升壓測試庫爲我的應用程序創建測試。使用升壓單元測試的問題

繼我在http://www.boost.org/doc/libs/1_47_0/libs/test/doc/html/tutorials/new-year-resolution.html找到的教程之後,我開始了我的測試課程。

所以,我創建了一個類爲我的測試,以及簡單的.cpp這是一個

#define BOOST_TEST_MODULE MyClass test 
#include <boost/test/unit_test.hpp> 

#include "myclasstest.h" 

MyClassTest::MyClassTest() 
{ 

} 

/** 
* Test the class. 
*/ 
bool MyClassTest::testClass() 
{ 
    BOOST_AUTO_TEST_CASE(empty_test) 
    { 
     MyClass xTest; 
     BOOST_CHECK(xTest.isEmpty()); 
    } 

return true; 
} 

好吧,我知道我必須做的東西比返回true更聰明,但它不問題。問題是它不能編譯。 我認爲該庫已更正加載,因爲如果我只用前兩行進行編譯,我沒有錯誤,如教程頁面中所述。

如果我嘗試編譯它,我從GCC獲得此錯誤輸出:

myclasstest.cpp: In member function ‘bool MyClassTest::testClass()’: 
myclasstest.cpp:16:5: error: a function-definition is not allowed here before ‘{’ token 
myclasstest.cpp:16:1: error: ‘empty_test_invoker’ was not declared in this scope 
myclasstest.cpp:16:5: error: template argument for ‘template<class T> struct boost::unit_test::ut_detail::auto_tc_exp_fail’ uses local type ‘MyClassTest::testClass()::empty_test_id’ 
myclasstest.cpp:16:5: error: trying to instantiate ‘template<class T> struct boost::unit_test::ut_detail::auto_tc_exp_fail’ 
myclasstest.cpp:17:5: error: a function-definition is not allowed here before ‘{’ token 
myclasstest.cpp:23:1: error: expected ‘}’ at end of input 
myclasstest.cpp:23:1: warning: no return statement in function returning non-void 

我新的刺激,所以我不知道我必須做的。我做錯了什麼?我認爲我已經完成了相同的教程步驟,或者不是?

感謝您的回覆。

回答

0

BOOST_AUTO_TEST_CASE應放置在文件範圍內。它不能在函數實現中放置。您可以使用基於類方法的測試用例,但不能用於自動註冊(暫時)。檢查文檔以獲取更多詳細信息

0

您應該使用BOOST_AUTO_TEST_CASE而不是成員函數。例如:

#define BOOST_TEST_MODULE MyClass test 
#include <boost/test/unit_test.hpp> 

#include "MyClass.h" 

BOOST_AUTO_TEST_CASE(testMyClass) 
{ 
    MyClass xTest; 
    BOOST_CHECK(xTest.isEmpty()); 
} 

如果您需要測試環境,請檢查fixtures