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