CppUnit project page | FAQ | CppUnit home page |
#include <TestCase.h>
Inheritance diagram for CppUnit::TestCase:
Public Methods | |
TestCase (std::string Name) | |
Constructs a test case. More... | |
TestCase () | |
Constructs a test case for a suite. More... | |
~TestCase () | |
Destructs a test case. More... | |
virtual void | run (TestResult *result) |
Run the test and catch any exceptions that are triggered by it. More... | |
virtual int | countTestCases () const |
Returns a count of all the tests executed. More... | |
std::string | getName () const |
Returns the name of the test case. More... | |
std::string | toString () const |
Returns the name of the test case instance. More... | |
virtual TestResult * | run () |
A default run method. More... | |
virtual void | setUp () |
A hook for fixture set up. More... | |
virtual void | tearDown () |
A hook for fixture tear down. More... | |
Protected Methods | |
virtual void | runTest () |
All the work for runTest is deferred to subclasses. More... | |
TestResult * | defaultResult () |
Create a default TestResult. More... | |
Private Methods | |
TestCase (const TestCase &other) | |
TestCase & | operator= (const TestCase &other) |
Private Attributes | |
const std::string | m_name |
This class is used to implement a simple test case: define a subclass that overrides the runTest method.
A test case defines the fixture to run multiple tests. To define a test case do the following:
class MathTest : public TestCase { protected: int m_value1; protected: int m_value2; public: MathTest (string name) : TestCase (name) { } protected: 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:
protected: void testAdd () { int result = value1 + value2; CPPUNIT_ASSERT (result == 5); }
Once the methods are defined you can run them. To do this, use a TestCaller.
Test *test = new TestCaller<MathTest>("testAdd", MathTest::testAdd); test->run ();
The tests to be run can be collected into a TestSuite.
public: static TestSuite *MathTest::suite () { TestSuite *suiteOfTests = new TestSuite; suiteOfTests->addTest(new TestCaller<MathTest>( "testAdd", testAdd)); suiteOfTests->addTest(new TestCaller<MathTest>( "testDivideByZero", testDivideByZero)); return suiteOfTests; }
|
Constructs a test case.
|
|
Constructs a test case for a suite. This TestCase is intended for use by the TestCaller and should not be used by a test case for which run() is called. |
|
Destructs a test case.
|
|
|
|
Returns a count of all the tests executed.
Reimplemented from CppUnit::Test. |
|
Create a default TestResult.
|
|
Returns the name of the test case.
Reimplemented from CppUnit::Test. |
|
|
|
A default run method.
|
|
Run the test and catch any exceptions that are triggered by it.
Reimplemented from CppUnit::Test. |
|
All the work for runTest is deferred to subclasses.
Reimplemented in CppUnit::Orthodox, and CppUnit::TestCaller. |
|
A hook for fixture set up.
Reimplemented in CppUnit::TestCaller. |
|
A hook for fixture tear down.
Reimplemented in CppUnit::TestCaller. |
|
Returns the name of the test case instance.
Reimplemented from CppUnit::Test. Reimplemented in CppUnit::TestCaller. |
|
|
|
hosts this site. |
Send comments to: CppUnit Developers |