3 # Copyright (c) 2023 Orange 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 classes required to run any Pytest suites."""
19 from xtesting.core import testcase
22 @pytest.hookimpl(hookwrapper=True)
23 def pytest_runtest_makereport(item, call):
24 """Collect all Pytest results"""
25 # pylint: disable=unused-argument
27 report = yreport.get_result()
28 if report.when == 'call':
29 test = {"name": report.nodeid, "status": report.outcome.upper()}
34 test['failure'] = report.longreprtext
35 Pytest.tests.append(test)
38 class Pytest(testcase.TestCase):
42 The pytest driver can be used on every test set written for pytest.
43 Given a pytest package that is launched with the command:
47 pytest --opt1 arg1 --opt2 testdir
49 it can be executed by xtesting with the following testcase.yaml:
60 __logger = logging.getLogger(__name__)
65 def run(self, **kwargs):
67 # - 'dir' is mandatory
68 # - 'options' is an optional list or a dict flatten to a list
69 status = self.EX_RUN_ERROR
70 self.start_time = time.time()
72 pydir = kwargs.pop('dir')
73 options = kwargs.pop('options', {})
74 options['html'] = f'{self.res_dir}/results.html'
75 options['junitxml'] = f'{self.res_dir}/results.xml'
76 if 'tb' not in options:
79 str(item) for opt in zip(
80 [f'--{k}' if len(str(k)) > 1 else
81 f'-{k}' for k in options.keys()],
83 for item in opt if item is not None]
84 with contextlib.redirect_stdout(io.StringIO()) as output:
86 args=[pydir] + ['-p', __name__] + options)
87 with open(f'{self.res_dir}/stdout.log',
88 'w', encoding='utf-8') as output_file:
89 output_file.write(output.getvalue())
92 output.getvalue().splitlines()[-1].replace('=', ''))
93 self.details["tests"] = Pytest.tests
94 if Pytest.passed + Pytest.failed:
95 self.result = Pytest.passed / (
96 Pytest.passed + Pytest.failed) * 100
98 except Exception: # pylint: # pylint: disable=broad-except
99 self.__logger.exception("Cannot execute pytest")
100 self.stop_time = time.time()