Loading...
Searching...
No Matches
juce::UnitTest Class Referenceabstract

## Detailed Description

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() override
{
testCase ("Part 1", [&]
{
expect (myFoobar.doesSomething());
expect (myFoobar.doesSomethingElse());
});
testCase ("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;
juce::UnitTest::testCase
void testCase(const String &testName, Invokable &&test)
Begins running a new subsection of the test.
Definition juce_UnitTest.h:168
juce::UnitTest::UnitTest
UnitTest(const String &name, const String &category=String())
Creates a test with the given name and optionally places it in a category.
juce::UnitTest::expect
void expect(bool testResult, const String &failureMessage=String())
Checks that the result of a test is true, and logs this result.
juce::UnitTest::runTest
virtual void runTest()=0
Implement this method in your subclass to actually run your tests.

To run a test, use the UnitTestRunner class.

See also
  UnitTestRunner

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

- juce_core/unit_tests/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.
virtual | ~UnitTest ()
| Destructor.
const String & | getName () const noexcept
| Returns the name of the test.
const String & | getCategory () const noexcept
| Returns the category 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.
template<typename Invokable>
void | testCase (const String &testName, Invokable &&test)
| Begins running a new subsection of the test.
void | expect (bool testResult, const String &failureMessage=String())
| 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())
| Compares a value to an expected value.
template<class ValueType>
void | expectNotEquals (ValueType value, ValueType valueToCompareTo, String failureMessage=String())
| Checks whether a value is not equal to a comparison value.
template<class ValueType>
void | expectGreaterThan (ValueType value, ValueType valueToCompareTo, String failureMessage=String())
| Checks whether a value is greater than a comparison value.
template<class ValueType>
void | expectLessThan (ValueType value, ValueType valueToCompareTo, String failureMessage=String())
| Checks whether a value is less than a comparison value.
template<class ValueType>
void | expectGreaterOrEqual (ValueType value, ValueType valueToCompareTo, String failureMessage=String())
| Checks whether a value is greater or equal to a comparison value.
template<class ValueType>
void | expectLessOrEqual (ValueType value, ValueType valueToCompareTo, String failureMessage=String())
| Checks whether a value is less or equal to a comparison value.
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.
void | logMessage (const String &message)
| Writes a message to the test log.
Random | getRandom () const
| Returns a shared RNG that all unit tests should use.

## Static Public Member Functions

static Array< UnitTest * > & | getAllTests ()
| Returns the set of all UnitTest objects that currently exist.
static Array< UnitTest * > | getTestsInCategory (const String &category)
| Returns the set of UnitTests in a specified category.
static Array< UnitTest * > | getTestsWithName (const String &name)
| Returns the set of UnitTests with a specified name.
static StringArray | getAllCategories ()
| Returns a StringArray containing all of the categories of UnitTests that have been registered.

## Constructors and Destructors

## UnitTest()

juce::UnitTest::UnitTest | ( | const String & | name,
|  | const String & | category = String() )

explicit

Creates a test with the given name and optionally places it in a category.

## ~UnitTest()

virtual juce::UnitTest::~UnitTest | ( |  | ) |

virtual

Destructor.

## Member Functions

## getName()

const String & juce::UnitTest::getName | ( |  | ) | const

inline noexcept

Returns the name of the test.

## getCategory()

const String & juce::UnitTest::getCategory | ( |  | ) | const

inline noexcept

Returns the category of the test.

## performTest()

void juce::UnitTest::performTest | ( | UnitTestRunner * | runner | ) |

Runs the test, using the specified UnitTestRunner.

You shouldn't need to call this method directly - use UnitTestRunner::runTests() instead.

## getAllTests()

Array< UnitTest * > & juce::UnitTest::getAllTests | ( |  | ) |

static

Returns the set of all UnitTest objects that currently exist.

## getTestsInCategory()

Array< UnitTest * > juce::UnitTest::getTestsInCategory | ( | const String & | category | ) |

static

Returns the set of UnitTests in a specified category.

## getTestsWithName()

Array< UnitTest * > juce::UnitTest::getTestsWithName | ( | const String & | name | ) |

static

Returns the set of UnitTests with a specified name.

## getAllCategories()

StringArray juce::UnitTest::getAllCategories | ( |  | ) |

static

Returns a StringArray containing all of the categories of UnitTests that have been registered.

## initialise()

virtual void juce::UnitTest::initialise | ( |  | ) |

virtual

You can optionally implement this method to set up your test.

This method will be called before runTest().

## shutdown()

virtual void juce::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.

## runTest()

virtual void juce::UnitTest::runTest | ( |  | ) |

pure virtual

Implement this method in your subclass to actually run your tests.

The content of your implementation should call testCase() and expect() to perform the tests.

## beginTest()

void juce::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.

In general prefer using testCase() rather than manually calling beginTest().

See also
  testCase

Referenced by testCase().

## testCase()

template<typename Invokable>

void juce::UnitTest::testCase | ( | const String & | testName,
|  | Invokable && | test )

inline

Begins running a new subsection of the test.

Example...

void runTest() override
{
testCase ("My first test", [&]
{
// do something
expect (someCondition);
});
testCase ("My second test", [&]
{
// do something else
expect (someOtherCondition);
});
}

References beginTest(), and expect().

## expect()

void juce::UnitTest::expect | ( | bool | testResult,
|  | const String & | failureMessage = String() )

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()
{
testCase ("basic tests", [&]
{
expect (x + y == 2);
expect (getThing() == someThing);
...etc...
});
}
x
float x
Definition juce_UnityPluginInterface.h:197
y
float float y
Definition juce_UnityPluginInterface.h:197

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.

Referenced by testCase().

## expectEquals()

template<class ValueType>

void juce::UnitTest::expectEquals | ( | ValueType | actual,
|  | ValueType | expected,
|  | String | failureMessage = String() )

inline

Compares a value to an expected value.

If they are not equal, prints out a message containing the expected and actual values.

References juce::exactlyEqual().

## expectNotEquals()

template<class ValueType>

void juce::UnitTest::expectNotEquals | ( | ValueType | value,
|  | ValueType | valueToCompareTo,
|  | String | failureMessage = String() )

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.

References juce::exactlyEqual().

## expectGreaterThan()

template<class ValueType>

void juce::UnitTest::expectGreaterThan | ( | ValueType | value,
|  | ValueType | valueToCompareTo,
|  | String | failureMessage = String() )

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.

## expectLessThan()

template<class ValueType>

void juce::UnitTest::expectLessThan | ( | ValueType | value,
|  | ValueType | valueToCompareTo,
|  | String | failureMessage = String() )

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.

## expectGreaterOrEqual()

template<class ValueType>

void juce::UnitTest::expectGreaterOrEqual | ( | ValueType | value,
|  | ValueType | valueToCompareTo,
|  | String | failureMessage = String() )

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.

## expectLessOrEqual()

template<class ValueType>

void juce::UnitTest::expectLessOrEqual | ( | ValueType | value,
|  | ValueType | valueToCompareTo,
|  | String | failureMessage = String() )

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.

## expectWithinAbsoluteError()

template<class ValueType>

void juce::UnitTest::expectWithinAbsoluteError | ( | ValueType | actual,
|  | ValueType | expected,
|  | ValueType | maxAbsoluteError,
|  | String | failureMessage = String() )

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.

## logMessage()

void juce::UnitTest::logMessage | ( | const String & | message | ) |

Writes a message to the test log.

This can only be called from within your runTest() method.

## getRandom()

Random juce::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.

### Purchase

Get JUCE

### Discover

What's New in JUCE Features

### Learn

Documentaion Tutorials Made with JUCE Resources

### Support

JUCE Forum Newsletter Archive

### About

Contact Us JUCE Legal JUCE Licensing FAQ

### Events

Audio Developer Conference
© Raw Material Software Limited