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/atomic.h>
29 #include <dpl/test/test_results_collector.h>
41 typedef std::map<std::string, TestResultsCollectorBasePtr>
42 TestResultsCollectors;
43 TestResultsCollectors m_collectors;
45 std::string m_startTestId;
51 , m_allowChildLogs(false)
54 typedef void (*TestCase)();
62 bool operator <(const TestCaseStruct &other) const
64 return name < other.name;
67 bool operator ==(const TestCaseStruct &other) const
69 return name == other.name;
72 TestCaseStruct(const std::string &n, TestCase p) :
78 typedef std::list<TestCaseStruct> TestCaseStructList;
79 typedef std::map<std::string, TestCaseStructList> TestCaseGroupMap;
80 TestCaseGroupMap m_testGroups;
82 typedef std::set<std::string> SelectedTestNameSet;
83 SelectedTestNameSet m_selectedTestNamesSet;
84 typedef std::set<std::string> SelectedTestGroupSet;
85 SelectedTestGroupSet m_selectedTestGroupSet;
86 std::string m_currentGroup;
88 DPL::Atomic m_totalAssertions;
90 // Terminate without any logs.
91 // Some test requires to call fork function.
92 // Child process must not produce any logs and should die quietly.
94 bool m_allowChildLogs;
97 void InvalidArgs(const std::string& message = "Invalid arguments!");
100 bool filterGroupsByXmls(const std::vector<std::string> & files);
101 bool filterByXML(std::map<std::string, bool> & casesMap);
102 void normalizeXMLTag(std::string& str, const std::string& testcase);
104 enum Status { FAILED, IGNORED, PASS };
106 Status RunTestCase(const TestCaseStruct& testCase);
110 void CollectResult(const std::string& id,
111 const std::string& description,
112 const TestResultsCollectorBase::FailStatus::Type status
113 = TestResultsCollectorBase::FailStatus::NONE,
114 const std::string& reason = std::string());
120 std::string m_message;
126 //! \brief Failed test message creator
128 //! \param[in] aTest string for tested expression
129 //! \param[in] aFile source file name
130 //! \param[in] aLine source file line
131 //! \param[in] aMessage error message
132 TestFailed(const char* aTest,
135 const std::string &aMessage);
137 TestFailed(const std::string &message);
139 std::string GetMessage() const
148 std::string m_message;
154 Ignored(const std::string &message) :
158 std::string GetMessage() const
164 void MarkAssertion();
166 void RegisterTest(const char *testName, TestCase proc);
167 void InitGroup(const char* name);
169 int ExecTestRunner(int argc, char *argv[]);
170 typedef std::vector<std::string> ArgsList;
171 int ExecTestRunner(const ArgsList& args);
172 bool getRunIgnored() const;
173 // The runner will terminate as soon as possible (after current test).
175 bool GetAllowChildLogs();
178 typedef DPL::Singleton<TestRunner> TestRunnerSingleton;
182 #define RUNNER_TEST_GROUP_INIT(GroupName) \
183 static int Static##GroupName##Init() \
185 DPL::Test::TestRunnerSingleton::Instance().InitGroup(#GroupName); \
188 const int DPL_UNUSED Static##GroupName##InitVar = \
189 Static##GroupName##Init();
191 #define RUNNER_TEST(Proc) \
193 static int Static##Proc##Init() \
195 DPL::Test::TestRunnerSingleton::Instance().RegisterTest(#Proc, &Proc); \
198 const int DPL_UNUSED Static##Proc##InitVar = Static##Proc##Init(); \
201 //! \brief Returns base name for path
203 #define RUNNER_ASSERT_MSG(test, message) \
206 DPL::Test::TestRunnerSingleton::Instance().MarkAssertion(); \
210 std::ostringstream assertMsg; \
211 assertMsg << message; \
212 throw DPL::Test::TestRunner::TestFailed(#test, \
219 #define RUNNER_ASSERT(test) RUNNER_ASSERT_MSG(test, "")
221 #define RUNNER_FAIL RUNNER_ASSERT(false)
223 #define RUNNER_IGNORED_MSG(message) do { std::ostringstream assertMsg; \
224 assertMsg << message; \
225 throw DPL::Test::TestRunner::Ignored( \
229 #endif // DPL_TEST_RUNNER_H