UnitTest
|
runTest()
assertTrue()
num_test_sucess:int
|
// UnitTest.h
#define UT_ASSERT( condition ) /
assertTrue(condition,__FILE__,__LINE_ _,#condition)
class UnitTest
{
public:
virtual ~UnitTest( ) {}
virtual void runTest( ) = 0;
protected:
void assertTrue(bool condition, const char *file,
int line, const char *msg);
static int num_test_success;
};
// UnitTest.cpp
#include
#include
#include "UnitTest.h"
int UnitTest::num_test_success = 0;
void UnitTest::assertTrue(bool condition,
const char *file, int line,
const char *msg)
{
if (!(condition))
{
printf("FAILURE!/n");
printf("%s:%d:%s/n", file, line, msg);
exit(1);
}
++num_test_success;
}
//BookTest.h
#include "UnitTest.h"
#include "Book.h" //
class BookTest : public UnitTest
{
public:
void runTest( ) // override
{
Book book("Cosmos");
UT_ASSERT(!strcmp(book.title, "Cosmos"));
}
};
//TestRunner.cpp
#include "stdio.h"
#include "BookTest.h"
int main( )
{
BookTest test;
test.runTest( ); // exit via assert if test fails.
printf("SUCCESS!/n");
return 0;
}
//Book.h
#include "string.h"
class Book
{
public:
Book(const char* title) {}
char title[255];
};
//Book.h
#include "string.h"
class Book
{
public:
Book(const char* title) {
strcpy(this->title, title);
}
char title[255];
};
---------
1
CppUnitTestCaseUnitTest
//Book.h
using std::string;
class Book {
public:
Book( string const &title )
: m_title( title ) {}
string getTitle( ) { return m_title; }
private:
string m_title;
};
//BookTest.h
#include "cppunit/TestCase.h"
#include "Book.h"
using std::string;
class BookTest : public CppUnit::TestCase {
public:
BookTest( string const &name ) : CppUnit::TestCase( name ) {}
void runTest( ) { //override
Book book( "Cosmos" );
CPPUNIT_ASSERT( book.getTitle( ) == "Cosmos" );
}
};
//test.cpp
#include
#include "BookTest.h"
int main( ) {
try {
BookTest test( "BookTest" );
test.runTest( );
std::cout << "SUCCESS!" << std::endl;
return 0;
} catch ( ... ) {
std::cout << "FAILURE!/n" << std::endl;
return 1;
}
}
Book.h
using std::string;
class Book {
public:
Book(string const &title, string const &author)
: m_title( title ), m_author( author ) {}
string getTitle( ) { return m_title; }
string getAuthor( ) { return m_author; }
private:
string m_title;
string m_author; //
};
BookTest.h
#include "cppunit/TestCase.h"
#include "Book.h"
using std::string;
//
从TestFixture派生;
增加作为fixture一部分的成员变量;
覆盖setUp(),teardown()
class BookTest : public CppUnit::TestFixture {
private:
Book *book;
public:
void setUp( ) { // override
book = new Book( "Cosmos", "Carl Sagan" );
}
void tearDown( ) { // , override
delete book;
}
void testTitle( ) { //
CPPUNIT_ASSERT_EQUAL( string("Cosmos"), book->getTitle( ) );
}
void testAuthor( ) { //
CPPUNIT_ASSERT_EQUAL( string("Carl Sagan"), book->getAuthor( ) );
}
};
//test.cpp
#include "cppunit/ui/text/TestRunner.h"
#include "cppunit/TestCaller.h"
#include "BookTest.h"
int main( ) {
CppUnit::TextUi::TestRunner runner; //text TestRunner
runner.addTest( new CppUnit::TestCaller(
"testTitle",
&BookTest::testTitle ) );
runner.addTest( new CppUnit::TestCaller(
"testAuthor",
&BookTest::testAuthor ) );
if ( runner.run( ) ) // true
return 0;
else
return 1;
}
//BookTest.h
//BooKTestsuite()Helper Macros
// (4)
static CppUnit::Test* suite()
{
CppUnit::TestSuite *suite
= new CppUnit::TestSuite( "BookTest" );
suite->addTest( new CppUnit::TestCaller(
"testTitle",
&BookTest::testTitle ) );
suite->addTest( new CppUnit::TestCaller(
"testAuthor",
&BookTest::testAuthor ) );
return suite;
}
//test.cpp
#include "cppunit/ui/text/TestRunner.h"
#include "cppunit/TestCaller.h"
#include "BookTest.h"
int main( ) {
CppUnit::TextUi::TestRunner runner;
runner.addTest( BookTest::suite( ) );
// ::suite()
// runner.addTest(OtherTest::suite());
if ( runner.run( ) ) //
return 0;
else
return 1;
}
#include <cppunit/extensions/HelperMacros.h>
CPPUNIT_TEST_SUITE_REGISTRATION( ComplexNumber ); //
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
int main( int argc, char *argv[])
{
CppUnit::TextUi::TestRunner runner;
CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry();
runner.addTest( registry.makeTest() );
if (runner.run()) // run
return 0;
else
return 1;
}