Skip to content

attributes can't be added via record_xml_attribute to the JUnit XML #8662

Closed

Description

@gs202

When using it record_xml_attribute, existing attribute can updated but new ones won't be added.

Example for this test (pytest==6.2.4):

class TestClass:
    def test_foo(self):
        assert True

Here is the junit xml file without using record_xml_attribute:

<?xml version="1.0" encoding="utf-8"?>
<testsuites>
    <testsuite errors="0" failures="0" hostname="myhost" name="pytest" skipped="0" tests="1"
               time="0.438" timestamp="2021-05-11T11:17:44.943549">
        <testcase classname="tests.rba.test_me.TestClass" name="test_foo" time="0.015"/>
    </testsuite>
</testsuites>

After adding record_xml_attribute to the test:

class TestClass:
    def test_foo(self, record_xml_attribute):
        record_xml_attribute("assertions", "REQ-1234")
        record_xml_attribute("classname", "custom_classname")
        assert True

The xml file will be:

<?xml version="1.0" encoding="utf-8"?>
<testsuites>
    <testsuite errors="0" failures="0" hostname="myhost" name="pytest" skipped="0" tests="1"
               time="0.415" timestamp="2021-05-11T11:19:42.484839">
        <testcase classname="custom_classname" name="test_foo" time="0.022"/>
    </testsuite>
</testsuites>

As you can see the classname attribute was overridden but the assertions doesn't exist

Activity

  1. added
    plugin: junitxml
    on May 12, 2021
  2. Zac-HD commented on May 12, 2021

    @Zac-HD
    Member

    (edited just to add syntax highlighting 🙂)

  3. gs202 commented on May 12, 2021

    @gs202
    Author

    It looks like the issue is here

            # Filter out attributes not permitted by this test family.
            # Including custom attributes because they are not valid here.
            temp_attrs = {}
            for key in self.attrs.keys():
                if key in families[self.family]["testcase"]:
                    temp_attrs[key] = self.attrs[key]
            self.attrs = temp_attrs

    All custom attributes are filtered out

  4. nicoddemus commented on May 15, 2021

    @nicoddemus
    Member

    Hi @gs202,

    When I execute your example, I get:

    λ pytest .tmp\test_xml.py --junitxml=.tmp\foo.xml --no-header
    ======================== test session starts ========================
    collected 1 item
    
    .tmp\test_xml.py .                                             [100%]
    
    ========================= warnings summary ==========================
    test_xml.py::TestClass::test_foo
      test_xml.py:2: PytestExperimentalApiWarning: record_xml_attribute is an experimental feature
        def test_foo(self, record_xml_attribute):
    
    test_xml.py::TestClass::test_foo
      test_xml.py:2: PytestWarning: record_xml_attribute is incompatible with junit_family 'xunit2' (use 'legacy' or 'xunit1')
        def test_foo(self, record_xml_attribute):
    
    -- Docs: https://docs.pytest.org/en/stable/warnings.html
    -------- generated xml file: e:\projects\pytest\.tmp\foo.xml --------
    =================== 1 passed, 2 warnings in 0.01s ===================
    

    The 2nd warning is the important one: record_xml_attribute is incompatible with junit_family 'xunit2' (use 'legacy' or 'xunit1'). The default junit_family is indeed xunit2 in recent pytest versions.

    Running:

    λ pytest .tmp\test_xml.py --junitxml=.tmp\foo.xml --no-header -o junit_family=xunit1
    

    Then I can see assertions in the generated XML file.

    <?xml version="1.0" encoding="utf-8"?><testsuites><testsuite name="pytest" errors="0" failures="0" skipped="0" tests="1" time="0.000" timestamp="2021-05-15T11:15:09.525431" hostname="DESKTOP-31D77J6"><testcase classname="custom_classname" name="test_foo" file="test_xml.py" line="1" assertions="REQ-1234" time="0.004" /></testsuite></testsuites>

    So everything is working as it should.

    Hope this helps!

  5. gs202 commented on May 18, 2021

    @gs202
    Author

    Thanks @nicoddemus this indeed solved my issue
    I hope that issue #8654 will be resolved soon as well :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: junitxml

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions