English 中文(简体)
CppUnit, 非常简单的测试代码坠毁,原因何在?
原标题:CppUnit, very simple test code crashed, why?

我正在学习CppUnit,而我的代码则在请测试Runner:run()时作了核心总结。 这行文如下。 我看不到任何错误。 这几乎与CppUnit cookbook的样本代码相同。

#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;  
}  

The output:

  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  

The compile line: g++ -g -o -Wall test_runner test_runner.cpp -lcppunit
It compile successfully without any warning.
If I turned on "-Wall", it gave error:

......
(.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  
最佳回答

Given that the signature for the function is:

CppUnit::TestSuite * MyFixture::CreateSuite()

你们应该有?returning<>em>, 什么(appe?

页: 1 页: 1

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

页: 1


你在插入<代码>-Wall时可能重犯这些错误的原因是:

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

将把<代码>-Wall链接到test_runner,test_runner.cppcppunit图书馆,从而试图将你执行到文档中。 也许你打算打字:

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

(I m assuming here that the compile line you gave was the one where the errors occurred since it actually has -Wall in it ).

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?