AUnit  1.5.4
Unit testing framework for Arduino platforms inspired by ArduinoUnit and Google Test.
TestRunner.h
1 /*
2 MIT License
3 
4 Copyright (c) 2018 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #ifndef AUNIT_TEST_RUNNER_H
26 #define AUNIT_TEST_RUNNER_H
27 
28 #include <stdint.h>
29 #include "Test.h"
30 
31 class Print;
32 
33 namespace aunit {
34 
41 class TestRunner {
42  public:
47  typedef uint16_t TimeoutType;
48 
50  static void run() { getRunner()->runTest(); }
51 
53  static void list() { getRunner()->listTests(); }
54 
59  static void exclude(const char* pattern) {
60  getRunner()->setLifeCycleMatchingPattern(
61  pattern, Test::kLifeCycleExcluded);
62  }
63 
70  static void exclude(const char* testClass, const char* pattern) {
71  getRunner()->setLifeCycleMatchingPattern(testClass, pattern,
73  }
74 
79  static void include(const char* pattern) {
80  getRunner()->setLifeCycleMatchingPattern(pattern, Test::kLifeCycleNew);
81  }
82 
89  static void include(const char* testClass, const char* pattern) {
90  getRunner()->setLifeCycleMatchingPattern(testClass, pattern,
92  }
93 
95  static void setVerbosity(uint8_t verbosity) {
96  getRunner()->setVerbosityFlag(verbosity);
97  }
98 
100  static bool isVerbosity(uint8_t verbosity) {
101  return getRunner()->isVerbosityFlag(verbosity);
102  }
103 
105  static void setPrinter(Print* printer);
106 
113  static void setTimeout(TimeoutType seconds) {
114  getRunner()->setRunnerTimeout(seconds);
115  }
116 
117  private:
119  static const TimeoutType kTimeoutDefault = 10;
120 
122  static const uint8_t kMaxPatternLength = 63 + 1;
123 
125  static TestRunner* getRunner();
126 
128  static uint16_t countTests();
129 
130  // Disable copy-constructor and assignment operator
131  TestRunner(const TestRunner&) = delete;
132  TestRunner& operator=(const TestRunner&) = delete;
133 
135  TestRunner();
136 
138  void runTest();
139 
141  void listTests();
142 
144  void printStartRunner() const;
145 
147  void resolveRun() const;
148 
150  void setupRunner();
151 
153  void setVerbosityFlag(uint8_t verbosity) { mVerbosity = verbosity; }
154 
156  bool isVerbosityFlag(uint8_t verbosity) const {
157  return mVerbosity & verbosity;
158  }
159 
161  void setLifeCycleMatchingPattern(const char* pattern, uint8_t lifeCycle);
162 
167  void setLifeCycleMatchingPattern(const char* testClass, const char* pattern,
168  uint8_t lifeCycle);
169 
171  void setRunnerTimeout(TimeoutType seconds);
172 
173  // The current test case is represented by a pointer to a pointer. This
174  // allows treating the root node the same as all the other nodes, and
175  // simplifies the code traversing the singly-linked list significantly.
176  Test** mCurrent = nullptr;
177 
178  bool mIsResolved = false;
179  bool mIsSetup = false;
180  bool mIsRunning = false;
181  uint8_t mVerbosity = Verbosity::kDefault;
182  uint16_t mCount = 0;
183  uint16_t mPassedCount = 0;
184  uint16_t mFailedCount = 0;
185  uint16_t mSkippedCount = 0;
186  uint16_t mExpiredCount = 0;
187  uint16_t mStatusErrorCount = 0;
188  TimeoutType mTimeout = kTimeoutDefault;
189  unsigned long mStartTime;
190  unsigned long mEndTime;
191 };
192 
193 }
194 
195 #endif
aunit::TestRunner::setPrinter
static void setPrinter(Print *printer)
Set the output printer.
Definition: TestRunner.cpp:47
aunit::Test::kLifeCycleExcluded
static const uint8_t kLifeCycleExcluded
Test is Excluded by an exclude() method.
Definition: Test.h:65
aunit::TestRunner::list
static void list()
Print out the known tests.
Definition: TestRunner.h:53
aunit::TestRunner::include
static void include(const char *testClass, const char *pattern)
Include the tests which match the pattern given by (testClass + "_" + pattern), the same concatenatio...
Definition: TestRunner.h:89
aunit::TestRunner::exclude
static void exclude(const char *testClass, const char *pattern)
Exclude the tests which match the pattern given by (testClass + "_" + pattern), the same concatenatio...
Definition: TestRunner.h:70
aunit::Verbosity::kDefault
static const uint8_t kDefault
The default verbosity.
Definition: Verbosity.h:69
aunit::TestRunner::setVerbosity
static void setVerbosity(uint8_t verbosity)
Set the verbosity flag.
Definition: TestRunner.h:95
aunit::TestRunner::TimeoutType
uint16_t TimeoutType
Integer type of the timeout parameter.
Definition: TestRunner.h:47
aunit::TestRunner
The class that runs the various test cases defined by the test() and testing() macros.
Definition: TestRunner.h:41
aunit::TestRunner::run
static void run()
Run all tests using the current runner.
Definition: TestRunner.h:50
aunit::TestRunner::setTimeout
static void setTimeout(TimeoutType seconds)
Set test runner timeout across all tests, in seconds.
Definition: TestRunner.h:113
aunit::TestRunner::isVerbosity
static bool isVerbosity(uint8_t verbosity)
Returns true if ANY of the bit flags of 'verbosity' is set.
Definition: TestRunner.h:100
aunit::TestRunner::exclude
static void exclude(const char *pattern)
Exclude the tests which match the pattern.
Definition: TestRunner.h:59
aunit::Test::kLifeCycleNew
static const uint8_t kLifeCycleNew
Test is new, needs to be setup.
Definition: Test.h:57
aunit::TestRunner::include
static void include(const char *pattern)
Include the tests which match the pattern.
Definition: TestRunner.h:79