|
[B]asic module for [unit] program testing:
|
Public Member Functions | |
| bool | run () |
| Main testing method. | |
| void | test_constructor () |
test_BUnit ==> TestCase::TestCase(...). | |
| void | test_reset () |
test_BUnit ==> TestCase::reset() + TestCase::resetTests(). | |
| void | test_addTest () |
test_BUnit ==> TestSuite<TestCase>::addTest(). | |
| void | test_run () |
test_BUnit ==> run() + runBare(). | |
| void | test_setName () |
test_BUnit ==> getName() + setName(). | |
| void | test_testThis () |
test_BUnit ==> testThis(). | |
| void | test_BUnit_macro () |
| test_BUnit ==> Macros. | |
| void | test_assertTrue_Msg () |
test_BUnit ==> assertTrue_Msg(). | |
| void | test_Allison () |
test_BUnit ==> test_Allison(). | |
| void | runBare () |
Excecutes the test run setUp(); run(); tearDown();. | |
| bool | Run () |
Synonym for run(). | |
| bool | runTest () |
Synonym for run(). | |
| virtual void | setUp () |
| Sets the environment for the test run. | |
| virtual void | tearDown () |
| Destroys the test environment. | |
| int | countTestCases () const |
| 1 == Number of test cases. | |
| int | runCount () const |
| Number of test runs. | |
| virtual int | failureCount () const |
| Number of test runs that failed. | |
| int | errorCount () const |
Always returns 0 (cero): "Number of errors". | |
| virtual int | successCount () const |
| Number of successful test runs. | |
| bool | wasSuccessful () const |
Returns "true" when all test runs where successful. | |
| virtual void | reset () |
| Discards all test runs. | |
| std::string | getName () const |
| Gets the test case name. | |
| void | setName (const char *name=0) |
Sets the test case name to "name". | |
| virtual const std::string | toString () const |
Huuuge string that holds a copy of non successfull test, separated by "\n". | |
| virtual const std::string | summary () const |
| Returns a string with the name, number of successes and failures. | |
| virtual const std::string | toXML () const |
| XML string that holds a copy of all unsuccessful tests. | |
| const std::string | report () const |
Returns string summary() followed by toString(). | |
| const std::string | failureString () const |
Synonym for toString(). | |
Static Public Member Functions | |
| template<class T > | |
| static std::string | toString (const T &val) |
Returns a std::string constructed form value val. | |
Protected Member Functions | |
| void | recordSuccess () |
| Records the test run as a success. | |
| void | recordFailure (const char *label, const char *fname, int lineno, bool must_copy=false) |
| Records that the test run did not succeed. | |
| void | recordFailure (const std::string &label, const char *fname, int lineno) |
| Records that the test did not succeed. | |
| void | testThis (bool cond, const char *label, const char *fname, long lineno, bool must_copy=false) |
| Executes the test and records its result. | |
| void | testThis (bool cond, const std::string &label, const char *fname, long lineno) |
Synonym for testThis(). | |
| int | nPass () const |
Synonym for successCount() [OBSOLETE]. | |
| int | nError () const |
Synonym for failureCount() [OBSOLETE]. | |
Protected Attributes | |
| int | m_pass |
| Number of successful tests. | |
| int | m_failure |
| Number of test that produced failed. | |
| const char * | m_name |
| Test case name. | |
| bool | m_test_suite_destroy |
Holds "true" if the test case is stored in dynamic memory. | |
| std::list< TestCaseFailure > | m_failureList |
| Container where test cases that produced failures are stored. | |
Friends | |
| class | TestSuite |
| Colección de pruebas. | |
| template<class TestCase > | |
| void | do_toXML (const TestCase *tc, std::basic_ostringstream< char > &ost) |
Adds to ost the string of al unsuccessful test from *tc in XML format. | |
| template<class TestCase > | |
| void | do_toString (const TestCase *tc, std::basic_ostringstream< char > &ost) |
Adds to ost the string of al unsuccessful test from *tc. | |
Testing BUnit.h.
Definition at line 15 of file test_BUnit.cpp.
| bool test_BUnit::run | ( | ) | [virtual] |
| void test_BUnit::test_constructor | ( | ) |
test_BUnit ==> TestCase::TestCase(...).
Definition at line 45 of file test_BUnit.cpp.
| void test_BUnit::test_reset | ( | ) |
test_BUnit ==> TestCase::reset() + TestCase::resetTests().
Definition at line 65 of file test_BUnit.cpp.
| void test_BUnit::test_addTest | ( | ) |
test_BUnit ==> TestSuite<TestCase>::addTest().
SSS se queda vacío
Definition at line 97 of file test_BUnit.cpp.
| void test_BUnit::test_run | ( | ) |
test_BUnit ==> run() + runBare().
Definition at line 170 of file test_BUnit.cpp.
| void test_BUnit::test_setName | ( | ) |
test_BUnit ==> getName() + setName().
Definition at line 200 of file test_BUnit.cpp.
| void test_BUnit::test_testThis | ( | ) |
test_BUnit ==> testThis().
Definition at line 225 of file test_BUnit.cpp.
| void test_BUnit::test_BUnit_macro | ( | ) |
test_BUnit ==> Macros.
Definition at line 249 of file test_BUnit.cpp.
| void test_BUnit::test_assertTrue_Msg | ( | ) |
test_BUnit ==> assertTrue_Msg().
Definition at line 291 of file test_BUnit.cpp.
| void test_BUnit::test_Allison | ( | ) |
test_BUnit ==> test_Allison().
{{ // test::Allison()
// Stack class for letter -- very simple (<em>Last In First Out</em>).
// - Throws \c std::logic_error on invalid operations.
// - Example inspired in Chuck Allison' work.
// - http://search.yahoo.com/search?n=100&p=Simplest+Unit+Test+Allison
class Stack {
public:
enum { N = 5 /* Max Capacity for the stack */ };
// Constructor for a stack that can hold up to \c "Stack::N" values.
Stack() : m_top(0) { m_vec[m_top] = 0; }
// Stoes a copy of "v" at the top of the stack.
void push(const char& v) {
if ( m_top != N ) { m_vec[m_top] = v; m_top++; }
else { throw std::out_of_range("Stack::push()"); }
}
// Removes the topmost value from the stack.
void pop() {
if ( m_top>0 ) { m_top--; }
else { throw std::out_of_range("Stack::pop()"); }
}
// Reference to topmost value in the stack.
char& top() {
if ( m_top>0 ) { return m_vec[m_top-1]; }
else { throw std::out_of_range("Stack::top()"); }
}
// Number of values stored in the stack.
unsigned size() const { return m_top; }
private:
char m_vec[N+1] ; // Vector to hold stored values.
int m_top; // Next available vector entry.
}; // Stack
class MyTest : public TestCase {
public:
bool run() {
Stack S; // the stack
try { // empty stack
S.pop();
fail_Msg("! S.pop()");
}
catch ( std::out_of_range & ex ) {
assertTrue( 0 == strcmp( ex.what() , "Stack::pop()" ) ); // Ok: empty stack
}
catch (...) {
fail_Msg("! ( std::out_of_range & )");
}
try { // Pila llena
for (int i=0; true; ++i) {
S.push('0'+i);
assertTrue( S.top() == '0'+i );
}
fail_Msg("! S.push()");
}
catch ( std::out_of_range & ex ) {
assertTrue( 0 == strcmp( ex.what() , "Stack::push()" ) ); // Ok: full stack
}
catch (...) {
fail_Msg("! ( std::out_of_range & )");
}
try { // Vacea la pila
for (int i=Stack::N-1; true; --i) {
assertTrue( S.top() == '0'+i );
S.pop();
}
fail_Msg("! S.pop()");
}
catch ( std::out_of_range & ex ) {
assertTrue( 0 == strcmp( ex.what() , "Stack::top()" ) ); // Ok: empty stack
}
catch (...) {
fail_Msg("! ( std::out_of_range & )");
}
return wasSuccessful();
}
}; // MyTest
MyTest thisTest;
thisTest.run(); // 0 failures
assertTrue( thisTest.wasSuccessful() );
}}
Definition at line 330 of file test_BUnit.cpp.
| void TestCase::runBare | ( | ) | [inline, inherited] |
Excecutes the test run setUp(); run(); tearDown();.
run(), this method will setup the environment for the test run invoking setUp() and tearDown() before and after the test. {{ // test::run()
class MyTest : public TestCase {
int m_val;
public:
MyTest() : m_val(0) {} // init: m_val == 0;
void setUp() { m_val = 1; }
void tearDown() { m_val = 2; }
bool run() {
assertTrue( m_val == 1 );
return wasSuccessful();
}
}; // MyTest
TestSuite<TestCase> SSS;
SSS.addTest( new MyTest ); SSS.addTest( new MyTest );
assertTrue( 2 == SSS.countTestCases() );
assertTrue( "" == SSS.failureString() );
SSS.runBare(); // Ok ==> setUp() sets [m_val == 1]
assertTrue( "" == SSS.toXML() );
SSS.run(); // Failure: [m_val == 2] ==> value set by tearDown()
std::string sssXML = SSS.toXML();
assertTrue( "" != sssXML ); // SSS contains failures.
assertTrue( sssXML.find("m_val") != string::npos );
assertTrue( SSS.runCount() == 2+2 );
}}
Reimplemented in TestSuite< TestCase >.
| bool TestCase::Run | ( | ) | [inline, inherited] |
| bool TestCase::runTest | ( | ) | [inline, inherited] |
| void TestCase::setUp | ( | ) | [inline, virtual, inherited] |
Sets the environment for the test run.
TestCase::run() is an abstract method, to facilitate programming it is usual for the programmer not to invoke TestCase::setUp() and TestCase::tearDown() because it is easier to have TestSuite<TestCase>::runBare() do it.TestCase::runBare(), method TestCase::run() will not establish the test environment because it does not invoke neither TestCase::setUp() before the test run nor TestCase::tearDown() after the test run.TestSuite<TestCase>::runBare() invokes methods TestCase::setUp() and TestCase::tearDown() when the test run is executed.TestSuite and execute all of them invoking TestSuite<TestCase>::runBare(). Reimplemented in test_rational< INT >, ADH::test_Graph, and test1.
| void TestCase::tearDown | ( | ) | [inline, virtual, inherited] |
| int TestCase::countTestCases | ( | ) | const [inline, inherited] |
1 == Number of test cases.
The value returned always is one 1 because class TestCase represents a single test case. For the container TestSuite<> the value returned can be bigger than 1.
TestCase."assert()" macro, as assertTrue(), fail_Msg(), assertEquals_Delta(), or others like BUnit_SUCCESS() or BUnit_TEST().TestSuite<>. Reimplemented in TestSuite< TestCase >.
| int TestCase::runCount | ( | ) | const [inline, inherited] |
Number of test runs.
successCount()+failureCount()+errorCount(). | int TestCase::failureCount | ( | ) | const [inline, virtual, inherited] |
| int TestCase::errorCount | ( | ) | const [inline, inherited] |
Always returns 0 (cero): "Number of errors".
| int TestCase::successCount | ( | ) | const [inline, virtual, inherited] |
| bool TestCase::wasSuccessful | ( | ) | const [inline, inherited] |
Returns "true" when all test runs where successful.
(successCount() == runCount()) | void TestCase::reset | ( | ) | [inline, virtual, inherited] |
Discards all test runs.
{{ // test::reset()
class MyTest : public TestCase {
public:
bool run() {
assertTrue( 1 == 2 ); // Failure !!!
assertTrue( 1 == 1 ); // Ok
assertTrue( 2 == 2 ); // Ok
return wasSuccessful();
}
}; // MyTest
MyTest thisTest;
for ( int i=0; i<11; ++i ) {
thisTest.run(); // runs the same test 11 times
}
assertTrue( 11 == thisTest.failureCount() ); // ( 1 == 2 ) x 11
assertTrue( 22 == thisTest.successCount() ); // ( 1 == 1 ) && ( 2 == 2 )
assertTrue( 33 == thisTest.runCount() ); // 33 == 11+22
assertTrue( "" != thisTest.failureString() ); // 11 recorded failures
std::string remember = thisTest.getName();
thisTest.reset(); // Anula los contadores
assertTrue( 0 == thisTest.failureCount() );
assertTrue( 0 == thisTest.successCount() );
assertTrue( 0 == thisTest.runCount() );
assertTrue( "" == thisTest.failureString() );
assertTrue( remember == thisTest.getName() ); // reset() won´t change the name
}}
Reimplemented in TestSuite< TestCase >.
| std::string TestCase::getName | ( | ) | const [inline, inherited] |
| void TestCase::setName | ( | const char * | name = 0 | ) | [inline, inherited] |
Sets the test case name to "name".
"name" is a null string or pointer, later typeid(*this).name() will be invoked to get the test´s name. {{ // test::setName()
class MyTest : public TestCase {
public:
bool run() {
assertTrue( 2 == 2 );
return wasSuccessful();
}
}; // MyTest
MyTest thisTest;
assertTrue( "chorlito" != thisTest.getName() );
thisTest.setName( "chorlito" );
assertTrue( "chorlito" == thisTest.getName() );
{
// thisTest.setName( std::string("chorlito") ); // won´t compile
thisTest.setName( std::string("chorlito").c_str() ); // bad practice
std::string V("boom!");
assertTrue( 0==strcmp( V.c_str() , "boom!") );
assertTrue( "chorlito" != thisTest.getName() ); // c_str() uses
assertTrue( "boom!" == thisTest.getName() ); // static data
}
}}
| const std::string TestCase::toString | ( | ) | const [inline, virtual, inherited] |
Huuuge string that holds a copy of non successfull test, separated by "\n".
=_fail: 1 == 0
=/ (125) X:/DIR/SubDir/test_BUnit.cpp
=_fail: 4 == 0
=/ (128) X:/DIR/SubDir/test_BUnit.cpp
Reimplemented in TestSuite< TestCase >.
| std::string TestCase::toString | ( | const T & | val | ) | [static, inherited] |
Returns a std::string constructed form value val.
toString() with standard C++ | const std::string TestCase::summary | ( | ) | const [inline, virtual, inherited] |
Returns a string with the name, number of successes and failures.
Reimplemented in TestSuite< TestCase >.
| const std::string TestCase::toXML | ( | ) | const [inline, virtual, inherited] |
XML string that holds a copy of all unsuccessful tests.
<fail file="X:/DIR/SubDir/test_BUnit.cpp" line="125" message="1 == 0"/> <fail file="X:/DIR/SubDir/test_BUnit.cpp" line="128" message="4 == 0"/>
Reimplemented in TestSuite< TestCase >.
| const std::string TestCase::report | ( | ) | const [inline, inherited] |
Returns string summary() followed by toString().
| const std::string TestCase::failureString | ( | ) | const [inline, inherited] |
Synonym for toString().
| void TestCase::recordSuccess | ( | ) | [inline, protected, inherited] |
| void TestCase::recordFailure | ( | const char * | label, |
| const char * | fname, | ||
| int | lineno, | ||
| bool | must_copy = false |
||
| ) | [inline, protected, inherited] |
Records that the test run did not succeed.
"fname" and "lineno" indicate the file and line where the test run is executed."fname" and "lineno" are usuallly obtained invoking global macros "__FILE__" and "__LINE__"."must_copy" forces a copy of string label to be created in dynamic memory. This dynamic memory will be destroyed when the test case gets destroyed or when method TestCase::reset() gets invoked."label" is a constant string generated by the preprocessor macro using #cond; this string constant is not stored in dynamic memory and it must not be destroyed.This method is invoked using macro BUnit_FAILURE().
| void TestCase::recordFailure | ( | const std::string & | label, |
| const char * | fname, | ||
| int | lineno | ||
| ) | [inline, protected, inherited] |
| void TestCase::testThis | ( | bool | cond, |
| const char * | label, | ||
| const char * | fname, | ||
| long | lineno, | ||
| bool | must_copy = false |
||
| ) | [inline, protected, inherited] |
Executes the test and records its result.
successCount().toString() this fact."cond"."fname" and "lineno" show the file and line of the executed test."fname" and "lineno" are values obtained with global macros "__FILE__" and "__LINE__"."must_copy" indicates whether it is necessary to make a copy of string "label"; this copy will be destroyed when the record of not successful test is deleted."label" is a constant genertated by the preprocessor when using macro #cond its memory must not be returned. This method is invoked using macro BUnit_TEST(). {{ // test::testThis()
class MyTest : public TestCase {
public:
bool run() {
bool dont_copy = false;
testThis( 2 == 2, "2 is 2", __FILE__, __LINE__, dont_copy ); // Ok
testThis( 1 == 2, "1 is 2", __FILE__, __LINE__, dont_copy ); // failure #1
testThis( 2 == 1, "2 is 1", __FILE__, __LINE__, dont_copy ); // failure #2
return wasSuccessful();
}
}; // MyTest
MyTest thisTest;
assertTrue( thisTest.wasSuccessful() ); // run() has not been executed
thisTest.run(); // 2 failures
assertTrue( thisTest.failureCount() == 2 );
assertTrue( ! thisTest.wasSuccessful() );
}}
| void TestCase::testThis | ( | bool | cond, |
| const std::string & | label, | ||
| const char * | fname, | ||
| long | lineno | ||
| ) | [inline, protected, inherited] |
Synonym for testThis().
| int TestCase::nPass | ( | ) | const [inline, protected, inherited] |
| int TestCase::nError | ( | ) | const [inline, protected, inherited] |
friend class TestSuite [friend, inherited] |
| void do_toString | ( | const TestCase * | tc, |
| std::basic_ostringstream< char > & | ost | ||
| ) | [friend, inherited] |
int TestCase::m_pass [protected, inherited] |
int TestCase::m_failure [protected, inherited] |
const char * TestCase::m_name [protected, inherited] |
bool TestCase::m_test_suite_destroy [protected, inherited] |
std::list< TestCaseFailure > TestCase::m_failureList [protected, inherited] |
1.8.0