This is a base class for classes that perform a unit test. More...
Public Member Functions | |
| UnitTest (const String &name) | |
| Creates a test with the given name. | |
| virtual | ~UnitTest () |
| Destructor. | |
| const String & | getName () const noexcept |
| Returns the name of the test. | |
| void | performTest (UnitTestRunner *runner) |
| Runs the test, using the specified UnitTestRunner. | |
| virtual void | initialise () |
| You can optionally implement this method to set up your test. | |
| virtual void | shutdown () |
| You can optionally implement this method to clear up after your test has been run. | |
| virtual void | runTest ()=0 |
| Implement this method in your subclass to actually run your tests. | |
| void | beginTest (const String &testName) |
| Tells the system that a new subsection of tests is beginning. | |
| void | expect (bool testResult, const String &failureMessage=String::empty) |
| Checks that the result of a test is true, and logs this result. | |
| template<class ValueType > | |
| void | expectEquals (ValueType actual, ValueType expected, String failureMessage=String::empty) |
| Compares two values, and if they don't match, prints out a message containing the expected and actual result values. | |
| void | logMessage (const String &message) |
| Writes a message to the test log. | |
Static Public Member Functions | |
| static Array< UnitTest * > & | getAllTests () |
| Returns the set of all UnitTest objects that currently exist. | |
This is a base class for classes that perform a unit test.
To write a test using this class, your code should look something like this:
class MyTest : public UnitTest { public: MyTest() : UnitTest ("Foobar testing") {} void runTest() { beginTest ("Part 1"); expect (myFoobar.doesSomething()); expect (myFoobar.doesSomethingElse()); beginTest ("Part 2"); expect (myOtherFoobar.doesSomething()); expect (myOtherFoobar.doesSomethingElse()); ...etc.. } }; // Creating a static instance will automatically add the instance to the array // returned by UnitTest::getAllTests(), so the test will be included when you call // UnitTestRunner::runAllTests() static MyTest test;
To run a test, use the UnitTestRunner class.
| UnitTest::UnitTest | ( | const String & | name | ) | [explicit] |
Creates a test with the given name.
| virtual UnitTest::~UnitTest | ( | ) | [virtual] |
Destructor.
| const String& UnitTest::getName | ( | ) | const |
Returns the name of the test.
| void UnitTest::performTest | ( | UnitTestRunner * | runner | ) |
Runs the test, using the specified UnitTestRunner.
You shouldn't need to call this method directly - use UnitTestRunner::runTests() instead.
| static Array<UnitTest*>& UnitTest::getAllTests | ( | ) | [static] |
Returns the set of all UnitTest objects that currently exist.
| virtual void UnitTest::initialise | ( | ) | [virtual] |
You can optionally implement this method to set up your test.
This method will be called before runTest().
| virtual void UnitTest::shutdown | ( | ) | [virtual] |
You can optionally implement this method to clear up after your test has been run.
This method will be called after runTest() has returned.
| virtual void UnitTest::runTest | ( | ) | [pure virtual] |
Implement this method in your subclass to actually run your tests.
The content of your implementation should call beginTest() and expect() to perform the tests.
| void UnitTest::beginTest | ( | const String & | testName | ) |
Tells the system that a new subsection of tests is beginning.
This should be called from your runTest() method, and may be called as many times as you like, to demarcate different sets of tests.
| void UnitTest::expect | ( | bool | testResult, |
| const String & | failureMessage = String::empty |
||
| ) |
Checks that the result of a test is true, and logs this result.
In your runTest() method, you should call this method for each condition that you want to check, e.g.
void runTest() { beginTest ("basic tests"); expect (x + y == 2); expect (getThing() == someThing); ...etc... }
If testResult is true, a pass is logged; if it's false, a failure is logged. If the failure message is specified, it will be written to the log if the test fails.
| void UnitTest::expectEquals | ( | ValueType | actual, |
| ValueType | expected, | ||
| String | failureMessage = String::empty |
||
| ) |
Compares two values, and if they don't match, prints out a message containing the expected and actual result values.
| void UnitTest::logMessage | ( | const String & | message | ) |
Writes a message to the test log.
This can only be called from within your runTest() method.