This is a base class for classes that perform a unit test. More...
#include <juce_UnitTest.h>
Public Member Functions | |
UnitTest (const String &name, const String &category=String()) | |
Creates a test with the given name and optionally places it in a category. More... | |
virtual | ~UnitTest () |
Destructor. More... | |
void | beginTest (const String &testName) |
Tells the system that a new subsection of tests is beginning. More... | |
void | expect (bool testResult, const String &failureMessage=String()) |
Checks that the result of a test is true, and logs this result. More... | |
template<class ValueType > | |
void | expectEquals (ValueType actual, ValueType expected, String failureMessage=String()) |
Compares a value to an expected value. More... | |
template<class ValueType > | |
void | expectGreaterOrEqual (ValueType value, ValueType valueToCompareTo, String failureMessage=String()) |
Checks whether a value is greater or equal to a comparison value. More... | |
template<class ValueType > | |
void | expectGreaterThan (ValueType value, ValueType valueToCompareTo, String failureMessage=String()) |
Checks whether a value is greater than a comparison value. More... | |
template<class ValueType > | |
void | expectLessOrEqual (ValueType value, ValueType valueToCompareTo, String failureMessage=String()) |
Checks whether a value is less or equal to a comparison value. More... | |
template<class ValueType > | |
void | expectLessThan (ValueType value, ValueType valueToCompareTo, String failureMessage=String()) |
Checks whether a value is less than a comparison value. More... | |
template<class ValueType > | |
void | expectNotEquals (ValueType value, ValueType valueToCompareTo, String failureMessage=String()) |
Checks whether a value is not equal to a comparison value. More... | |
template<class ValueType > | |
void | expectWithinAbsoluteError (ValueType actual, ValueType expected, ValueType maxAbsoluteError, String failureMessage=String()) |
Computes the difference between a value and a comparison value, and if it is larger than a specified maximum value, prints out a message containing the actual and comparison values and the maximum allowed error. More... | |
const String & | getCategory () const noexcept |
Returns the category of the test. More... | |
const String & | getName () const noexcept |
Returns the name of the test. More... | |
Random | getRandom () const |
Returns a shared RNG that all unit tests should use. More... | |
virtual void | initialise () |
You can optionally implement this method to set up your test. More... | |
void | logMessage (const String &message) |
Writes a message to the test log. More... | |
void | performTest (UnitTestRunner *runner) |
Runs the test, using the specified UnitTestRunner. More... | |
virtual void | runTest ()=0 |
Implement this method in your subclass to actually run your tests. More... | |
virtual void | shutdown () |
You can optionally implement this method to clear up after your test has been run. More... | |
Static Public Member Functions | |
static StringArray | getAllCategories () |
Returns a StringArray containing all of the categories of UnitTests that have been registered. More... | |
static Array< UnitTest * > & | getAllTests () |
Returns the set of all UnitTest objects that currently exist. More... | |
static Array< UnitTest * > | getTestsInCategory (const String &category) |
Returns the set of UnitTests in a specified category. More... | |
Private Member Functions | |
template<class ValueType > | |
void | expectResultAndPrint (ValueType value, ValueType valueToCompareTo, bool result, String compDescription, String failureMessage) |
Private Attributes | |
const String | category |
const String | name |
UnitTestRunner * | runner |
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:
To run a test, use the UnitTestRunner class.
Creates a test with the given name and optionally places it in a category.
|
virtual |
Destructor.
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.
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.
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.
|
inline |
Compares a value to an expected value.
If they are not equal, prints out a message containing the expected and actual values.
|
inline |
Checks whether a value is greater or equal to a comparison value.
If this check fails, prints out a message containing the actual and comparison values.
|
inline |
Checks whether a value is greater than a comparison value.
If this check fails, prints out a message containing the actual and comparison values.
|
inline |
Checks whether a value is less or equal to a comparison value.
If this check fails, prints out a message containing the actual and comparison values.
|
inline |
Checks whether a value is less than a comparison value.
If this check fails, prints out a message containing the actual and comparison values.
|
inline |
Checks whether a value is not equal to a comparison value.
If this check fails, prints out a message containing the actual and comparison values.
|
inlineprivate |
References String::isEmpty(), and String::isNotEmpty().
|
inline |
Computes the difference between a value and a comparison value, and if it is larger than a specified maximum value, prints out a message containing the actual and comparison values and the maximum allowed error.
|
static |
Returns a StringArray containing all of the categories of UnitTests that have been registered.
Returns the set of all UnitTest objects that currently exist.
|
inlinenoexcept |
Returns the category of the test.
|
inlinenoexcept |
Returns the name of the test.
Random UnitTest::getRandom | ( | ) | const |
Returns a shared RNG that all unit tests should use.
If a test needs random numbers, it's important that when an error is found, the exact circumstances can be re-created in order to re-test the problem, by repeating the test with the same random seed value. To make this possible, the UnitTestRunner class creates a master seed value for the run, writes this number to the log, and then this method returns a Random object based on that seed. All tests should only use this method to create any Random objects that they need.
Note that this method will return an identical object each time it's called for a given run, so if you need several different Random objects, the best way to do that is to call Random::combineSeed() on the result to permute it with a constant value.
Returns the set of UnitTests in a specified category.
|
virtual |
You can optionally implement this method to set up your test.
This method will be called before runTest().
void UnitTest::logMessage | ( | const String & | message | ) |
Writes a message to the test log.
This can only be called from within your runTest() method.
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.
|
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.
|
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.
|
private |
|
private |
|
private |