AUnit  0.3.1
Unit testing framework for Arduino platforms inspired by ArduinoUnit.
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  static void run() { getRunner()->runTest(); }
45 
47  static void list() { getRunner()->listTests(); }
48 
53  static void exclude(const char* pattern) {
54  getRunner()->setStatusMatchingPattern(pattern, Test::kStatusSkipped);
55  }
56 
61  static void include(const char* pattern) {
62  getRunner()->setStatusMatchingPattern(pattern, Test::kStatusSetup);
63  }
64 
66  static void setVerbosity(uint8_t verbosity) {
67  getRunner()->setVerbosityFlag(verbosity);
68  }
69 
71  static bool isVerbosity(uint8_t verbosity) {
72  return getRunner()->isVerbosityFlag(verbosity);
73  }
74 
76  static void setPrinter(Print* printer);
77 
79  static void setPassOrFail(bool ok) { getRunner()->setTestPassOrFail(ok); }
80 
94  static void setTimeout(long millis) {
95  getRunner()->setRunnerTimeout(millis);
96  }
97 
98  private:
99  // 10 second timeout for the runner
100  static const unsigned long kTimeoutDefault = 10000;
101 
103  static TestRunner* getRunner();
104 
106  static uint16_t countTests();
107 
108  // Disable copy-constructor and assignment operator
109  TestRunner(const TestRunner&) = delete;
110  TestRunner& operator=(const TestRunner&) = delete;
111 
113  TestRunner();
114 
116  void runTest();
117 
119  void listTests();
120 
122  void printStartRunner();
123 
125  void resolveRun();
126 
128  void resolveTest(Test* testCase);
129 
131  void setupRunner();
132 
134  void setVerbosityFlag(uint8_t verbosity) { mVerbosity = verbosity; }
135 
137  bool isVerbosityFlag(uint8_t verbosity) { return mVerbosity & verbosity; }
138 
140  void setStatusMatchingPattern(const char* pattern, uint8_t status);
141 
143  void setTestPassOrFail(bool ok) { (*mCurrent)->setPassOrFail(ok); }
144 
146  void setRunnerTimeout(unsigned long timeout);
147 
148  // The current test case is represented by a pointer to a pointer. This
149  // allows treating the root node the same as all the other nodes, and
150  // simplifies the code traversing the singly-linked list significantly.
151  Test** mCurrent;
152 
153  bool mIsResolved;
154  bool mIsSetup;
155  bool mIsRunning;
156  uint8_t mVerbosity;
157  uint16_t mCount;
158  uint16_t mPassedCount;
159  uint16_t mFailedCount;
160  uint16_t mSkippedCount;
161  uint16_t mExpiredCount;;
162  unsigned long mTimeout;
163  unsigned long mStartTime;
164 };
165 
166 }
167 
168 #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:100
static void exclude(const char *pattern)
Exclude the tests which match the pattern.
Definition: TestRunner.h:53
static void include(const char *pattern)
Include the tests which match the pattern.
Definition: TestRunner.h:61
Various macros (test(), testing(), externTest(), externTesting()) are defined in this header...
static void setVerbosity(uint8_t verbosity)
Set the verbosity flag.
Definition: TestRunner.h:66
static void run()
Run all tests using the current runner.
Definition: TestRunner.h:44
static const uint8_t kStatusSkipped
Test is skipped, through the exclude() method or skip() was called.
Definition: Test.h:118
static void list()
Print out the known tests.
Definition: TestRunner.h:47
static void setPassOrFail(bool ok)
Set the pass/fail status of the current test.
Definition: TestRunner.h:79
static void setPrinter(Print *printer)
Set the output printer.
Definition: TestRunner.cpp:46
static const uint8_t kStatusSetup
Test is set up.
Definition: Test.h:109
static void setTimeout(long millis)
Set test runner timeout across all tests, in millis.
Definition: TestRunner.h:94
static bool isVerbosity(uint8_t verbosity)
Returns true if ANY of the bit flags of &#39;verbosity&#39; is set.
Definition: TestRunner.h:71