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