2011-10-11 125 views
0

我正在學習CppUnit,並且在調用TestRunner :: run()時,我的代碼被調用了。這是下面的代碼中的第34行。我看不到任何錯誤。它幾乎與CppUnit食譜中的示例代碼相同。CppUnit,非常簡單的測試代碼崩潰了,爲什麼?

#include <iostream> 
#include <cppunit/TestCaller.h> 
#include <cppunit/TestSuite.h> 
#include <cppunit/ui/text/TestRunner.h> 
using namespace std; 

//class MyFixture: public CppUnit::TestFixture { 
class MyFixture{ 
public: 
    MyFixture() {cout<<"MyFixture:: ctor()"<<endl;} 
    ~MyFixture() {cout<<"MyFixture:: dtor()"<<endl;} 
    void setUp() {cout<<"MyFixture::Setup()"<<endl;} 
    void tearDown() {cout<<"MyFixture::tearDown()"<<endl;} 
    void testFunc1() {cout<<"MyFixture::testFunc1()"<<endl; m=1; CPPUNIT_ASSERT(m==1);} 
    void testFunc2() {cout<<"MyFixture::testFunc2()"<<endl; m=2;} 
    int m; 
    static CppUnit::TestSuite * CreateSuite(); 
}; 

CppUnit::TestSuite * MyFixture::CreateSuite() 
{ 
    CppUnit::TestSuite * suite = new CppUnit::TestSuite("My TestSuite for MyFixture"); 
    suite->addTest(new CppUnit::TestCaller<MyFixture>("MyFixture::testFunc1", &MyFixture::testFunc1)); 
    suite->addTest(new CppUnit::TestCaller<MyFixture>("MyFixture::testFunc2", &MyFixture::testFunc2)); 
} 

int main() 
{ 
    cout<<"point 1000"<<endl; 
    CppUnit::TextUi::TestRunner runner; 
    runner.addTest(MyFixture::CreateSuite()); 
    cout<<"point 7000"<<endl; 
/*Line34*/ runner.run(); 


    cout<<"point 8000"<<endl; 
} 

輸出:

cppunit$ ./test_runner 
    point 1000 
    MyFixture:: ctor() 
    MyFixture:: ctor() 
    point 7000 
    Segmentation fault (core dumped) 

堆棧跟蹤:

(gdb)bt 
-0 0x00733cc9 in CppUnit::TestRunner::WrappingSuite::run(CppUnit::TestResult*)() 
    from /usr/lib/libcppunit-1.12.so.1 
-1 0x0073170a in CppUnit::TestResult::runTest(CppUnit::Test*)() from /usr/lib/libcppunit-1.12.so.1 
-2 0x00733af0 in CppUnit::TestRunner::run(CppUnit::TestResult&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)() from /usr/lib/libcppunit-1.12.so.1 
-3 0x00736d2b in CppUnit::TextTestRunner::run(CppUnit::TestResult&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)() from /usr/lib/libcppunit-1.12.so.1 
-4 0x00736da2 in CppUnit::TextTestRunner::run(std::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, bool, bool)() from /usr/lib/libcppunit-1.12.so.1 
-5 0x080494dd in main() at test_runner.cpp:34 

編譯行:克++ -g -o -Wall test_runner test_runner.cpp -lcppunit
它沒有任何警告成功編譯。
如果我打開 「-Wall」,它給了錯誤:

...... 
(.data+0x4): multiple definition of `__dso_handle' 
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/crtbegin.o:(.data+0x0): first defined here 
test_runner: In function `_init': 
(.init+0x0): multiple definition of `_init' 
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crti.o:(.init+0x0): first defined here 
/tmp/ccgzqvu9.o: In function `MyFixture::CreateSuite()': 
/home/fzhao/temp/cppunit/test_runner.cpp:22: multiple definition of `MyFixture::CreateSuite()' 
test_runner:/home/fzhao/temp/cppunit/test_runner.cpp:22: first defined here 
/tmp/ccgzqvu9.o: In function `main': 
/home/fzhao/temp/cppunit/test_runner.cpp:29: multiple definition of `main' 
test_runner:/home/fzhao/temp/cppunit/test_runner.cpp:29: first defined here 
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/crtend.o:(.dtors+0x0): multiple definition of `__DTOR_END__' 
test_runner:(.dtors+0x4): first defined here 
/usr/bin/ld: warning: Cannot create .eh_frame_hdr section, --eh-frame-hdr ignored. 
/usr/bin/ld: error in test_runner(.eh_frame); no .eh_frame_hdr table will be created. 
collect2: ld returned 1 exit status 
make: *** [test_runner] Error 1 
+0

它崩潰了,還是它沒有建立?選一個。 –

回答

1

鑑於該函數的簽名是:

CppUnit::TestSuite * MyFixture::CreateSuite() 

你不應該是返回東西(suite會是我的第一個猜測)?

通過不返回任何行:

runner.addTest(MyFixture::CreateSuite()); 

將會非常狡猾的指針添加到您的runner


當你插入-Wall你可能得到這些錯誤的原因是因爲:

g++ -g -o -Wall test_runner test_runner.cpp -lcppunit 
     \______/ \___________________________________/ 

test_runnertest_runner.cppcppunit連接在一起試圖輸出你的可執行文件-Wall圖書館。也許你想鍵入:

g++ -g -Wall -o test_runner test_runner.cpp -lcppunit 
      \____________/ \_______________________/ 

(我假設在這裏,你給編譯行是一個地方發生的錯誤,因爲它實際上中有-Wall)。

+1

糟糕。這是一個愚蠢的錯誤。解決後,它的工作。 –

+0

但是,爲什麼當我打開「-Wall」時,g ++發出了很多錯誤並失敗了?它報告了main(),_init,_dso_handle的「多重定義」?這是我的主要問題。我將編輯帖子以反映這一點。 –

+1

@約翰,看我的第二部分。如果你有'-Wall',你說你有它,你正在試圖結合'test_runner'可執行文件,'test_runner.cpp'和'cppunit'庫。由於可執行文件已經定義了所有事物,因此您將得到重複的符號錯誤。改變它,使'-o'和'test_runner'相鄰,並按照這個順序。 – paxdiablo