CppUnit project page FAQ CppUnit home page

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

TestFixture Class Reference
[Writing test fixture]

Wraps a test case with setUp and tearDown methods. More...

#include <TestFixture.h>

Inheritance diagram for TestFixture:

Inheritance graph
[legend]
List of all members.

Public Methods

virtual ~TestFixture ()
virtual void setUp ()
 \brief Set up context before running a test. More...

virtual void tearDown ()
 Clean up after the test run. More...


Detailed Description

Wraps a test case with setUp and tearDown methods.

A TestFixture is used to provide a common environment for a set of test cases.

To define a test fixture, do the following:

Each test runs in its own fixture so there can be no side effects among test runs. Here is an example:

 class MathTest : public CppUnit::TestFixture {
 protected:
   int m_value1, m_value2;

 public:
   MathTest() {}

   void setUp () {
     m_value1 = 2;
     m_value2 = 3;
   }
 }

For each test implement a method which interacts with the fixture. Verify the expected results with assertions specified by calling CPPUNIT_ASSERT on the expression you want to test:

 public: 
   void testAdd () {
     int result = m_value1 + m_value2;
     CPPUNIT_ASSERT( result == 5 );
   }

Once the methods are defined you can run them. To do this, use a TestCaller.

 CppUnit::Test *test = new CppUnit::TestCaller<MathTest>( "testAdd", 
                                                          &MathTest::testAdd );
 test->run();

The tests to be run can be collected into a TestSuite.

 public: 
   static CppUnit::TestSuite *MathTest::suite () {
      CppUnit::TestSuite *suiteOfTests = new CppUnit::TestSuite;
      suiteOfTests->addTest(new CppUnit::TestCaller<MathTest>(
                              "testAdd", &MathTest::testAdd));
      suiteOfTests->addTest(new CppUnit::TestCaller<MathTest>(
                              "testDivideByZero", &MathTest::testDivideByZero));
      return suiteOfTests;
  }

A set of macros have been created for convenience. They are located in HelperMacros.h.

See also:
TestResult, TestSuite, TestCaller, , CPPUNIT_TEST_SUB_SUITE, CPPUNIT_TEST, CPPUNIT_TEST_SUITE_END, , CPPUNIT_TEST_SUITE_REGISTRATION, CPPUNIT_TEST_EXCEPTION, CPPUNIT_TEST_FAIL.


Constructor & Destructor Documentation

virtual TestFixture::~TestFixture   [inline, virtual]
 


Member Function Documentation

virtual void TestFixture::setUp   [inline, virtual]
 

\brief Set up context before running a test.

Reimplemented in TestCaseDecorator.

virtual void TestFixture::tearDown   [inline, virtual]
 

Clean up after the test run.

Reimplemented in TestCaseDecorator.


The documentation for this class was generated from the following file:
SourceForge Logo hosts this site. Send comments to:
CppUnit Developers