File: TestRunner.cpp

package info (click to toggle)
etlcpp 20.40.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 18,548 kB
  • sloc: cpp: 257,359; ansic: 10,566; sh: 1,730; asm: 301; python: 281; makefile: 24
file content (82 lines) | stat: -rw-r--r-- 2,208 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "TestRunner.h"
#include "TestResults.h"
#include "TestReporter.h"
#include "TestReporterStdout.h"
#include "TimeHelpers.h"
#include "MemoryOutStream.h"

#include <cstring>


namespace UnitTest {

   int RunAllTests()
   {
      TestReporterStdout reporter;
      TestRunner runner(reporter);
      return runner.RunTestsIf(Test::GetTestList(), NULL, True(), 0);
   }


   TestRunner::TestRunner(TestReporter& reporter)
      : m_reporter(&reporter)
      , m_result(new TestResults(&reporter))
      , m_timer(new Timer)
   {
      m_timer->Start();
   }

   TestRunner::~TestRunner()
   {
      delete m_result;
      delete m_timer;
   }

   TestResults* TestRunner::GetTestResults()
   {
      return m_result;
   }

   int TestRunner::Finish() const
   {
      float const secondsElapsed = static_cast<float>(m_timer->GetTimeInMs() / 1000.0);
      m_reporter->ReportSummary(m_result->GetTotalTestCount(),
                                m_result->GetFailedTestCount(),
                                m_result->GetFailureCount(),
                                secondsElapsed);

      return m_result->GetFailureCount();
   }

   bool TestRunner::IsTestInSuite(const Test* const curTest, char const* suiteName) const
   {
      using namespace std;
      return (suiteName == NULL) || !strcmp(curTest->m_details.suiteName, suiteName);
   }

   void TestRunner::RunTest(TestResults* const result, Test* const curTest, int const maxTestTimeInMs) const
   {
      if (curTest->m_isMockTest == false)
         CurrentTest::Results() = result;

      Timer testTimer;
      testTimer.Start();

      result->OnTestStart(curTest->m_details);

      curTest->Run();

      double const testTimeInMs = testTimer.GetTimeInMs();
      if (maxTestTimeInMs > 0 && testTimeInMs > maxTestTimeInMs && !curTest->m_details.timeConstraintExempt)
      {
         MemoryOutStream stream;
         stream << "Global time constraint failed. Expected under " << maxTestTimeInMs <<
            "ms but took " << testTimeInMs << "ms.";

         result->OnTestFailure(curTest->m_details, stream.GetText());
      }

      result->OnTestFinish(curTest->m_details, static_cast< float >(testTimeInMs / 1000.0));
   }

}