Merge "Update pytest reports generation"

This commit is contained in:
Zuul 2021-02-04 18:49:13 +00:00 committed by Gerrit Code Review
commit d7e8a65948
4 changed files with 32 additions and 2 deletions

View File

@ -1,2 +1,3 @@
[pytest] [pytest]
junit_family=legacy junit_family=legacy
render_collapsed = True

View File

@ -15,6 +15,8 @@
from __future__ import absolute_import from __future__ import absolute_import
from datetime import datetime from datetime import datetime
import os
import subprocess
from oslo_log import log from oslo_log import log
from py.xml import html # pylint: disable=no-name-in-module,import-error from py.xml import html # pylint: disable=no-name-in-module,import-error
@ -23,11 +25,25 @@ import pytest
LOG = log.getLogger(__name__) LOG = log.getLogger(__name__)
TOX_REPORT_NAME = os.environ.get('TOX_REPORT_NAME', "tobiko_results")
@pytest.hookimpl @pytest.hookimpl
def pytest_configure(config): def pytest_configure(config):
configure_metadata(config)
configure_caplog(config) configure_caplog(config)
configure_timeout(config) configure_timeout(config)
configure_junitxml(config)
def configure_metadata(config):
# pylint: disable=protected-access
from tobiko import version
config._metadata["Tobiko Version"] = version.release
git_commit = subprocess.check_output(
['git', 'log', '-n', '1'],
universal_newlines=True).replace('\n', '<br>')
config._metadata["Tobiko Git Commit"] = git_commit
def configure_caplog(config): def configure_caplog(config):
@ -65,6 +81,10 @@ def configure_caplog(config):
set_default_inicfg(config, key, default) set_default_inicfg(config, key, default)
def configure_junitxml(config):
config.inicfg['junit_suite_name'] = TOX_REPORT_NAME
def set_default_inicfg(config, key, default): def set_default_inicfg(config, key, default):
value = config.inicfg.setdefault(key, default) value = config.inicfg.setdefault(key, default)
if value != default: if value != default:
@ -100,6 +120,10 @@ def pytest_runtest_makereport(item, call): # pylint: disable=unused-argument
report.description = str(item.function.__doc__) report.description = str(item.function.__doc__)
def pytest_html_report_title(report):
report.title = f"Tobiko test results ({TOX_REPORT_NAME})"
@pytest.hookimpl(hookwrapper=True) @pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(item): def pytest_runtest_call(item):
# pylint: disable=protected-access # pylint: disable=protected-access

View File

@ -55,6 +55,7 @@ def execute(command, *args, **kwargs):
universal_newlines = kwargs.pop('universal_newlines', True) universal_newlines = kwargs.pop('universal_newlines', True)
check = kwargs.pop('check', True) check = kwargs.pop('check', True)
shell = kwargs.pop('shell', '/bin/bash') shell = kwargs.pop('shell', '/bin/bash')
environ = kwargs.pop('environ', os.environ)
if args or kwargs: if args or kwargs:
command = command.format(*args, **kwargs) command = command.format(*args, **kwargs)
@ -70,7 +71,8 @@ def execute(command, *args, **kwargs):
LOG.info(f"Execute: {command_line}") LOG.info(f"Execute: {command_line}")
result = subprocess.run(command_line, result = subprocess.run(command_line,
stdout=stdout, shell=False, stdout=stdout, shell=False,
universal_newlines=universal_newlines) universal_newlines=universal_newlines,
env=dict(environ))
if check: if check:
result.check_returncode() result.check_returncode()
return result.stdout return result.stdout

View File

@ -140,15 +140,18 @@ def run_test_cases():
cover_options = '' cover_options = ''
if TOX_COVER: if TOX_COVER:
cover_options = f"--cov=tobiko" cover_options = f"--cov=tobiko"
# Pass environment variables to pytest command
environ = dict(os.environ, TOX_REPORT_NAME=TOX_REPORT_NAME)
common.execute(f"pytest " common.execute(f"pytest "
f"{xdist_options} " f"{xdist_options} "
f"{rerun_options} " f"{rerun_options} "
f"{cover_options} " f"{cover_options} "
f"--log-file={TOX_REPORT_LOG} " f"--log-file={TOX_REPORT_LOG} "
f"--junitxml={TOX_REPORT_XML} " f"--junitxml={TOX_REPORT_XML} "
f"--junit-prefix={TOX_REPORT_NAME} "
f"--html={TOX_REPORT_HTML} --self-contained-html " f"--html={TOX_REPORT_HTML} --self-contained-html "
f"{common.get_posargs()}", f"{common.get_posargs()}",
environ=environ,
capture_stdout=False) capture_stdout=False)