[B]asic module for [unit] program testing:
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | Friends
TestSuite< TestCase > Class Template Reference

Test collection. More...

#include <BUnit.h>

Inheritance diagram for TestSuite< TestCase >:
TestCase

List of all members.

Public Types

typedef std::list
< TestCase_is_base_for_TestSuite * > 
container_type
 Container type (choice your preferred).
typedef container_type::iterator iterator
 Container iterador.
typedef const
container_type::iterator 
cons_iterator
 Container iterador (const).
typedef const container_type const_container_type
 Container type (const).

Public Member Functions

 TestSuite (const char *name=0)
 Constructor.
virtual ~TestSuite ()
 Destructor.
bool addTest (TestCase &T)
 Adds test "T" to collection "*this" (and later it will not be destroyed).
bool addTest (TestCase *T)
 Adds test "*pT" to collection "*this" (and later it will be destroyed).
void addSuite (TestSuite &SSS)
 Moves all test from collection "SSS" and ads them to "*this".
bool run ()
 Invokes TestCase::run() for each test in the collection.
void runBare ()
 Invokes TestCase::runBare() for each test in the collection.
int countTestCases () const
 Number of test cases.
int failureCount () const
 Number of test runs that failed.
int successCount () const
 Number of successful test runs.
void reset ()
 Discards all test runs.
const_container_typeallTests () const
 Reference to the container where al the test runs are stored.
const std::string toString () const
 Huuuge string that holds a copy of non successfull test, separated by "\n".
const std::string summary () const
 Returns a string with the name, number of successes and failures.
const std::string toXML () const
 XML string that holds a copy of all unsuccessful tests.
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 runCount () const
 Number of test runs.
int errorCount () const
 Always returns 0 (cero): "Number of errors".
bool wasSuccessful () const
 Returns "true" when all test runs where successful.
std::string getName () const
 Gets the test case name.
void setName (const char *name=0)
 Sets the test case name to "name".
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< TestCaseFailurem_failureList
 Container where test cases that produced failures are stored.

Private Member Functions

bool iAmTestSuite () const
 Returns true only for derived class TestSuite<>.
 TestSuite (const TestSuite &DONTcopy)
 This "private" declaration forbids test case copying.
TestSuiteoperator= (const TestSuite &DONTcopy)
 This "private" declaration forbids test case copying.

Private Attributes

container_type m_allTest
 Container to store pointers to tests.

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.

Detailed Description

template<class TestCase>
class TestSuite< TestCase >

Test collection.

Trick to avoid using file "BUnit.cpp"

Definition at line 187 of file BUnit.h.


Member Typedef Documentation

template<class TestCase>
template< class TestCase > typedef TestSuite< TestCase >::container_type

Container type (choice your preferred).

Definition at line 189 of file BUnit.h.

template<class TestCase>
template< class TestCase > typedef TestSuite< TestCase >::iterator

Container iterador.

Definition at line 190 of file BUnit.h.

template<class TestCase>
template< class TestCase > typedef TestSuite< TestCase >::cons_iterator

Container iterador (const).

Definition at line 191 of file BUnit.h.

template<class TestCase>
template< class TestCase > typedef TestSuite< TestCase >::const_container_type

Container type (const).

Definition at line 192 of file BUnit.h.


Constructor & Destructor Documentation

template<class TestCase>
template< class TestCase > TestSuite< TestCase >::TestSuite ( const char *  name = 0) [inline]

Constructor.

Definition at line 196 of file BUnit.h.

template<class TestCase >
TestSuite< TestCase >::~TestSuite ( ) [virtual]

Destructor.

Definition at line 1673 of file BUnit.h.

template<class TestCase>
template< class TestCase > TestSuite< TestCase >::TestSuite ( const TestSuite< TestCase > &  DONTcopy) [private]

This "private" declaration forbids test case copying.


Member Function Documentation

template<class TestCase >
bool TestSuite< TestCase >::addTest ( TestCase T)

Adds test "T" to collection "*this" (and later it will not be destroyed).

Definition at line 1603 of file BUnit.h.

template<class TestCase >
bool TestSuite< TestCase >::addTest ( TestCase pT)

