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!");
199 return testName +
" / " + subCategory;
202void UnitTestRunner::beginNewTest (UnitTest*
const test,
const String& subCategory)
207 auto testName = test->
getName();
208 results.add (
new TestResult (testName, subCategory));
210 logMessage (
"-----------------------------------------------------------------");
211 logMessage (
"Starting tests in: " + getTestNameString (testName, subCategory) +
"...");
216void UnitTestRunner::endTest()
218 if (
auto* r = results.getLast())
224 String m (
"FAILED!! ");
225 m << r->failures << (r->failures == 1 ?
" test" :
" tests")
226 <<
" failed, out of a total of " << (r->passes + r->failures);
234 logMessage (
"Completed tests in " + getTestNameString (r->unitTestName, r->subcategoryName));
239void UnitTestRunner::addPass()
242 const ScopedLock sl (results.getLock());
244 auto* r = results.getLast();
245 jassert (r !=
nullptr);
251 String message (
"Test ");
252 message << (r->failures + r->passes) <<
" passed";
260void UnitTestRunner::addFail (
const String& failureMessage)
263 const ScopedLock sl (results.getLock());
265 auto* r = results.getLast();
266 jassert (r !=
nullptr);
270 String message (
"!!! Test ");
271 message << (r->failures + r->passes) <<
" failed";
273 if (failureMessage.isNotEmpty())
274 message <<
": " << failureMessage;
276 r->messages.add (message);
283 if (assertOnFailure) { jassertfalse; }
void add(const ElementType &newElement)
static void JUCE_CALLTYPE writeToLog(const String &message)
bool addIfNotAlreadyThere(const String &stringToAdd, bool ignoreCase=false)
bool isEmpty() const noexcept
static String toHexString(IntegerType number)
static Time JUCE_CALLTYPE getCurrentTime() noexcept
void runAllTests(int64 randomSeed=0)
void runTestsInCategory(const String &category, int64 randomSeed=0)
virtual ~UnitTestRunner()
virtual bool shouldAbortTests()
const TestResult * getResult(int index) const noexcept
int getNumResults() const noexcept
void setAssertOnFailure(bool shouldAssert) noexcept
void runTests(const Array< UnitTest * > &tests, int64 randomSeed=0)
void setPassesAreLogged(bool shouldDisplayPasses) noexcept
virtual void logMessage(const String &message)
virtual void resultsUpdated()
const String & getName() const noexcept
void logMessage(const String &message)
static StringArray getAllCategories()
static Array< UnitTest * > getTestsInCategory(const String &category)
virtual void initialise()
UnitTest(const String &name, const String &category=String())
static Array< UnitTest * > & getAllTests()
void beginTest(const String &testName)
void expect(bool testResult, const String &failureMessage=String())
void performTest(UnitTestRunner *runner)