Merge "Fix 2 issues related to logger in pytest_suite_runer.py"
[functest.git] / functest / core / pytest_suite_runner.py
1 # Copyright (c) 2015 All rights reserved
2 # This program and the accompanying materials
3 # are made available under the terms of the Apache License, Version 2.0
4 # which accompanies this distribution, and is available at
5 #
6 # http://www.apache.org/licenses/LICENSE-2.0
7
8 # pylint: disable=missing-docstring
9
10 import logging
11 import unittest
12 import time
13
14 from functest.core import testcase
15
16
17 class PyTestSuiteRunner(testcase.TestCase):
18     """
19     This superclass is designed to execute pre-configured unittest.TestSuite()
20     objects
21     """
22     def __init__(self, **kwargs):
23         super(PyTestSuiteRunner, self).__init__(**kwargs)
24         self.suite = None
25         self.logger = logging.getLogger(__name__)
26
27     def run(self, **kwargs):
28         """
29         Starts test execution from the functest framework
30         """
31         try:
32             name = kwargs["name"]
33             self.suite = unittest.TestLoader().loadTestsFromName(name)
34         except KeyError:
35             pass
36         self.start_time = time.time()
37         result = unittest.TextTestRunner(verbosity=2).run(self.suite)
38         self.stop_time = time.time()
39
40         if result.errors:
41             self.logger.error('Number of errors in test suite - ' +
42                               str(len(result.errors)))
43             for test, message in result.errors:
44                 self.logger.error(str(test) + " ERROR with " + message)
45
46         if result.failures:
47             self.logger.error('Number of failures in test suite - ' +
48                               str(len(result.failures)))
49             for test, message in result.failures:
50                 self.logger.error(str(test) + " FAILED with " + message)
51
52         # a result can be PASS or FAIL
53         # But in this case it means that the Execution was OK
54         # we shall distinguish Execution Error from FAIL results
55         # TestCase.EX_RUN_ERROR means that the test case was not run
56         # not that it was run but the result was FAIL
57         exit_code = testcase.TestCase.EX_OK
58         if ((result.errors and len(result.errors) > 0) or
59                 (result.failures and len(result.failures) > 0)):
60             self.logger.info("%s FAILED", self.case_name)
61             self.result = 0
62         else:
63             self.logger.info("%s OK", self.case_name)
64             self.result = 100
65
66         self.details = {}
67         return exit_code