Skip to content

Commit

Permalink
Renamed the fixture record_xml_property to record_property and adapte…
Browse files Browse the repository at this point in the history
…d logic so that the properties are passed to the TestReport object and thus allow compatibility with pytest-xdist.
  • Loading branch information
carlos-jenkins committed Feb 20, 2018
1 parent 97bb6ab commit 3377c2f
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 108 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Brianna Laugher
Bruno Oliveira
Cal Leeming
Carl Friedrich Bolz
Carlos Jenkins
Ceridwen
Charles Cloud
Charnjit SiNGH (CCSJ)
Expand Down
6 changes: 6 additions & 0 deletions _pytest/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class RemovedInPytest4Warning(DeprecationWarning):
"For more details, see: https://docs.pytest.org/en/latest/parametrize.html"
)

RECORD_XML_PROPERTY = (
'Fixture renamed from "record_xml_property" to "record_property" as user '
'properties are now available to all reporters.\n'
'"record_xml_property" is now deprecated.'
)

COLLECTOR_MAKEITEM = RemovedInPytest4Warning(
"pycollector makeitem was removed "
"as it is an accidentially leaked internal api"
Expand Down
44 changes: 29 additions & 15 deletions _pytest/junitxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,31 +233,27 @@ def finalize(self):


@pytest.fixture
def record_xml_property(request):
"""Add extra xml properties to the tag for the calling test.
The fixture is callable with ``(name, value)``, with value being automatically
xml-encoded.
def record_property(request):
"""Add an extra properties the calling test.
User properties become part of the test report and are available to the
configured reporters, like JUnit XML.
The fixture is callable with ``(name, value)``.
"""
request.node.warn(
code='C3',
message='record_xml_property is an experimental feature',
message='record_property is an experimental feature',
)
xml = getattr(request.config, "_xml", None)
if xml is not None:
node_reporter = xml.node_reporter(request.node.nodeid)
return node_reporter.add_property
else:
def add_property_noop(name, value):
pass

return add_property_noop
def append_property(name, value):
request.node.user_properties.append((name, value))
return append_property


@pytest.fixture
def record_xml_attribute(request):
"""Add extra xml attributes to the tag for the calling test.
The fixture is callable with ``(name, value)``, with value being automatically
xml-encoded
The fixture is callable with ``(name, value)``, with value being
automatically xml-encoded
"""
request.node.warn(
code='C3',
Expand All @@ -274,6 +270,20 @@ def add_attr_noop(name, value):
return add_attr_noop


@pytest.fixture
def record_xml_property(request):
"""(Deprecated) use record_property."""
import warnings
from _pytest import deprecated
warnings.warn(
deprecated.RECORD_XML_PROPERTY,
DeprecationWarning,
stacklevel=2
)

return record_property(request)


def pytest_addoption(parser):
group = parser.getgroup("terminal reporting")
group.addoption(
Expand Down Expand Up @@ -442,6 +452,10 @@ def pytest_runtest_logreport(self, report):
if report.when == "teardown":
reporter = self._opentestcase(report)
reporter.write_captured_output(report)

for propname, propvalue in report.user_properties:
reporter.add_property(propname, propvalue)

self.finalize(report)
report_wid = getattr(report, "worker_id", None)
report_ii = getattr(report, "item_index", None)
Expand Down
4 changes: 4 additions & 0 deletions _pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ def __init__(self, name, parent=None, config=None, session=None):
super(Item, self).__init__(name, parent, config, session)
self._report_sections = []

#: user properties is a list of tuples (name, value) that holds user
#: defined properties for this test.
self.user_properties = []

def add_report_section(self, when, key, content):
"""
Adds a new report section, similar to what's done internally to add stdout and
Expand Down
8 changes: 7 additions & 1 deletion _pytest/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ def pytest_runtest_makereport(item, call):
sections.append(("Captured %s %s" % (key, rwhen), content))
return TestReport(item.nodeid, item.location,
keywords, outcome, longrepr, when,
item.user_properties,
sections, duration)


Expand All @@ -326,7 +327,8 @@ class TestReport(BaseReport):
"""

def __init__(self, nodeid, location, keywords, outcome,
longrepr, when, sections=(), duration=0, **extra):
longrepr, when, user_properties,
sections=(), duration=0, **extra):
#: normalized collection node id
self.nodeid = nodeid

Expand All @@ -348,6 +350,10 @@ def __init__(self, nodeid, location, keywords, outcome,
#: one of 'setup', 'call', 'teardown' to indicate runtest phase.
self.when = when

#: user properties is a list of tuples (name, value) that holds user
#: defined properties of the test
self.user_properties = user_properties

#: list of pairs ``(str, str)`` of extra information which needs to
#: marshallable. Used by pytest to add captured text
#: from ``stdout`` and ``stderr``, but may be used by other plugins
Expand Down
2 changes: 2 additions & 0 deletions changelog/2770.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``record_xml_property`` renamed to ``record_property`` and is now compatible with xdist, markers and any reporter.
``record_xml_property`` name is now deprecated.
29 changes: 15 additions & 14 deletions doc/en/builtin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ You can ask for available builtin or project-custom
$ pytest -q --fixtures
cache
Return a cache object that can persist state between testing sessions.

cache.get(key, default)
cache.set(key, value)

Keys must be a ``/`` separated value, where the first part is usually the
name of your plugin or application to avoid clashes with other cache users.

Values can be any object handled by the json stdlib module.
capsys
Enable capturing of writes to sys.stdout/sys.stderr and make
Expand All @@ -112,26 +112,27 @@ You can ask for available builtin or project-custom
Inject names into the doctest namespace.
pytestconfig
the pytest config object with access to command line opts.
record_xml_property
Add extra xml properties to the tag for the calling test.
The fixture is callable with ``(name, value)``, with value being automatically
xml-encoded.
record_property
Add an extra properties the calling test.
User properties become part of the test report and are available to the
configured reporters, like JUnit XML.
The fixture is callable with ``(name, value)``.
record_xml_attribute
Add extra xml attributes to the tag for the calling test.
The fixture is callable with ``(name, value)``, with value being automatically
xml-encoded
caplog
Access and control log capturing.

Captured logs are available through the following methods::

* caplog.text() -> string containing formatted log output
* caplog.records() -> list of logging.LogRecord instances
* caplog.record_tuples() -> list of (logger_name, level, message) tuples
monkeypatch
The returned ``monkeypatch`` fixture provides these
helper methods to modify objects, dictionaries or os.environ::

monkeypatch.setattr(obj, name, value, raising=True)
monkeypatch.delattr(obj, name, raising=True)
monkeypatch.setitem(mapping, name, value)
Expand All @@ -140,17 +141,17 @@ You can ask for available builtin or project-custom
monkeypatch.delenv(name, value, raising=True)
monkeypatch.syspath_prepend(path)
monkeypatch.chdir(path)

All modifications will be undone after the requesting
test function or fixture has finished. The ``raising``
parameter determines if a KeyError or AttributeError
will be raised if the set/deletion operation has no target.
recwarn
Return a WarningsRecorder instance that provides these methods:

* ``pop(category=None)``: return last warning matching the category.
* ``clear()``: clear list of warnings

See http://docs.python.org/library/warnings.html for information
on warning categories.
tmpdir_factory
Expand All @@ -161,5 +162,5 @@ You can ask for available builtin or project-custom
created as a sub directory of the base temporary
directory. The returned object is a `py.path.local`_
path object.

no tests ran in 0.12 seconds
Loading

0 comments on commit 3377c2f

Please sign in to comment.