2 * Copyright (c) 2011 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
23 #ifndef DPL_TEST_RUNNER_H
24 #define DPL_TEST_RUNNER_H
26 #include <dpl/singleton.h>
27 #include <dpl/availability.h>
28 #include <dpl/test/test_results_collector.h>
42 typedef std::map<std::string, TestResultsCollectorBasePtr>
43 TestResultsCollectors;
44 TestResultsCollectors m_collectors;
46 std::string m_startTestId;
52 , m_allowChildLogs(false)
54 , m_totalAssertions(0)
57 typedef void (*TestCase)();
65 bool operator <(const TestCaseStruct &other) const
67 return name < other.name;
70 bool operator ==(const TestCaseStruct &other) const
72 return name == other.name;
75 TestCaseStruct(const std::string &n, TestCase p) :
81 typedef std::list<TestCaseStruct> TestCaseStructList;
82 typedef std::map<std::string, TestCaseStructList> TestCaseGroupMap;
83 TestCaseGroupMap m_testGroups;
85 typedef std::set<std::string> SelectedTestNameSet;
86 SelectedTestNameSet m_selectedTestNamesSet;
87 typedef std::set<std::string> SelectedTestGroupSet;
88 SelectedTestGroupSet m_selectedTestGroupSet;
89 std::string m_currentGroup;
91 // Terminate without any logs.
92 // Some test requires to call fork function.
93 // Child process must not produce any logs and should die quietly.
94 bool m_allowChildLogs;
97 std::atomic<int> m_totalAssertions;
100 void InvalidArgs(const std::string& message = "Invalid arguments!");
103 bool filterGroupsByXmls(const std::vector<std::string> & files);
104 bool filterByXML(std::map<std::string, bool> & casesMap);
105 void normalizeXMLTag(std::string& str, const std::string& testcase);
107 enum Status { FAILED, IGNORED, PASS };
109 Status RunTestCase(const TestCaseStruct& testCase);
113 void CollectResult(const std::string& id,
114 const std::string& description,
115 const TestResultsCollectorBase::FailStatus::Type status
116 = TestResultsCollectorBase::FailStatus::NONE,
117 const std::string& reason = std::string());
123 std::string m_message;
129 //! \brief Failed test message creator
131 //! \param[in] aTest string for tested expression
132 //! \param[in] aFile source file name
133 //! \param[in] aLine source file line
134 //! \param[in] aMessage error message
135 TestFailed(const char* aTest,
138 const std::string &aMessage);
140 TestFailed(const std::string &message);
142 std::string GetMessage() const
151 std::string m_message;
157 Ignored(const std::string &message) :
161 std::string GetMessage() const
167 void MarkAssertion();
169 void RegisterTest(const char *testName, TestCase proc);
170 void InitGroup(const char* name);
172 int ExecTestRunner(int argc, char *argv[]);
173 typedef std::vector<std::string> ArgsList;
174 int ExecTestRunner(const ArgsList& args);
175 bool getRunIgnored() const;
176 // The runner will terminate as soon as possible (after current test).
178 bool GetAllowChildLogs();
181 typedef DPL::Singleton<TestRunner> TestRunnerSingleton;
185 #define RUNNER_TEST_GROUP_INIT(GroupName) \
186 static int Static##GroupName##Init() \
188 DPL::Test::TestRunnerSingleton::Instance().InitGroup(#GroupName);\
191 const int DPL_UNUSED Static##GroupName##InitVar = \
192 Static##GroupName##Init();
194 #define RUNNER_TEST(Proc) \
196 static int Static##Proc##Init() \
198 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
201 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
204 #define RUNNER_ASSERT_MSG(test, message) \
207 DPL::Test::TestRunnerSingleton::Instance().MarkAssertion(); \
211 std::ostringstream assertMsg; \
212 assertMsg << message; \
213 throw DPL::Test::TestRunner::TestFailed(#test, \
220 #define RUNNER_ASSERT(test) RUNNER_ASSERT_MSG(test, "")
222 #define RUNNER_FAIL RUNNER_ASSERT(false)
224 #define RUNNER_IGNORED_MSG(message) \
226 std::ostringstream assertMsg; \
227 assertMsg << message; \
228 throw DPL::Test::TestRunner::Ignored( assertMsg.str() ); \
231 #endif // DPL_TEST_RUNNER_H