Bugfix: fix the criteria of refstack_defcore
[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 import testcase_base as base
9 import unittest
10 import time
11
12
13 class PyTestSuiteRunner(base.TestcaseBase):
14     """
15     This superclass is designed to execute pre-configured unittest.TestSuite()
16     objects
17     """
18     def __init__(self):
19         super(PyTestSuiteRunner, self).__init__()
20         self.suite = None
21
22     def run(self, **kwargs):
23         """
24         Starts test execution from the functest framework
25         """
26         self.start_time = time.time()
27         result = unittest.TextTestRunner(verbosity=2).run(self.suite)
28         self.stop_time = time.time()
29
30         if result.errors:
31             self.logger.error('Number of errors in test suite - ' +
32                               str(len(result.errors)))
33             for test, message in result.errors:
34                 self.logger.error(str(test) + " ERROR with " + message)
35
36         if result.failures:
37             self.logger.error('Number of failures in test suite - ' +
38                               str(len(result.failures)))
39             for test, message in result.failures:
40                 self.logger.error(str(test) + " FAILED with " + message)
41
42         # a result can be PASS or FAIL
43         # But in this case it means that the Execution was OK
44         # we shall distinguish Execution Error from FAIL results
45         # TestcaseBase.EX_RUN_ERROR means that the test case was not run
46         # not that it was run but the result was FAIL
47         exit_code = base.TestcaseBase.EX_OK
48         if ((result.errors and len(result.errors) > 0)
49                 or (result.failures and len(result.failures) > 0)):
50             self.logger.info("%s FAILED" % self.case_name)
51             self.criteria = 'FAIL'
52         else:
53             self.logger.info("%s OK" % self.case_name)
54             self.criteria = 'PASS'
55
56         self.details = {}
57         return exit_code