27 : name (nm), category (ctg)
51 if (test->getCategory() == category)
62 if (test->getCategory().isNotEmpty())
73 jassert (newRunner !=
nullptr);
84 jassert (runner !=
nullptr);
92 jassert (runner !=
nullptr);
94 runner->beginNewTest (
this, testName);
100 jassert (runner !=
nullptr);
105 runner->addFail (failureMessage);
111 jassert (runner !=
nullptr);
113 return runner->randomForTest;
117UnitTestRunner::UnitTestRunner() {}
122 assertOnFailure = shouldAssert;
127 logPasses = shouldDisplayPasses;
132 return results.size();
137 return results [index];
152 randomForTest =
Random (randomSeed);
155 for (
auto* t : tests)
160 #if JUCE_EXCEPTIONS_DISABLED
161 t->performTest (
this);
165 t->performTest (
this);
169 addFail (
"An unhandled exception was thrown!");
197void UnitTestRunner::beginNewTest (
UnitTest*
const test,
const String& subCategory)
202 auto* r =
new TestResult();
204 r->unitTestName = test->
getName();
205 r->subcategoryName = subCategory;
209 logMessage (
"-----------------------------------------------------------------");
210 logMessage (
"Starting test: " + r->unitTestName +
" / " + subCategory +
"...");
215void UnitTestRunner::endTest()
217 if (
auto* r = results.getLast())
221 String m (
"FAILED!! ");
222 m << r->failures << (r->failures == 1 ?
" test" :
" tests")
223 <<
" failed, out of a total of " << (r->passes + r->failures);
231 logMessage (
"All tests completed successfully");
236void UnitTestRunner::addPass()
239 const ScopedLock sl (results.getLock());
241 auto* r = results.getLast();
242 jassert (r !=
nullptr);
248 String message (
"Test ");
249 message << (r->failures + r->passes) <<
" passed";
257void UnitTestRunner::addFail (
const String& failureMessage)
260 const ScopedLock sl (results.getLock());
262 auto* r = results.getLast();
263 jassert (r !=
nullptr);
267 String message (
"!!! Test ");
268 message << (r->failures + r->passes) <<
" failed";
270 if (failureMessage.isNotEmpty())
271 message <<
": " << failureMessage;
273 r->messages.add (message);
280 if (assertOnFailure) { jassertfalse; }
Holds a resizable array of primitive or copy-by-value objects.
void add(const ElementType &newElement)
Appends a new element at the end of the array.
static void JUCE_CALLTYPE writeToLog(const String &message)
Writes a string to the current logger.
A random number generator.
int nextInt() noexcept
Returns the next random 32 bit integer.
A special array for holding a list of strings.
bool addIfNotAlreadyThere(const String &stringToAdd, bool ignoreCase=false)
Adds a string to the array as long as it's not already in there.
bool isEmpty() const noexcept
Returns true if the string contains no characters.
static String toHexString(IntegerType number)
Returns a string representing this numeric value in hexadecimal.
Runs a set of unit tests.
void runAllTests(int64 randomSeed=0)
Runs all the UnitTest objects that currently exist.
void runTestsInCategory(const String &category, int64 randomSeed=0)
Runs all the UnitTest objects within a specified category.
virtual ~UnitTestRunner()
Destructor.
virtual bool shouldAbortTests()
This can be overridden to let the runner know that it should abort the tests as soon as possible,...
const TestResult * getResult(int index) const noexcept
Returns one of the TestResult objects that describes a test that has been run.
int getNumResults() const noexcept
Returns the number of TestResult objects that have been performed.
void setAssertOnFailure(bool shouldAssert) noexcept
Sets a flag to indicate whether an assertion should be triggered if a test fails.
void runTests(const Array< UnitTest * > &tests, int64 randomSeed=0)
Runs a set of tests.
void setPassesAreLogged(bool shouldDisplayPasses) noexcept
Sets a flag to indicate whether successful tests should be logged.
virtual void logMessage(const String &message)
Logs a message about the current test progress.
virtual void resultsUpdated()
Called when the list of results changes.
This is a base class for classes that perform a unit test.
const String & getName() const noexcept
Returns the name of the test.
void logMessage(const String &message)
Writes a message to the test log.
static StringArray getAllCategories()
Returns a StringArray containing all of the categories of UnitTests that have been registered.
static Array< UnitTest * > getTestsInCategory(const String &category)
Returns the set of UnitTests in a specified category.
virtual void initialise()
You can optionally implement this method to set up your test.
UnitTest(const String &name, const String &category=String())
Creates a test with the given name and optionally places it in a category.
static Array< UnitTest * > & getAllTests()
Returns the set of all UnitTest objects that currently exist.
void beginTest(const String &testName)
Tells the system that a new subsection of tests is beginning.
void expect(bool testResult, const String &failureMessage=String())
Checks that the result of a test is true, and logs this result.
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.
Random getRandom() const
Returns a shared RNG that all unit tests should use.
virtual ~UnitTest()
Destructor.
void performTest(UnitTestRunner *runner)
Runs the test, using the specified UnitTestRunner.
Contains the results of a test.