Merge "Use StringIO in odl.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 time
12 import unittest
13
14 import six
15
16 from functest.core import testcase
17
18
19 class PyTestSuiteRunner(testcase.TestCase):
20     """
21     This superclass is designed to execute pre-configured unittest.TestSuite()
22     objects
23     """
24
25     def __init__(self, **kwargs):
26         super(PyTestSuiteRunner, self).__init__(**kwargs)
27         self.suite = None
28         self.logger = logging.getLogger(__name__)
29
30     def run(self, **kwargs):
31         """
32         Starts test execution from the functest framework
33         """
34         try:
35             name = kwargs["name"]
36             try:
37                 self.suite = unittest.TestLoader().loadTestsFromName(name)
38             except ImportError:
39                 self.logger.error("Can not import %s", name)
40                 return testcase.TestCase.EX_RUN_ERROR
41         except KeyError:
42             pass
43         self.start_time = time.time()
44         stream = six.StringIO()
45         result = unittest.TextTestRunner(
46             stream=stream, verbosity=2).run(self.suite)
47         self.logger.debug("\n\n%s", stream.getvalue())
48         self.stop_time = time.time()
49
50         if result.errors:
51             self.logger.error('Number of errors in test suite - ' +
52                               str(len(result.errors)))
53             for test, message in result.errors:
54                 self.logger.error(str(test) + " ERROR with " + message)
55
56         if result.failures:
57             self.logger.error('Number of failures in test suite - ' +
58                               str(len(result.failures)))
59             for test, message in result.failures:
60                 self.logger.error(str(test) + " FAILED with " + message)
61
62         # a result can be PASS or FAIL
63         # But in this case it means that the Execution was OK
64         # we shall distinguish Execution Error from FAIL results
65         # TestCase.EX_RUN_ERROR means that the test case was not run
66         # not that it was run but the result was FAIL
67         exit_code = testcase.TestCase.EX_OK
68         if ((result.errors and len(result.errors) > 0) or
69                 (result.failures and len(result.failures) > 0)):
70             self.logger.info("%s FAILED", self.case_name)
71             self.result = 0
72         else:
73             self.logger.info("%s OK", self.case_name)
74             self.result = 100
75
76         self.details = {}
77         return exit_code