Skip to content

Commit 4194258

Browse files
authored
Feature: Use pytest.stash instead of protected member (#65)
1 parent 81c00d9 commit 4194258

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

CHANGES.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
Release Notes
22
-------------
33

4+
3.0.0 (unreleased)
5+
------------------
6+
7+
* Use `pytest.stash` internally instead of `_metadata`
8+
9+
* Simplify code
10+
11+
* Thanks to `@LieYangGH <https://github.com/LeiYangGH>`_ for the PR.
12+
413
2.0.4 (2022-10-30)
514
------------------
615

src/pytest_metadata/hooks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

55

6-
def pytest_metadata(metadata):
6+
def pytest_metadata(metadata, config):
77
"""Called after collecting metadata"""

src/pytest_metadata/plugin.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
travis_ci.ENVIRONMENT_VARIABLES,
3232
]
3333

34+
metadata_key = pytest.StashKey[dict]()
35+
3436

3537
def pytest_addhooks(pluginmanager):
3638
from . import hooks
@@ -41,13 +43,13 @@ def pytest_addhooks(pluginmanager):
4143
@pytest.fixture(scope="session")
4244
def metadata(pytestconfig):
4345
"""Provide test session metadata"""
44-
return pytestconfig._metadata
46+
return pytestconfig.stash[metadata_key]
4547

4648

4749
@pytest.fixture(scope="session")
4850
def include_metadata_in_junit_xml(metadata, pytestconfig, record_testsuite_property):
4951
"""Provide test session metadata"""
50-
metadata_ = pytestconfig._metadata
52+
metadata_ = pytestconfig.stash[metadata_key]
5153
for name, value in metadata_.items():
5254
record_testsuite_property(name, value)
5355

@@ -79,46 +81,47 @@ def pytest_addoption(parser):
7981

8082
@pytest.hookimpl(tryfirst=True)
8183
def pytest_configure(config):
82-
config._metadata = {
84+
config.stash[metadata_key] = {
8385
"Python": platform.python_version(),
8486
"Platform": platform.platform(),
8587
"Packages": {
8688
"pytest": pytest.__version__,
8789
"pluggy": pluggy.__version__,
8890
},
8991
}
90-
config._metadata.update({k: v for k, v in config.getoption("metadata")})
91-
config._metadata.update(json.loads(config.getoption("metadata_from_json")))
92+
config.stash[metadata_key].update({k: v for k, v in config.getoption("metadata")})
93+
config.stash[metadata_key].update(
94+
json.loads(config.getoption("metadata_from_json"))
95+
)
9296
if config.getoption("metadata_from_json_file"):
9397
with open(config.getoption("metadata_from_json_file"), "r") as json_file:
94-
config._metadata.update(json.load(json_file))
98+
config.stash[metadata_key].update(json.load(json_file))
9599
plugins = dict()
96100
for plugin, dist in config.pluginmanager.list_plugin_distinfo():
97101
name, version = dist.project_name, dist.version
98102
if name.startswith("pytest-"):
99103
name = name[7:]
100104
plugins[name] = version
101-
config._metadata["Plugins"] = plugins
105+
config.stash[metadata_key]["Plugins"] = plugins
102106

103107
for provider in CONTINUOUS_INTEGRATION:
104108
for var in provider:
105109
if os.environ.get(var):
106-
config._metadata.update({var: os.environ.get(var)})
110+
config.stash[metadata_key].update({var: os.environ.get(var)})
107111

108112
if hasattr(config, "workeroutput"):
109-
config.workeroutput["metadata"] = config._metadata
110-
111-
config.hook.pytest_metadata(metadata=config._metadata)
113+
config.workeroutput["metadata"] = config.stash[metadata_key]
114+
config.hook.pytest_metadata(metadata=config.stash[metadata_key], config=config)
112115

113116

114117
def pytest_report_header(config):
115118
if config.getoption("verbose") > 0:
116-
return "metadata: {0}".format(config._metadata)
119+
return "metadata: {0}".format(config.stash[metadata_key])
117120

118121

119122
@pytest.hookimpl(optionalhook=True)
120123
def pytest_testnodedown(node):
121124
# note that any metadata from remote workers will be replaced with the
122125
# environment from the final worker to quit
123126
if hasattr(node, "workeroutput"):
124-
node.config._metadata.update(node.workeroutput["metadata"])
127+
node.config.stash[metadata_key].update(node.workeroutput["metadata"])

0 commit comments

Comments
 (0)