我正在尝试使用CppUnit测试,这是从CppUnit Cookbook借来的简单测试代码。 该系统是带有g ++ 4.2.1的Mac OS X.
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/ui/text/TestRunner.h>
#include <iostream>
class Board
{
int m_x, m_y;
public:
Board(int x, int y) : m_x(x), m_y(y) {}
bool operator==(const Board& other) const
{
return (m_x == other.m_x && m_y == other.m_y);
}
int getX() {return m_x;}
int getY() {return m_y;}
};
inline std::ostream &operator <<( std::ostream &os, Board &value )
{
return os << "Board <x =" << value.getX() << "; y = " << value.getY() << "> \n";
}
class BoardTest : public CppUnit::TestFixture {
Board *m_10_20, *m_10_20p, *m_10_30;
public:
BoardTest() {}
void setUp()
{
m_10_20 = new Board(10,20);
m_10_20p = new Board(10,20);
m_10_30 = new Board(10,30);
}
void tearDown()
{
delete m_10_20; delete m_10_20p; delete m_10_30;
}
void testX()
{
CPPUNIT_ASSERT(*m_10_20 == *m_10_20p);
}
void testY()
{
CPPUNIT_ASSERT(!(*m_10_20 == *m_10_30));
}
static CppUnit::Test *suite()
{
CppUnit::TestSuite *testsuite = new CppUnit::TestSuite("Board test");
suite->addTest(new CppUnit::TestCaller<BoardTest>("testX", &BoardTest::testX));
suite->addTest(new CppUnit::TestCaller<BoardTest>("testY", &BoardTest::testY));
return testsuite;
}
};
int main()
{
CppUnit::TextUi::TestRunner runner;
runner.addTest(BoardTest::suite() );
bool retcode = runner.run();
return retcode;
}
当我运行时,我收到以下错误。
simple4.cpp:48: error: request for member 'addTest' in 'BoardTest::suite', which is of non-class type 'CppUnit::Test* ()()' simple4.cpp:49: error: request for member 'addTest' in 'BoardTest::suite', which is of non-class type 'CppUnit::Test* ()()'
以下等效代码才有效。 可能有什么问题?第一个(前一个)示例是否过时了?
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/ui/text/TestRunner.h>
#include <iostream>
class Board
{
int m_x, m_y;
public:
Board(int x, int y) : m_x(x), m_y(y) {}
bool operator==(const Board& other) const
{
return (m_x == other.m_x && m_y == other.m_y);
}
int getX() {return m_x;}
int getY() {return m_y;}
};
inline std::ostream &operator <<( std::ostream &os, Board &value )
{
return os << "Board <x =" << value.getX() << "; y = " << value.getY() << "> \n";
}
class BoardTest : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(BoardTest);
CPPUNIT_TEST(testX);
CPPUNIT_TEST(testY);
CPPUNIT_TEST_SUITE_END();
Board *m_10_20, *m_10_20p, *m_10_30;
public:
BoardTest() {}
void setUp()
{
m_10_20 = new Board(10,20);
m_10_20p = new Board(10,20);
m_10_30 = new Board(10,30);
}
void tearDown()
{
delete m_10_20; delete m_10_20p; delete m_10_30;
}
void testX()
{
CPPUNIT_ASSERT(*m_10_20 == *m_10_20p);
}
void testY()
{
CPPUNIT_ASSERT(!(*m_10_20 == *m_10_30));
}
};
int main()
{
CppUnit::TestSuite *suite = BoardTest::suite();
CppUnit::TextUi::TestRunner runner;
runner.addTest(suite);
bool retcode = runner.run();
return retcode;
}