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 CollectResult(testCase.name,
104 TestResultsCollectorBase::FailStatus::FAILED,
108 catch (const Ignored &e)
111 // Simple test have to be implemented
112 CollectResult(testCase.name,
114 TestResultsCollectorBase::FailStatus::IGNORED,
120 catch (const DPL::Exception &e)
122 // DPL exception failure
123 CollectResult(testCase.name,
125 TestResultsCollectorBase::FailStatus::INTERNAL,
126 "DPL exception:" + e.GetMessage());
130 catch (const std::exception &)
132 // std exception failure
133 CollectResult(testCase.name,
135 TestResultsCollectorBase::FailStatus::INTERNAL,
142 // Unknown exception failure
143 CollectResult(testCase.name,
145 TestResultsCollectorBase::FailStatus::INTERNAL,
146 "unknown exception");
151 CollectResult(testCase.name,
153 TestResultsCollectorBase::FailStatus::NONE);
159 void TestRunner::RunTests()
161 using namespace DPL::Colors::Text;
164 std::for_each(m_collectors.begin(),
166 [](const TestResultsCollectors::value_type& collector)
168 collector.second->Start();
171 fprintf(stderr, "%s%s%s\n", GREEN_BEGIN, "Running tests...", GREEN_END);
172 FOREACH(group, m_testGroups) {
173 TestCaseStructList list = group->second;
176 m_collectors.begin(),
178 [&group](const TestResultsCollectors::value_type& collector)
181 CollectCurrentTestGroupName(group->first);
185 for (TestCaseStructList::const_iterator iterator = list.begin();
186 iterator != list.end();
189 TestCaseStruct test = *iterator;
190 if (m_startTestId == test.name)
193 if (m_startTestId.empty()) {
200 std::for_each(m_collectors.begin(),
202 [](const TestResultsCollectors::value_type& collector)
204 collector.second->Finish();
208 fprintf(stderr, "%s%s%s\n\n", GREEN_BEGIN, "Finished", GREEN_END);
211 void TestRunner::CollectResult(
212 const std::string& id,
213 const std::string& description,
214 const TestResultsCollectorBase::FailStatus::Type status,
215 const std::string& reason)
217 std::for_each(m_collectors.begin(),
219 [&](const TestResultsCollectors::value_type& collector)
221 collector.second->CollectResult(id,
228 void TestRunner::Banner()
230 using namespace DPL::Colors::Text;
244 void TestRunner::InvalidArgs(const std::string& message)
246 using namespace DPL::Colors::Text;
254 void TestRunner::Usage()
256 fprintf(stderr, "Usage: runner [options]\n\n");
257 fprintf(stderr, "Output type:\n");
258 fprintf(stderr, " --output=<output type> --output=<output type> ...\n");
259 fprintf(stderr, "\n possible output types:\n");
260 FOREACH (type, TestResultsCollectorBase::GetCollectorsNames()) {
261 fprintf(stderr, " --output=%s\n", type->c_str());
263 fprintf(stderr, "\n example:\n");
264 fprintf(stderr, " test-binary --output=text --output=xml --file=output.xml\n\n");
265 fprintf(stderr, "Other parameters:\n");
267 " --regexp='regexp'\t Only selected tests"
268 " which names match regexp run\n\n");
269 fprintf(stderr, " --start=<test id>\tStart from concrete test id");
270 fprintf(stderr, " --group=<group name>\t Run tests only from one group\n");
271 fprintf(stderr, " --runignored\t Run also ignored tests\n");
272 fprintf(stderr, " --list\t Show a list of Test IDs\n");
273 fprintf(stderr, " --listgroups\t Show a list of Test Group names \n");
274 fprintf(stderr, " --listingroup=<group name>\t Show a list of Test IDS in one group\n");
275 fprintf(stderr, " --help\t This help\n\n");
276 std::for_each(m_collectors.begin(),
278 [](const TestResultsCollectors::value_type& collector)
281 "Output %s has specific args:\n",
282 collector.first.c_str());
286 CollectorSpecificHelp().c_str());
288 fprintf(stderr, "For bug reporting, please write to:\n");
289 fprintf(stderr, "<p.dobrowolsk@samsung.com>\n");
292 int TestRunner::ExecTestRunner(int argc, char *argv[])
294 std::vector<std::string> args;
295 for (int i = 0; i < argc; ++i)
297 args.push_back(argv[i]);
299 return ExecTestRunner(args);
302 void TestRunner::MarkAssertion()
307 int TestRunner::ExecTestRunner(const ArgsList& value)
309 m_runIgnored = false;
310 ArgsList args = value;
311 // Parse command line
312 if (args.size() == 1)
319 args.erase(args.begin());
321 bool showHelp = false;
323 TestResultsCollectorBasePtr currentCollector;
325 // Parse each argument
328 std::string arg = *it;
329 const std::string regexp = "--regexp=";
330 const std::string output = "--output=";
331 const std::string groupId = "--group=";
332 const std::string runIgnored = "--runignored";
333 const std::string listCmd = "--list";
334 const std::string startCmd = "--start=";
335 const std::string listGroupsCmd = "--listgroups";
336 const std::string listInGroup = "--listingroup=";
338 if (currentCollector)
340 if (currentCollector->ParseCollectorSpecificArg(arg))
346 if (arg.find(startCmd) == 0)
348 arg.erase(0, startCmd.length());
349 FOREACH(group, m_testGroups) {
350 FOREACH(tc, group->second) {
351 if (tc->name == arg) {
356 if (!m_startTestId.empty()) break;
358 if (!m_startTestId.empty()) continue;
360 fprintf(stderr, "Start test id has not been found\n");
364 else if (arg.find(groupId) == 0)
366 arg.erase(0, groupId.length());
367 TestCaseGroupMap::iterator found = m_testGroups.find(arg);
368 if (found != m_testGroups.end()) {
369 std::string name = found->first;
370 TestCaseStructList newList = found->second;
371 m_testGroups.clear();
372 m_testGroups[name] = newList;
374 fprintf(stderr, "Group %s not found\n", arg.c_str());
380 else if (arg == runIgnored)
384 else if (arg == listCmd)
386 FOREACH(group, m_testGroups) {
387 FOREACH(test, group->second) {
388 printf("ID:%s:%s\n", group->first.c_str(), test->name.c_str());
393 else if (arg == listGroupsCmd)
395 FOREACH(group, m_testGroups) {
396 printf("GR:%s\n", group->first.c_str());
400 else if (arg.find(listInGroup) == 0)
402 arg.erase(0, listInGroup.length());
403 FOREACH(test, m_testGroups[arg]) {
404 printf("ID:%s\n", test->name.c_str());
408 else if (arg == "--help")
410 else if (arg.find(output) == 0)
412 arg.erase(0, output.length());
413 if (m_collectors.find(arg) != m_collectors.end())
415 InvalidArgs("Multiple outputs of the same type are not supported!");
419 currentCollector.Reset(TestResultsCollectorBase::Create(arg));
420 if (!currentCollector) {
421 InvalidArgs("Unsupported output type!");
425 m_collectors[arg] = currentCollector;
427 else if (arg.find(regexp) == 0)
429 arg.erase(0, regexp.length());
430 if (arg.length() == 0)
437 if (arg[0] == '\'' && arg[arg.length() - 1] == '\'')
440 arg.erase(arg.length() - 1);
443 if (arg.length() == 0)
450 pcrecpp::RE re(arg.c_str());
451 FOREACH(group, m_testGroups) {
452 TestCaseStructList newList;
453 FOREACH(iterator, group->second)
455 if (re.PartialMatch(iterator->name))
457 newList.push_back(*iterator);
460 group->second = newList;
471 currentCollector.Reset();
480 if (m_collectors.empty())
482 TestResultsCollectorBasePtr collector(
483 TestResultsCollectorBase::Create("text"));
484 m_collectors["text"] = collector;
487 for (auto it = m_collectors.begin(); it != m_collectors.end(); ++it)
489 if (!it->second->Configure())
491 fprintf(stderr, "Could not configure selected output");
502 bool TestRunner::getRunIgnored() const