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.
17 * @file test_runner.cpp
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19 * @author Lukasz Wrzosek (l.wrzosek@samsung.com)
21 * @brief This file is the implementation file of test runner
23 #include <dpl/test/test_runner.h>
24 #include <dpl/test/test_results_collector.h>
25 #include <dpl/exception.h>
26 #include <dpl/scoped_free.h>
27 #include <dpl/foreach.h>
28 #include <dpl/log/log.h>
29 #include <dpl/colors.h>
37 #include <dpl/utils/wrt_global_settings.h>
39 #include <dpl/singleton_impl.h>
40 IMPLEMENT_SINGLETON(DPL::Test::TestRunner)
47 namespace // anonymous
50 std::string BaseName(std::string aPath)
52 ScopedFree<char> path(strdup(aPath.c_str()));
53 if (NULL == path.Get())
55 throw std::bad_alloc();
57 char* baseName = basename(path.Get());
58 std::string retValue = baseName;
62 } // namespace anonymous
64 //! \brief Failed test message creator
66 //! \param[in] aTest string for tested expression
67 //! \param[in] aFile source file name
68 //! \param[in] aLine source file line
69 //! \param[in] aMessage error message
70 TestRunner::TestFailed::TestFailed(const char* aTest,
73 const std::string &aMessage)
75 std::ostringstream assertMsg;
76 assertMsg << "[" << BaseName(aFile) << ":" << aLine
77 << "] Assertion failed ("
78 << aTest << ") " << aMessage;
79 m_message = assertMsg.str();
82 void TestRunner::RegisterTest(const char *testName, TestCase proc)
84 m_testGroups[m_currentGroup].push_back(TestCaseStruct(testName, proc));
87 void TestRunner::InitGroup(const char* name)
89 m_currentGroup = name;
93 TestRunner::Status TestRunner::RunTestCase(const TestCaseStruct& testCase)
99 catch (const TestFailed &e)
101 // Simple test failure
102 m_collector->CollectResult(testCase.name,
104 TestResultsCollectorBase::FailStatus::FAILED,
108 catch (const Ignored &e)
110 // Simple test have to be implemented
111 m_collector->CollectResult(testCase.name,
113 TestResultsCollectorBase::FailStatus::IGNORED,
118 catch (const ToDo &e)
120 // Simple test have to be implemented
121 m_collector->CollectResult(testCase.name,
123 TestResultsCollectorBase::FailStatus::TODO,
128 catch (const DPL::Exception &e)
130 // DPL exception failure
131 m_collector->CollectResult(testCase.name,
133 TestResultsCollectorBase::FailStatus::INTERNAL,
139 catch (const std::exception &)
141 // std exception failure
142 m_collector->CollectResult(testCase.name,
144 TestResultsCollectorBase::FailStatus::INTERNAL,
151 // Unknown exception failure
152 m_collector->CollectResult(testCase.name,
154 TestResultsCollectorBase::FailStatus::INTERNAL,
155 "unknown exception");
160 m_collector->CollectResult(testCase.name,
162 TestResultsCollectorBase::FailStatus::NONE);
167 void TestRunner::RunTests()
169 using namespace DPL::Colors::Text;
172 m_collector->Start();
174 fprintf(stderr, "%s%s%s\n", GREEN_BEGIN, "Running tests...", GREEN_END);
175 FOREACH(group, m_testGroups) {
176 TestCaseStructList list = group->second;
178 m_collector->CollectCurrentTestGroupName(group->first);
181 for (TestCaseStructList::const_iterator iterator = list.begin();
182 iterator != list.end();
185 TestCaseStruct test = *iterator;
186 if (m_startTestId == test.name)
189 if (m_startTestId.empty()) {
196 m_collector->Finish();
198 fprintf(stderr, "%s%s%s\n\n", GREEN_BEGIN, "Finished", GREEN_END);
201 void TestRunner::Banner()
203 using namespace DPL::Colors::Text;
217 void TestRunner::InvalidArgs()
219 using namespace DPL::Colors::Text;
223 "Invalid parameters!",
227 void TestRunner::Usage()
229 fprintf(stderr, "Usage: runner [options]\n\n");
230 fprintf(stderr, "Output type:\n");
231 fprintf(stderr, " --output=<output type>\n");
232 fprintf(stderr, " possible output types:\n");
233 FOREACH (type, TestResultsCollectorBase::GetCollectorsNames()) {
234 fprintf(stderr, " --output=%s\n", type->c_str());
236 fprintf(stderr, "Other parameters:\n");
238 " --regexp='regexp'\t Only selected tests"
239 " which names match regexp run\n\n");
240 fprintf(stderr, " --start=<test id>\tStart from concrete test id");
241 fprintf(stderr, " --group=<group name>\t Run tests only from one group\n");
242 fprintf(stderr, " --list\t Show a list of Test IDs\n");
243 fprintf(stderr, " --listgroups\t Show a list of Test Group names \n");
244 fprintf(stderr, " --listingroup=<group name>\t Show a list of Test IDS in one group\n");
245 fprintf(stderr, " --help\t This help\n\n");
247 fprintf(stderr, "Output %s has specific args:\n", m_collectorName.c_str());
248 fprintf(stderr, "%s\n", m_collector->CollectorSpecificHelp().c_str());
250 fprintf(stderr, "For bug reporting, please write to:\n");
251 fprintf(stderr, "<p.dobrowolsk@samsung.com>\n");
254 int TestRunner::ExecTestRunner(int argc, char *argv[])
256 std::vector<std::string> args;
257 for (int i = 0; i < argc; ++i)
259 args.push_back(argv[i]);
261 return ExecTestRunner(args);
264 void TestRunner::MarkAssertion()
269 int TestRunner::ExecTestRunner(const ArgsList& value)
271 ArgsList args = value;
272 // Parse command line
273 if (args.size() == 1)
280 args.erase(args.begin());
282 bool showHelp = false;
284 // Parse each argument
287 std::string arg = *it;
288 const std::string regexp = "--regexp=";
289 const std::string output = "--output=";
290 const std::string groupId = "--group=";
291 const std::string listCmd = "--list";
292 const std::string startCmd = "--start=";
293 const std::string listGroupsCmd = "--listgroups";
294 const std::string listInGroup = "--listingroup=";
296 if (m_collector && m_collector->ParseCollectorSpecificArg(arg)) continue;
297 else if (arg.find(startCmd) == 0)
299 arg.erase(0, startCmd.length());
300 FOREACH(group, m_testGroups) {
301 FOREACH(tc, group->second) {
302 if (tc->name == arg) {
307 if (!m_startTestId.empty()) break;
309 if (!m_startTestId.empty()) continue;
311 fprintf(stderr, "Start test id has not been found\n");
315 else if (arg.find(groupId) == 0)
317 arg.erase(0, groupId.length());
318 TestCaseGroupMap::iterator found = m_testGroups.find(arg);
319 if (found != m_testGroups.end()) {
320 std::string name = found->first;
321 TestCaseStructList newList = found->second;
322 m_testGroups.clear();
323 m_testGroups[name] = newList;
325 fprintf(stderr, "Group %s not found\n", arg.c_str());
331 else if (arg == listCmd)
333 FOREACH(group, m_testGroups) {
334 FOREACH(test, group->second) {
335 printf("ID:%s:%s\n", group->first.c_str(), test->name.c_str());
340 else if (arg == listGroupsCmd)
342 FOREACH(group, m_testGroups) {
343 printf("GR:%s\n", group->first.c_str());
347 else if (arg.find(listInGroup) == 0)
349 arg.erase(0, listInGroup.length());
350 FOREACH(test, m_testGroups[arg]) {
351 printf("ID:%s\n", test->name.c_str());
355 else if (arg == "--help")
357 else if (arg.find(output) == 0)
359 arg.erase(0, output.length());
360 m_collector.Reset(TestResultsCollectorBase::Create(arg));
366 m_collectorName = arg;
369 else if (arg.find(regexp) == 0)
371 arg.erase(0, regexp.length());
372 if (arg.length() == 0)
379 if (arg[0] == '\'' && arg[arg.length() - 1] == '\'')
382 arg.erase(arg.length() - 1);
385 if (arg.length() == 0)
392 pcrecpp::RE re(arg.c_str());
393 FOREACH(group, m_testGroups) {
394 TestCaseStructList newList;
395 FOREACH(iterator, group->second)
397 if (re.PartialMatch(iterator->name))
399 newList.push_back(*iterator);
402 group->second = newList;
421 m_collector.Reset(TestResultsCollectorBase::Create("text"));
423 if (!m_collector->Configure()) {
424 fprintf(stderr, "Could not configure selected output");