[418] | 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 | |
---|
[477] | 13 | int RunAllTests() { |
---|
[418] | 14 | TestReporterStdout reporter; |
---|
[477] | 15 | TestRunner runner ( reporter ); |
---|
| 16 | return runner.RunTestsIf ( Test::GetTestList(), NULL, True(), 0 ); |
---|
[418] | 17 | } |
---|
| 18 | |
---|
| 19 | |
---|
[477] | 20 | TestRunner::TestRunner ( TestReporter& reporter ) |
---|
| 21 | : m_reporter ( &reporter ) |
---|
| 22 | , m_result ( new TestResults ( &reporter ) ) |
---|
| 23 | , m_timer ( new Timer ) { |
---|
[418] | 24 | m_timer->Start(); |
---|
| 25 | } |
---|
| 26 | |
---|
[477] | 27 | TestRunner::~TestRunner() { |
---|
[418] | 28 | delete m_result; |
---|
| 29 | delete m_timer; |
---|
| 30 | } |
---|
| 31 | |
---|
[477] | 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 | |
---|
[418] | 39 | return m_result->GetFailureCount(); |
---|
| 40 | } |
---|
| 41 | |
---|
[477] | 42 | bool TestRunner::IsTestInSuite ( const Test* const curTest, char const* suiteName ) const { |
---|
[418] | 43 | using namespace std; |
---|
[477] | 44 | return ( suiteName == NULL ) || !strcmp ( curTest->m_details.suiteName, suiteName ); |
---|
[418] | 45 | } |
---|
| 46 | |
---|
[477] | 47 | void TestRunner::RunTest ( TestResults* const result, Test* const curTest, int const maxTestTimeInMs ) const { |
---|
[418] | 48 | CurrentTest::Results() = result; |
---|
| 49 | |
---|
| 50 | Timer testTimer; |
---|
| 51 | testTimer.Start(); |
---|
| 52 | |
---|
[477] | 53 | result->OnTestStart ( curTest->m_details ); |
---|
[418] | 54 | |
---|
| 55 | curTest->Run(); |
---|
| 56 | |
---|
| 57 | double const testTimeInMs = testTimer.GetTimeInMs(); |
---|
[477] | 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."; |
---|
[418] | 62 | |
---|
[477] | 63 | result->OnTestFailure ( curTest->m_details, stream.GetText() ); |
---|
[418] | 64 | } |
---|
| 65 | |
---|
[477] | 66 | result->OnTestFinish ( curTest->m_details, static_cast<float> ( testTimeInMs / 1000.0 ) ); |
---|
[418] | 67 | } |
---|
| 68 | |
---|
| 69 | } |
---|