| 1 | #include "TestRunner.h" |
|---|
| 2 | #include "TestResults.h" |
|---|
| 3 | #include "TestReporter.h" |
|---|
| 4 | #include "TestReporterStdout.h" |
|---|
| 5 | #include "TimeHelpers.h" |
|---|
| 6 | #include "MemoryOutStream.h" |
|---|
| 7 | |
|---|
| 8 | #include <cstring> |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | namespace UnitTest { |
|---|
| 12 | |
|---|
| 13 | int RunAllTests() |
|---|
| 14 | { |
|---|
| 15 | TestReporterStdout reporter; |
|---|
| 16 | TestRunner runner(reporter); |
|---|
| 17 | return runner.RunTestsIf(Test::GetTestList(), NULL, True(), 0); |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | TestRunner::TestRunner(TestReporter& reporter) |
|---|
| 22 | : m_reporter(&reporter) |
|---|
| 23 | , m_result(new TestResults(&reporter)) |
|---|
| 24 | , m_timer(new Timer) |
|---|
| 25 | { |
|---|
| 26 | m_timer->Start(); |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | TestRunner::~TestRunner() |
|---|
| 30 | { |
|---|
| 31 | delete m_result; |
|---|
| 32 | delete m_timer; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | int TestRunner::Finish() const |
|---|
| 36 | { |
|---|
| 37 | float const secondsElapsed = static_cast<float>(m_timer->GetTimeInMs() / 1000.0); |
|---|
| 38 | m_reporter->ReportSummary(m_result->GetTotalTestCount(), |
|---|
| 39 | m_result->GetFailedTestCount(), |
|---|
| 40 | m_result->GetFailureCount(), |
|---|
| 41 | secondsElapsed); |
|---|
| 42 | |
|---|
| 43 | return m_result->GetFailureCount(); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | bool TestRunner::IsTestInSuite(const Test* const curTest, char const* suiteName) const |
|---|
| 47 | { |
|---|
| 48 | using namespace std; |
|---|
| 49 | return (suiteName == NULL) || !strcmp(curTest->m_details.suiteName, suiteName); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | void TestRunner::RunTest(TestResults* const result, Test* const curTest, int const maxTestTimeInMs) const |
|---|
| 53 | { |
|---|
| 54 | CurrentTest::Results() = result; |
|---|
| 55 | |
|---|
| 56 | Timer testTimer; |
|---|
| 57 | testTimer.Start(); |
|---|
| 58 | |
|---|
| 59 | result->OnTestStart(curTest->m_details); |
|---|
| 60 | |
|---|
| 61 | curTest->Run(); |
|---|
| 62 | |
|---|
| 63 | double const testTimeInMs = testTimer.GetTimeInMs(); |
|---|
| 64 | if (maxTestTimeInMs > 0 && testTimeInMs > maxTestTimeInMs && !curTest->m_timeConstraintExempt) |
|---|
| 65 | { |
|---|
| 66 | MemoryOutStream stream; |
|---|
| 67 | stream << "Global time constraint failed. Expected under " << maxTestTimeInMs << |
|---|
| 68 | "ms but took " << testTimeInMs << "ms."; |
|---|
| 69 | |
|---|
| 70 | result->OnTestFailure(curTest->m_details, stream.GetText()); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | result->OnTestFinish(curTest->m_details, static_cast<float>(testTimeInMs/1000.0)); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | } |
|---|