Adds test "*pT" to collection "*this" (and later it will be destroyed).

Definition at line 1563 of file BUnit.h.

template<class TestCase >
void TestSuite< TestCase >::addSuite ( TestSuite< TestCase > &  SSS) [inline]

Moves all test from collection "SSS" and ads them to "*this".

  • Leaves "SSS" empty.
  • Will not copy tests form SSS to avoid problems that come up when both "SSS" and "*this" try to destroy the same test.
  • It is not possible to make a test hierarchy because all become flattened in a single collection.

Definition at line 1637 of file BUnit.h.

template<class TestCase >
bool TestSuite< TestCase >::run ( ) [virtual]

Invokes TestCase::run() for each test in the collection.

Implements TestCase.

Definition at line 1703 of file BUnit.h.

template<class TestCase >
void TestSuite< TestCase >::runBare ( )

Invokes TestCase::runBare() for each test in the collection.

Reimplemented from TestCase.

Definition at line 1732 of file BUnit.h.

template<class TestCase>
template< class TestCase > int TestSuite< TestCase >::countTestCases ( ) const [inline]

Number of test cases.

Reimplemented from TestCase.

Definition at line 203 of file BUnit.h.

template<class TestCase >
int TestSuite< TestCase >::failureCount ( ) const [virtual]

Number of test runs that failed.

See also:
reset().

Reimplemented from TestCase.

Definition at line 1744 of file BUnit.h.

template<class TestCase >
int TestSuite< TestCase >::successCount ( ) const [virtual]

Number of successful test runs.

See also:
reset().

Reimplemented from TestCase.

Definition at line 1776 of file BUnit.h.

template<class TestCase >
void TestSuite< TestCase >::reset ( ) [virtual]

Discards all test runs.

Reimplemented from TestCase.

Definition at line 1787 of file BUnit.h.

template<class TestCase>
template< class TestCase > TestSuite< TestCase >::const_container_type & TestSuite< TestCase >::allTests ( ) const [inline]

Reference to the container where al the test runs are stored.

Definition at line 207 of file BUnit.h.

template<class TestCase >
const std::string TestSuite< TestCase >::toString ( ) const [virtual]

Huuuge string that holds a copy of non successfull test, separated by "\n".

Reimplemented from TestCase.

Definition at line 1810 of file BUnit.h.

template<class TestCase >
const std::string TestSuite< TestCase >::summary ( ) const [inline, virtual]

Returns a string with the name, number of successes and failures.

Reimplemented from TestCase.

Definition at line 1801 of file BUnit.h.

template<class TestCase >
const std::string TestSuite< TestCase >::toXML ( ) const [virtual]

XML string that holds a copy of all unsuccessful tests.

Reimplemented from TestCase.

Definition at line 1821 of file BUnit.h.

template<class TestCase>
template< class TestCase > bool TestSuite< TestCase >::iAmTestSuite ( ) const [inline, private, virtual]

Returns true only for derived class TestSuite<>.

Reimplemented from TestCase.

Definition at line 212 of file BUnit.h.

template<class TestCase>
template< class TestCase > TestSuite< TestCase >::TestSuite & TestSuite< TestCase >::operator= ( const TestSuite< TestCase > &  DONTcopy) [private]

This "private" declaration forbids test case copying.

bool TestCase::Run ( ) [inline, inherited]

Synonym for run().

Definition at line 123 of file BUnit.h.

bool TestCase::runTest ( ) [inline, inherited]

Synonym for run().

Definition at line 124 of file BUnit.h.

void TestCase::setUp ( ) [inline, virtual, inherited]

Sets the environment for the test run.

Reimplemented in test_rational< INT >, ADH::test_Graph, and test1.

Definition at line 831 of file BUnit.h.

void TestCase::tearDown ( ) [inline, virtual, inherited]

Destroys the test environment.

Definition at line 839 of file BUnit.h.

int TestCase::runCount ( ) const [inline, inherited]

Number of test runs.

Definition at line 129 of file BUnit.h.

int TestCase::errorCount ( ) const [inline, inherited]

Always returns 0 (cero): "Number of errors".

Definition at line 131 of file BUnit.h.

