diff --git a/_pytest/junitxml.py b/_pytest/junitxml.py index 1bb543000b4..e4fbed56c61 100644 --- a/_pytest/junitxml.py +++ b/_pytest/junitxml.py @@ -191,6 +191,11 @@ def finalize(self): @pytest.fixture 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)``. + """ def append_property(name, value): request.node.user_properties.append((name, value)) return append_property diff --git a/doc/en/builtin.rst b/doc/en/builtin.rst index b59399a7923..f57fd0ac5bf 100644 --- a/doc/en/builtin.rst +++ b/doc/en/builtin.rst @@ -100,10 +100,11 @@ 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)``. monkeypatch The returned ``monkeypatch`` fixture provides these helper methods to modify objects, dictionaries or os.environ:: diff --git a/doc/en/example/simple.rst b/doc/en/example/simple.rst index 82347409531..e500ff8d527 100644 --- a/doc/en/example/simple.rst +++ b/doc/en/example/simple.rst @@ -537,7 +537,7 @@ We can run this:: file $REGENDOC_TMPDIR/b/test_error.py, line 1 def test_root(db): # no db here, will error out E fixture 'db' not found - > available fixtures: cache, capfd, capsys, doctest_namespace, monkeypatch, pytestconfig, record_xml_property, recwarn, tmpdir, tmpdir_factory + > available fixtures: cache, capfd, capsys, doctest_namespace, monkeypatch, pytestconfig, record_property, recwarn, tmpdir, tmpdir_factory > use 'pytest --fixtures [testpath]' for help on them. $REGENDOC_TMPDIR/b/test_error.py:1 diff --git a/doc/en/usage.rst b/doc/en/usage.rst index a8c6d40a027..82a5f89fea1 100644 --- a/doc/en/usage.rst +++ b/doc/en/usage.rst @@ -221,19 +221,23 @@ To set the name of the root test suite xml item, you can configure the ``junit_s [pytest] junit_suite_name = my_suite -record_xml_property +record_property ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. versionadded:: 2.8 +.. versionchanged:: 3.3 + + Function renamed from ``record_xml_property`` to ``record_property`` as user + properties are now available to all reporters. If you want to log additional information for a test, you can use the -``record_xml_property`` fixture: +``record_property`` fixture: .. code-block:: python - def test_function(record_xml_property): - record_xml_property("example_key", 1) - assert 0 + def test_function(record_property): + record_property("example_key", 1) + assert True This will add an extra property ``example_key="1"`` to the generated ``testcase`` tag: @@ -246,13 +250,40 @@ This will add an extra property ``example_key="1"`` to the generated -.. warning:: +Alternatively, you can integrate this functionality with custom markers: - ``record_xml_property`` is an experimental feature, and its interface might be replaced - by something more powerful and general in future versions. The - functionality per-se will be kept, however. +.. code-block:: python + + # content of conftest.py + + def pytest_collection_modifyitems(session, config, items): + for item in items: + marker = item.get_marker('test_id') + if marker is not None: + test_id = marker.args[0] + item.user_properties.append(('test_id', test_id)) + +And in your tests: + +.. code-block:: python + + # content of test_function.py - Currently it does not work when used with the ``pytest-xdist`` plugin. + @pytest.mark.test_id(1501) + def test_function(): + assert 0 + +Will result in: + +.. code-block:: xml + + + + + + + +.. warning:: Also please note that using this feature will break any schema verification. This might be a problem when used with some CI servers. diff --git a/testing/test_junitxml.py b/testing/test_junitxml.py index b604c02a3de..475e8ce4cbc 100644 --- a/testing/test_junitxml.py +++ b/testing/test_junitxml.py @@ -846,10 +846,10 @@ def test_record_property(testdir): import pytest @pytest.fixture - def other(record_xml_property): - record_xml_property("bar", 1) - def test_record(record_xml_property, other): - record_xml_property("foo", "<1"); + def other(record_property): + record_property("bar", 1) + def test_record(record_property, other): + record_property("foo", "<1"); """) result, dom = runandparse(testdir, '-rw') node = dom.find_first_by_tag("testsuite") @@ -860,15 +860,15 @@ def test_record(record_xml_property, other): pnodes[1].assert_attr(name="foo", value="<1") result.stdout.fnmatch_lines([ 'test_record_property.py::test_record', - '*record_xml_property*experimental*', + '*record_property*experimental*', ]) def test_record_property_same_name(testdir): testdir.makepyfile(""" - def test_record_with_same_name(record_xml_property): - record_xml_property("foo", "bar") - record_xml_property("foo", "baz") + def test_record_with_same_name(record_property): + record_property("foo", "bar") + record_property("foo", "baz") """) result, dom = runandparse(testdir, '-rw') node = dom.find_first_by_tag("testsuite")