FreeNOS
TestRunner.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <string.h>
19 #include <ListIterator.h>
20 #include "TestCase.h"
21 #include "TestSuite.h"
22 #include "TestRunner.h"
23 #include "StdoutReporter.h"
24 #include "TAPReporter.h"
25 #include "XMLReporter.h"
26 
27 TestRunner::TestRunner(int argc, char **argv)
28 {
29  // Set member default values.
30  m_argc = argc;
31  m_argv = argv;
32  m_reporter = new StdoutReporter(argc, argv);
33 
34  // Check for command-line specified arguments.
35  for (int i = 0; i < argc; i++)
36  {
37  if (strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "--tap") == 0)
38  {
39  if (m_reporter)
40  delete m_reporter;
41 
42  m_reporter = new TAPReporter(argc, argv);
43  break;
44  }
45  else if (strcmp(argv[i], "-x") == 0 || strcmp(argv[i], "--xml") == 0)
46  {
47  if (m_reporter)
48  delete m_reporter;
49 
50  m_reporter = new XMLReporter(argc, argv);
51  break;
52  }
53  else if (strcmp(argv[i], "-n") == 0)
54  {
55  m_reporter->setStatistics(false);
56  }
57  }
58 }
59 
61 {
62  delete m_reporter;
63 }
64 
66 {
67  return m_reporter;
68 }
69 
70 int TestRunner::run(void)
71 {
72  // Prepare for testing.
74  m_reporter->begin(*tests);
75 
76  // Execute tests. Report per-test stats.
77  for (ListIterator<TestInstance *> i(tests); i.hasCurrent(); i++)
78  {
79  TestInstance *test = i.current();
80  if (!test)
81  break;
82 
83  m_reporter->prepare(*test);
84  TestResult result = test->run();
85  m_reporter->collect(*test, result);
86  }
87  // Finish testing. Report final stats.
88  m_reporter->finish(*tests);
89  return m_reporter->getFailed();
90 }
TestRunner::~TestRunner
virtual ~TestRunner()
Destructor.
Definition: TestRunner.cpp:60
TestRunner::run
int run(void)
Run all discovered tests.
Definition: TestRunner.cpp:70
StrictSingleton< TestSuite >::instance
static TestSuite * instance()
Retrieve the instance.
Definition: Singleton.h:53
StdoutReporter.h
string.h
TestRunner::getReporter
TestReporter * getReporter()
Get test reporter.
Definition: TestRunner.cpp:65
XMLReporter
Output TestResults to standard output in XML format.
Definition: XMLReporter.h:34
TestReporter::begin
virtual void begin(List< TestInstance * > &tests)
Begin testing.
Definition: TestReporter.cpp:88
TestSuite::getTests
List< TestInstance * > * getTests()
Retrieve a list of all tests.
Definition: TestSuite.cpp:32
StdoutReporter
Output TestResults to standard output.
Definition: StdoutReporter.h:34
TestRunner.h
TestReporter::finish
virtual void finish(List< TestInstance * > &tests)
Finish testing.
Definition: TestReporter.cpp:94
TestInstance::run
virtual TestResult run()=0
Run the test instance.
TestRunner::m_argc
int m_argc
Program argument count.
Definition: TestRunner.h:70
TestReporter::prepare
virtual void prepare(TestInstance &test)
Prepare for next test.
Definition: TestReporter.cpp:67
TestSuite.h
TAPReporter.h
ListIterator::hasCurrent
virtual bool hasCurrent() const
Check if there is a current item on the List.
Definition: ListIterator.h:104
TestReporter::collect
virtual void collect(TestInstance &test, TestResult &result)
Collect test statistics.
Definition: TestReporter.cpp:73
XMLReporter.h
strcmp
int strcmp(const char *dest, const char *src)
Compare two strings.
Definition: strcmp.cpp:20
TAPReporter
Output TestResults in TAP format to stdout.
Definition: TAPReporter.h:36
TestResult
Represents a Test result created by a TestInstance.
Definition: TestResult.h:47
ListIterator.h
TestRunner::m_reporter
TestReporter * m_reporter
Reports test results.
Definition: TestRunner.h:76
TestRunner::m_argv
char ** m_argv
Program argument values.
Definition: TestRunner.h:73
TestReporter
Responsible for outputting test results.
Definition: TestReporter.h:35
TestRunner::TestRunner
TestRunner(int argc, char **argv)
Class constructor.
Definition: TestRunner.cpp:27
List< TestInstance * >
TestCase.h
TestReporter::setStatistics
void setStatistics(bool value)
Set final statistics on/off.
Definition: TestReporter.cpp:57
TestReporter::getFailed
uint getFailed() const
Get fail count.
Definition: TestReporter.cpp:42
ListIterator
Iterate through a List.
Definition: ListIterator.h:37
TestInstance
Represents a test instance.
Definition: TestInstance.h:35