bool TestCase::wasSuccessful ( ) const [inline, inherited]

Returns "true" when all test runs where successful.

Definition at line 134 of file BUnit.h.

std::string TestCase::getName ( ) const [inline, inherited]

Gets the test case name.

See also:
setName().

Definition at line 731 of file BUnit.h.

void TestCase::setName ( const char *  name = 0) [inline, inherited]

Sets the test case name to "name".

  • When "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
            }
        }}
    
    See also:
    test_BUnit::test_setName()

Definition at line 754 of file BUnit.h.

template<class T >
std::string TestCase::toString ( const T &  val) [static, inherited]

Returns a std::string constructed form value val.

Definition at line 1895 of file BUnit.h.

const std::string TestCase::report ( ) const [inline, inherited]

Returns string summary() followed by toString().

Definition at line 142 of file BUnit.h.

const std::string TestCase::failureString ( ) const [inline, inherited]

Synonym for toString().

Definition at line 143 of file BUnit.h.

void TestCase::recordSuccess ( ) [inline, protected, inherited]

Records the test run as a success.

Definition at line 146 of file BUnit.h.

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.

  • Values "fname" and "lineno" indicate the file and line where the test run is executed.
  • The values form "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.
  • Oftentimes string "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().

Definition at line 945 of file BUnit.h.

void TestCase::recordFailure ( const std::string &  label,
const char *  fname,
int  lineno 
) [inline, protected, inherited]

Records that the test did not succeed.

  • En BUnit.h there is no difference between "failures" and "errors".

Definition at line 1004 of file BUnit.h.

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.

  • If the test is successful only increases the numbero of success successCount().
  • If the test is not successful reports in toString() this fact.
  • The result of the test is "cond".
  • Values "fname" and "lineno" show the file and line of the executed test.
  • Usuallyy "fname" and "lineno" are values obtained with global macros "__FILE__" and "__LINE__".
  • The value for "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.
  • Most of the time string "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() );
        }}
    
    See also:
    test_BUnit::test_testThis()

Definition at line 884 of file BUnit.h.

void TestCase::testThis ( bool  cond,
const std::string &  label,
const char *  fname,
long  lineno 
) [inline, protected, inherited]

Synonym for testThis().

Definition at line 901 of file BUnit.h.

int TestCase::nPass ( ) const [inline, protected, inherited]

Synonym for successCount() [OBSOLETE].

Deprecated:

Definition at line 162 of file BUnit.h.

int TestCase::nError ( ) const [inline, protected, inherited]

Synonym for failureCount() [OBSOLETE].

Deprecated:

Definition at line 163 of file BUnit.h.


Friends And Related Function Documentation

friend class TestSuite [friend, inherited]

Colección de pruebas.

Definition at line 161 of file BUnit.h.

template<class TestCase >
void do_toXML ( const TestCase tc,
std::basic_ostringstream< char > &  ost 
) [friend, inherited]

Adds to ost the string of al unsuccessful test from *tc in XML format.

Definition at line 1097 of file BUnit.h.

template<class TestCase >
void do_toString ( const TestCase tc,
std::basic_ostringstream< char > &  ost 
) [friend, inherited]

Adds to ost the string of al unsuccessful test from *tc.

Definition at line 1050 of file BUnit.h.


Member Data Documentation

template<class TestCase>
template< class TestCase > container_type TestSuite< TestCase >::m_allTest [private]

Container to store pointers to tests.

Definition at line 194 of file BUnit.h.

int TestCase::m_pass [protected, inherited]

Number of successful tests.

Definition at line 113 of file BUnit.h.

int TestCase::m_failure [protected, inherited]

Number of test that produced failed.

Definition at line 114 of file BUnit.h.

const char * TestCase::m_name [protected, inherited]

Test case name.

Definition at line 115 of file BUnit.h.

bool TestCase::m_test_suite_destroy [protected, inherited]

Holds "true" if the test case is stored in dynamic memory.

Definition at line 116 of file BUnit.h.

std::list< TestCaseFailure > TestCase::m_failureList [protected, inherited]

Container where test cases that produced failures are stored.

Definition at line 117 of file BUnit.h.


The documentation for this class was generated from the following file: