AUnit  1.3
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:
44  typedef uint8_t TimeoutType;
45 
47  static void run() { getRunner()->runTest(); }
48 
50  static void list() { getRunner()->listTests(); }
51 
56  static void exclude(const char* pattern) {
57  getRunner()->setLifeCycleMatchingPattern(
58  pattern, Test::kLifeCycleExcluded);
59  }
60 
67  static void exclude(const char* testClass, const char* pattern) {
68  getRunner()->setLifeCycleMatchingPattern(testClass, pattern,
70  }
71 
76  static void include(const char* pattern) {
77  getRunner()->setLifeCycleMatchingPattern(pattern, Test::kLifeCycleNew);
78  }
79 
86  static void include(const char* testClass, const char* pattern) {
87  getRunner()->setLifeCycleMatchingPattern(testClass, pattern,
89  }
90 
92  static void setVerbosity(uint8_t verbosity) {
93  getRunner()->setVerbosityFlag(verbosity);
94  }
95 
97  static bool isVerbosity(uint8_t verbosity) {
98  return getRunner()->isVerbosityFlag(verbosity);
99  }
100 
102  static void setPrinter(Print* printer);
103 
110  static void setTimeout(TimeoutType seconds) {
111  getRunner()->setRunnerTimeout(seconds);
112  }
113 
114  private:
116  static const TimeoutType kTimeoutDefault = 10;
117 
119  static const uint8_t kMaxPatternLength = 63 + 1;
120 
122  static TestRunner* getRunner();
123 
125  static uint16_t countTests();
126 
127  // Disable copy-constructor and assignment operator
128  TestRunner(const TestRunner&) = delete;
129  TestRunner& operator=(const TestRunner&) = delete;
130 
132  TestRunner();
133 
135  void runTest();
136 
138  void listTests();
139 
141  void printStartRunner() const;
142 
144  void resolveRun() const;
145 
147  void setupRunner();
148 
150  void setVerbosityFlag(uint8_t verbosity) { mVerbosity = verbosity; }
151 
153  bool isVerbosityFlag(uint8_t verbosity) const {
154  return mVerbosity & verbosity;
155  }
156 
158  void setLifeCycleMatchingPattern(const char* pattern, uint8_t lifeCycle);
159 
164  void setLifeCycleMatchingPattern(const char* testClass, const char* pattern,
165  uint8_t lifeCycle);
166 
168  void setRunnerTimeout(TimeoutType seconds);
169 
170  // The current test case is represented by a pointer to a pointer. This
171  // allows treating the root node the same as all the other nodes, and
172  // simplifies the code traversing the singly-linked list significantly.
173  Test** mCurrent;
174 
175  bool mIsResolved;
176  bool mIsSetup;
177  bool mIsRunning;
178  uint8_t mVerbosity;
179  uint16_t mCount;
180  uint16_t mPassedCount;
181  uint16_t mFailedCount;
182  uint16_t mSkippedCount;
183  uint16_t mExpiredCount;
184  uint16_t mStatusErrorCount;
185  TimeoutType mTimeout;
186  unsigned long mStartTime;
187  unsigned long mEndTime;
188 };
189 
190 }
191 
192 #endif
The class that runs the various test cases defined by the test() and testing() macros.
Definition: TestRunner.h:41
Base class of all test cases.
Definition: Test.h:43
static void exclude(const char *pattern)
Exclude the tests which match the pattern.
Definition: TestRunner.h:56
static void setTimeout(TimeoutType seconds)
Set test runner timeout across all tests, in seconds.
Definition: TestRunner.h:110
static void include(const char *pattern)
Include the tests which match the pattern.
Definition: TestRunner.h:76
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:67
static void setVerbosity(uint8_t verbosity)
Set the verbosity flag.
Definition: TestRunner.h:92
uint8_t TimeoutType
Integer type of the timeout parameter.
Definition: TestRunner.h:44
static void run()
Run all tests using the current runner.
Definition: TestRunner.h:47
static const uint8_t kLifeCycleExcluded
Test is Excluded by an exclude() method.
Definition: Test.h:65
static void list()
Print out the known tests.
Definition: TestRunner.h:50
static void setPrinter(Print *printer)
Set the output printer.
Definition: TestRunner.cpp:47
static const uint8_t kLifeCycleNew
Test is new, needs to be setup.
Definition: Test.h:57
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:86
static bool isVerbosity(uint8_t verbosity)
Returns true if ANY of the bit flags of &#39;verbosity&#39; is set.
Definition: TestRunner.h:97