Files
edx-platform/openedx/core/pytest_hooks.py
Manjinder Singh 6c69b6d435 Adding code to output pytest warnings. (#22570)
* Added pytest-json-report plugin
	- modifying app-opts in setup.cfg
	- adding hook to all conftest.py files in repo
	- setting report to be saved to test_root/log/warnings.json
	- Writing custom logic to save json report to avoid overwrite if pytest called twice
This was created to allow us to easily parse through test warnings in jenkins
2020-01-02 10:01:52 -05:00

80 lines
2.5 KiB
Python

"""
Module to put all pytest hooks that modify pytest behaviour
"""
import os
import io
import json
def pytest_json_modifyreport(json_report):
"""
- The function is called by pytest-json-report plugin to only output warnings in json format.
- Everything else is removed due to it already being saved by junitxml
- --json-omit flag in does not allow us to remove everything but the warnings
- (the environment metadata is one example of unremoveable data)
- The json warning outputs are meant to be read by jenkins
"""
warnings_flag = "warnings"
if warnings_flag in json_report:
warnings = json_report[warnings_flag]
json_report.clear()
json_report[warnings_flag] = warnings
else:
json_report = {}
return json_report
def create_file_name(dir_path, file_name_postfix, num=0):
"""
Used to create file name with this given
structure: TEST_SUITE + "_" + file_name_postfix + "_ " + num.json
The env variable TEST_SUITE is set in jenkinsfile
This was necessary cause Pytest is run multiple times and we need to make sure old pytest
warning json files are not being overwritten.
"""
name = dir_path + "/"
if "TEST_SUITE" in os.environ:
name += os.environ["TEST_SUITE"] + "_"
name += file_name_postfix
if num != 0:
name += "_" + str(num)
return name + ".json"
def pytest_sessionfinish(session):
"""
Since multiple pytests are running,
this makes sure warnings from different run are not overwritten
"""
dir_path = "test_root/log"
file_name_postfix = "pytest_warnings"
num = 0
# to make sure this doesn't loop forever, putting a maximum
while (
os.path.isfile(create_file_name(dir_path, file_name_postfix, num)) and num < 100
):
num += 1
report = session.config._json_report.report # noqa pylint: disable=protected-access
with io.open(create_file_name(dir_path, file_name_postfix, num), "w") as outfile:
json.dump(report, outfile)
class DeferPlugin(object):
"""Simple plugin to defer pytest-xdist hook functions."""
def pytest_json_modifyreport(self, json_report):
"""standard xdist hook function.
"""
return pytest_json_modifyreport(json_report)
def pytest_sessionfinish(self, session):
return pytest_sessionfinish(session)
def pytest_configure(config):
if config.pluginmanager.hasplugin("json-report"):
config.pluginmanager.register(DeferPlugin())