2 * Copyright (c) 2011-2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19 * @author Lukasz Wrzosek (l.wrzosek@samsung.com)
21 * @brief This file is the header file of test runner
24 #ifndef DPL_TEST_RUNNER_H
25 #define DPL_TEST_RUNNER_H
40 #include <dpl/atomic.h>
41 #include <dpl/availability.h>
42 #include <dpl/colors.h>
43 #include <dpl/gdbbacktrace.h>
44 #include <dpl/singleton.h>
45 #include <dpl/test/performance_result.h>
46 #include <dpl/test/test_exception.h>
47 #include <dpl/test/test_failed.h>
48 #include <dpl/test/test_ignored.h>
49 #include <dpl/test/test_result.h>
50 #include <dpl/test/test_results_collector.h>
56 typedef std::map<std::string, TestResultsCollectorBasePtr>
57 TestResultsCollectors;
58 TestResultsCollectors m_collectors;
60 std::string m_startTestId;
63 std::queue<std::string> m_failReason;
67 m_currentTestCase(nullptr)
69 , m_allowChildLogs(false)
71 , m_firstDeferredExceptionType(DeferredExceptionType::DEFERRED_FAILED)
74 void beginPerformance(std::chrono::system_clock::duration maxDurationInMicroseconds);
75 void endPerformance();
76 void setCurrentTestCasePerformanceResult(const PerformanceResultPtr &performance);
77 ConstPerformanceResultPtr getCurrentTestCasePerformanceResult();
79 void addFailReason(const std::string &reason);
81 typedef void (*TestCase)();
88 PerformanceResultPtr performance;
90 bool operator <(const TestCaseStruct &other) const
92 return name < other.name;
95 bool operator ==(const TestCaseStruct &other) const
97 return name == other.name;
100 TestCaseStruct(const std::string &n, TestCase p) :
106 typedef std::list<TestCaseStruct> TestCaseStructList;
107 typedef std::map<std::string, TestCaseStructList> TestCaseGroupMap;
108 TestCaseGroupMap m_testGroups;
110 TestCaseStruct * m_currentTestCase;
112 typedef std::set<std::string> SelectedTestNameSet;
113 SelectedTestNameSet m_selectedTestNamesSet;
114 typedef std::set<std::string> SelectedTestGroupSet;
115 SelectedTestGroupSet m_selectedTestGroupSet;
116 std::string m_currentGroup;
118 DPL::Atomic m_totalAssertions;
120 // Terminate without any logs.
121 // Some test requires to call fork function.
122 // Child process must not produce any logs and should die quietly.
124 bool m_allowChildLogs;
127 void InvalidArgs(const std::string& message = "Invalid arguments!");
130 bool filterGroupsByXmls(const std::vector<std::string> & files);
131 bool filterByXML(std::map<std::string, bool> & casesMap);
132 void normalizeXMLTag(std::string& str, const std::string& testcase);
134 void RunTestCase(const TestCaseStruct& testCase);
136 void setCurrentTestCase(TestCaseStruct* testCase);
137 TestCaseStruct *getCurrentTestCase();
141 std::string getConcatedFailReason(const std::string &reason);
143 void CollectResult(const std::string& id, const TestResult &result);
146 void MarkAssertion();
148 void RegisterTest(const char *testName, TestCase proc);
149 void InitGroup(const char* name);
151 int ExecTestRunner(int argc, char *argv[]);
152 typedef std::vector<std::string> ArgsList;
153 int ExecTestRunner(ArgsList args);
154 bool getRunIgnored() const;
155 // The runner will terminate as soon as possible (after current test).
157 bool GetAllowChildLogs();
159 void deferFailedException(const DPL::Test::TestFailed &ex);
160 void deferIgnoredException(const DPL::Test::TestIgnored &ex);
165 std::vector<std::string> m_deferredExceptionsMessages;
166 std::size_t m_deferDeepness;
167 enum DeferredExceptionType {
170 } m_firstDeferredExceptionType;
171 DPL::Test::TestFailed m_firstDeferredFail;
172 DPL::Test::TestIgnored m_firstDeferredIgnore;
175 typedef DPL::Singleton<TestRunner> TestRunnerSingleton;
179 #define RUNNER_TEST_GROUP_INIT(GroupName) \
180 static int Static##GroupName##Init() \
182 DPL::Test::TestRunnerSingleton::Instance().InitGroup(#GroupName); \
185 const int DPL_UNUSED Static##GroupName##InitVar = \
186 Static##GroupName##Init();
188 #define RUNNER_TEST(Proc) \
190 static int Static##Proc##Init() \
192 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
195 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
202 * Use them to create assertions in test cases. To do that put them inside test
203 * body. Failing assertion indicates failing test.
206 #define RUNNER_ASSERT_MSG(test, message) \
209 DPL::Test::TestRunnerSingleton::Instance().MarkAssertion(); \
213 std::ostringstream assertMsg; \
214 assertMsg << message << DPL::gdbbacktrace(); \
215 DPL::Test::TestFailed e(#test, \
219 if (!std::uncaught_exception()) \
221 DPL::Test::TestRunnerSingleton::Instance().addFailReason(e.GetMessage()); \
225 #define RUNNER_ASSERT_ERRNO_MSG(test, message) \
228 DPL::Test::TestRunnerSingleton::Instance().MarkAssertion(); \
232 const char *err = strerror(errno); \
233 std::ostringstream assertMsg; \
234 assertMsg << message; \
235 if (!assertMsg.str().empty()) \
237 assertMsg << err << DPL::gdbbacktrace(); \
238 DPL::Test::TestFailed e(#test, \
242 if (!std::uncaught_exception()) \
244 DPL::Test::TestRunnerSingleton::Instance().addFailReason(e.GetMessage()); \
248 #define RUNNER_ASSERT_ERRNO(test) \
249 RUNNER_ASSERT_ERRNO_MSG(test, "")
251 #define RUNNER_FAIL_MSG(message) \
252 RUNNER_ASSERT_MSG(false, message)
254 #define RUNNER_ASSERT(test) \
255 RUNNER_ASSERT_MSG(test, "")
260 * When test reaches this macro call, its furhter code will be ignored.
261 * To ignore whole test, put this macro call at the beginning of this tests
265 #define RUNNER_IGNORED_MSG(message) \
268 std::ostringstream assertMsg; \
269 assertMsg << message; \
270 throw DPL::Test::TestIgnored(assertMsg.str()); \
276 * Use these macros to do the time measurement. The first macro will start time measurement,
277 * the second will gather the result. These macros can be used only once per test-case.
278 * The result of time measurement will be displayed only if the test will pass.
279 * Notice that these macros will work only if will be used in parent process. If these
280 * macros will be used in child process then there will be no time measure results printed.
281 * This macro in multiprocess tests has effect only if used in parent process. This macro
282 * used in child process in multiprocess test has no effect.
283 * The precision of measurement is 1 microsecond - the smallest time value that can be
284 * measured is 0.000001s.
285 * The time measure results will be printed only specific output format:
291 #define RUNNER_PERF_TEST_BEGIN(maxTime) \
293 DPL::Test::TestRunnerSingleton::Instance().beginPerformance( \
294 std::chrono::microseconds{static_cast<long long int>(maxTime*1000000.0)}); \
297 #define RUNNER_PERF_TEST_END() \
299 DPL::Test::TestRunnerSingleton::Instance().endPerformance(); \
305 * Use these macros to print error messages during test run time
308 #define RUNNER_ERROR_MSG(message) \
310 std::cerr << DPL::Colors::Text::RED_BEGIN << message \
311 << DPL::Colors::Text::RED_END << std::endl; \
317 * Use them to defer fails and ignores in test cases.
318 * Some code constructions disallow to throw. Such places can be surrounded
319 * with RUNNER_DEFER_SCOPE macro. RUNNER_DEFER_TRYCATCH macro can be used to catch possibly thrown
320 * exceptions within such scope. Possibly catched exceptions will be rethrown
321 * when leaving RUNNER_DEFER_SCOPE scope.
322 * Macros can be safely nested.
326 #define RUNNER_DEFER_TRYCATCH(scope) \
332 catch (const DPL::Test::TestFailed &ex) \
334 DPL::Test::TestRunnerSingleton::Instance().deferFailedException(ex); \
336 catch (const DPL::Test::TestIgnored &ex) \
338 DPL::Test::TestRunnerSingleton::Instance().deferIgnoredException(ex); \
342 #define RUNNER_DEFER_SCOPE(scope) \
344 DPL::Test::TestRunnerSingleton::Instance().deferBegin(); \
346 DPL::Test::TestRunnerSingleton::Instance().deferEnd(); \
349 #endif // DPL_TEST_RUNNER_H