-
Notifications
You must be signed in to change notification settings - Fork 2
/
pytest_warnings_report.py
82 lines (65 loc) · 2.49 KB
/
pytest_warnings_report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
"""
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())