forked from pytest-dev/pytest-metadata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.py
131 lines (109 loc) · 3.81 KB
/
plugin.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import json
import os
import platform
try:
import _pytest._pluggy as pluggy
except ImportError:
import pluggy
import pytest
from pytest_metadata.ci import (
appveyor,
bitbucket,
circleci,
codebuild,
gitlab_ci,
jenkins,
taskcluster,
travis_ci,
)
CONTINUOUS_INTEGRATION = [
appveyor.ENVIRONMENT_VARIABLES,
bitbucket.ENVIRONMENT_VARIABLES,
circleci.ENVIRONMENT_VARIABLES,
codebuild.ENVIRONMENT_VARIABLES,
gitlab_ci.ENVIRONMENT_VARIABLES,
jenkins.ENVIRONMENT_VARIABLES,
taskcluster.ENVIRONMENT_VARIABLES,
travis_ci.ENVIRONMENT_VARIABLES,
]
metadata_key = pytest.StashKey[dict]()
def pytest_addhooks(pluginmanager):
from pytest_metadata import hooks
pluginmanager.add_hookspecs(hooks)
@pytest.fixture(scope="session")
def metadata(pytestconfig):
"""Provide test session metadata"""
return pytestconfig.stash[metadata_key]
@pytest.fixture(scope="session")
def include_metadata_in_junit_xml(metadata, pytestconfig, record_testsuite_property):
"""Provide test session metadata"""
metadata_ = pytestconfig.stash[metadata_key]
for name, value in metadata_.items():
record_testsuite_property(name, value)
def pytest_addoption(parser):
group = parser.getgroup("pytest-metadata")
group.addoption(
"--metadata",
action="append",
default=[],
dest="metadata",
metavar=("key", "value"),
nargs=2,
help="additional metadata.",
)
group.addoption(
"--metadata-from-json",
action="store",
default="{}",
dest="metadata_from_json",
help="additional metadata from a json string.",
)
group.addoption(
"--metadata-from-json-file",
type=str,
dest="metadata_from_json_file",
help="additional metadata from a json file.",
)
@pytest.hookimpl(tryfirst=True)
def pytest_configure(config):
config.stash[metadata_key] = {
"Python": platform.python_version(),
"Platform": platform.platform(),
"Packages": {
"pytest": pytest.__version__,
"pluggy": pluggy.__version__,
},
}
config.stash[metadata_key].update({k: v for k, v in config.getoption("metadata")})
config.stash[metadata_key].update(
json.loads(config.getoption("metadata_from_json"))
)
if config.getoption("metadata_from_json_file"):
with open(config.getoption("metadata_from_json_file"), "r") as json_file:
config.stash[metadata_key].update(json.load(json_file))
plugins = dict()
for plugin, dist in config.pluginmanager.list_plugin_distinfo():
name, version = dist.project_name, dist.version
if name.startswith("pytest-"):
name = name[7:]
plugins[name] = version
config.stash[metadata_key]["Plugins"] = plugins
for provider in CONTINUOUS_INTEGRATION:
for var in provider:
if os.environ.get(var):
config.stash[metadata_key].update({var: os.environ.get(var)})
if hasattr(config, "workeroutput"):
config.workeroutput["metadata"] = config.stash[metadata_key]
config.hook.pytest_metadata(metadata=config.stash[metadata_key], config=config)
def pytest_report_header(config):
if config.getoption("verbose") > 0:
return "metadata: {0}".format(config.stash[metadata_key])
@pytest.hookimpl(optionalhook=True)
def pytest_testnodedown(node):
# note that any metadata from remote workers will be replaced with the
# environment from the final worker to quit
if hasattr(node, "workeroutput"):
node.config.stash[metadata_key].update(node.workeroutput["metadata"])