3 # Copyright (c) 2016 Cable Television Laboratories, Inc. and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 """Define the parent class to run unittest.TestSuite as TestCase."""
12 from __future__ import division
20 from functest.core import testcase
22 __author__ = ("Steven Pisarski <s.pisarski@cablelabs.com>, "
23 "Cedric Ollivier <cedric.ollivier@orange.com>")
26 class Suite(testcase.TestCase):
27 """Base model for running unittest.TestSuite."""
29 def __init__(self, **kwargs):
30 super(Suite, self).__init__(**kwargs)
32 self.logger = logging.getLogger(__name__)
34 def run(self, **kwargs):
35 """Run the test suite.
37 It allows running any unittest.TestSuite and getting its
40 By default, it runs the suite defined as instance attribute.
41 It can be overriden by passing name as arg. It must
42 conform with TestLoader.loadTestsFromName().
44 It sets the following attributes required to push the results
53 kwargs: Arbitrary keyword arguments.
56 TestCase.EX_OK if any TestSuite has been run,
57 TestCase.EX_RUN_ERROR otherwise.
62 self.suite = unittest.TestLoader().loadTestsFromName(name)
64 self.logger.error("Can not import %s", name)
65 return testcase.TestCase.EX_RUN_ERROR
68 self.start_time = time.time()
69 stream = six.StringIO()
70 result = unittest.TextTestRunner(
71 stream=stream, verbosity=2).run(self.suite)
72 self.logger.debug("\n\n%s", stream.getvalue())
73 self.stop_time = time.time()
74 self.details = {"failures": result.failures,
75 "errors": result.errors}
78 (result.testsRun - (len(result.failures) +
79 len(result.errors))) /
81 return testcase.TestCase.EX_OK
82 except ZeroDivisionError:
83 self.logger.error("No test has been run")
84 return testcase.TestCase.EX_RUN_ERROR