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
39 #include <dpl/atomic.h>
40 #include <dpl/availability.h>
41 #include <dpl/colors.h>
42 #include <dpl/gdbbacktrace.h>
43 #include <dpl/singleton.h>
44 #include <dpl/test/performance_result.h>
45 #include <dpl/test/test_result.h>
46 #include <dpl/test/test_results_collector.h>
52 typedef std::map<std::string, TestResultsCollectorBasePtr>
53 TestResultsCollectors;
54 TestResultsCollectors m_collectors;
56 std::string m_startTestId;
59 std::queue<std::string> m_failReason;
63 m_currentTestCase(nullptr)
65 , m_allowChildLogs(false)
68 void beginPerformance(std::chrono::system_clock::duration maxDurationInMicroseconds);
69 void endPerformance();
70 void setCurrentTestCasePerformanceResult(const PerformanceResultPtr &performance);
71 ConstPerformanceResultPtr getCurrentTestCasePerformanceResult();
73 void addFailReason(const std::string &reason);
75 typedef void (*TestCase)();
82 PerformanceResultPtr performance;
84 bool operator <(const TestCaseStruct &other) const
86 return name < other.name;
89 bool operator ==(const TestCaseStruct &other) const
91 return name == other.name;
94 TestCaseStruct(const std::string &n, TestCase p) :
100 typedef std::list<TestCaseStruct> TestCaseStructList;
101 typedef std::map<std::string, TestCaseStructList> TestCaseGroupMap;
102 TestCaseGroupMap m_testGroups;
104 TestCaseStruct * m_currentTestCase;
106 typedef std::set<std::string> SelectedTestNameSet;
107 SelectedTestNameSet m_selectedTestNamesSet;
108 typedef std::set<std::string> SelectedTestGroupSet;
109 SelectedTestGroupSet m_selectedTestGroupSet;
110 std::string m_currentGroup;
112 DPL::Atomic m_totalAssertions;
114 // Terminate without any logs.
115 // Some test requires to call fork function.
116 // Child process must not produce any logs and should die quietly.
118 bool m_allowChildLogs;
121 void InvalidArgs(const std::string& message = "Invalid arguments!");
124 bool filterGroupsByXmls(const std::vector<std::string> & files);
125 bool filterByXML(std::map<std::string, bool> & casesMap);
126 void normalizeXMLTag(std::string& str, const std::string& testcase);
128 enum Status { FAILED, IGNORED, PASS };
130 Status RunTestCase(const TestCaseStruct& testCase);
132 void setCurrentTestCase(TestCaseStruct* testCase);
133 TestCaseStruct *getCurrentTestCase();
137 std::string getConcatedFailReason(const std::string &reason);
139 void CollectResult(const std::string& id, const TestResult &result);
145 std::string m_message;
151 //! \brief Failed test message creator
153 //! \param[in] aTest string for tested expression
154 //! \param[in] aFile source file name
155 //! \param[in] aLine source file line
156 //! \param[in] aMessage error message
157 TestFailed(const char* aTest,
160 const std::string &aMessage);
162 TestFailed(const std::string &message);
164 std::string GetMessage() const
173 std::string m_message;
179 Ignored(const std::string &message) :
183 std::string GetMessage() const
189 void MarkAssertion();
191 void RegisterTest(const char *testName, TestCase proc);
192 void InitGroup(const char* name);
194 int ExecTestRunner(int argc, char *argv[]);
195 typedef std::vector<std::string> ArgsList;
196 int ExecTestRunner(ArgsList args);
197 bool getRunIgnored() const;
198 // The runner will terminate as soon as possible (after current test).
200 bool GetAllowChildLogs();
203 typedef DPL::Singleton<TestRunner> TestRunnerSingleton;
207 #define RUNNER_TEST_GROUP_INIT(GroupName) \
208 static int Static##GroupName##Init() \
210 DPL::Test::TestRunnerSingleton::Instance().InitGroup(#GroupName); \
213 const int DPL_UNUSED Static##GroupName##InitVar = \
214 Static##GroupName##Init();
216 #define RUNNER_TEST(Proc) \
218 static int Static##Proc##Init() \
220 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
223 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
230 * Use them to create assertions in test cases. To do that put them inside test
231 * body. Failing assertion indicates failing test.
234 #define RUNNER_ASSERT_MSG(test, message) \
237 DPL::Test::TestRunnerSingleton::Instance().MarkAssertion(); \
241 std::ostringstream assertMsg; \
242 assertMsg << message << DPL::gdbbacktrace(); \
243 DPL::Test::TestRunner::TestFailed e(#test, \
247 if (!std::uncaught_exception()) \
249 DPL::Test::TestRunnerSingleton::Instance().addFailReason(e.GetMessage()); \
253 #define RUNNER_ASSERT_ERRNO_MSG(test, message) \
256 DPL::Test::TestRunnerSingleton::Instance().MarkAssertion(); \
260 const char *err = strerror(errno); \
261 std::ostringstream assertMsg; \
262 assertMsg << message; \
263 if (!assertMsg.str().empty()) \
265 assertMsg << err << DPL::gdbbacktrace(); \
266 DPL::Test::TestRunner::TestFailed e(#test, \
270 if (!std::uncaught_exception()) \
272 DPL::Test::TestRunnerSingleton::Instance().addFailReason(e.GetMessage()); \
276 #define RUNNER_ASSERT_ERRNO(test) \
277 RUNNER_ASSERT_ERRNO_MSG(test, "")
279 #define RUNNER_FAIL_MSG(message) \
280 RUNNER_ASSERT_MSG(false, message)
282 #define RUNNER_ASSERT(test) \
283 RUNNER_ASSERT_MSG(test, "")
288 * When test reaches this macro call, its furhter code will be ignored.
289 * To ignore whole test, put this macro call at the beginning of this tests
293 #define RUNNER_IGNORED_MSG(message) \
296 std::ostringstream assertMsg; \
297 assertMsg << message; \
298 throw DPL::Test::TestRunner::Ignored(assertMsg.str()); \
304 * Use these macros to do the time measurement. The first macro will start time measurement,
305 * the second will gather the result. These macros can be used only once per test-case.
306 * The result of time measurement will be displayed only if the test will pass.
307 * Notice that these macros will work only if will be used in parent process. If these
308 * macros will be used in child process then there will be no time measure results printed.
309 * This macro in multiprocess tests has effect only if used in parent process. This macro
310 * used in child process in multiprocess test has no effect.
311 * The precision of measurement is 1 microsecond - the smallest time value that can be
312 * measured is 0.000001s.
313 * The time measure results will be printed only specific output format:
319 #define RUNNER_PERF_TEST_BEGIN(maxTime) \
321 DPL::Test::TestRunnerSingleton::Instance().beginPerformance( \
322 std::chrono::microseconds{static_cast<long long int>(maxTime*1000000.0)}); \
325 #define RUNNER_PERF_TEST_END() \
327 DPL::Test::TestRunnerSingleton::Instance().endPerformance(); \
333 * Use these macros to print error messages during test run time
336 #define RUNNER_ERROR_MSG(message) \
338 std::cerr << DPL::Colors::Text::RED_BEGIN << message \
339 << DPL::Colors::Text::RED_END << std::endl; \
342 #endif // DPL_TEST_RUNNER_H