Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get testcases properties? #116

Open
nicola-lunghi opened this issue Oct 19, 2023 · 9 comments
Open

How to get testcases properties? #116

nicola-lunghi opened this issue Oct 19, 2023 · 9 comments

Comments

@nicola-lunghi
Copy link

I have the following xml

<testsuite name="LatencyTestSuite/LatencyTest" tests="8" failures="8" disabled="0" skipped="0"
  errors="0" time="20.006" timestamp="2023-10-19T09:02:39.209">
  <testcase name="LatencyTest/LAT_64" value_param="64" file="test_latency.cpp" line="125"
    status="run" result="completed" time="2.5" timestamp="2023-10-19T09:02:39.209"
    classname="LatencyTestSuite/LatencyTest">
    <failure
      message="test_latency.cpp:146&#x0A;Expected equality of these values:&#x0A;  test_result.EdmaRttValid&#x0A;    Which is: 0&#x0A;  1"
      type=""><![CDATA[test_latency.cpp:146
Expected equality of these values:
  test_result.EdmaRttValid
    Which is: 0
  1]]></failure>
    <failure
      message="test_latency.cpp:147&#x0A;Expected: (test_result.EdmaRtt01) &gt; (0), actual: 0 vs 0"
      type=""><![CDATA[test_latency.cpp:147
Expected: (test_result.EdmaRtt01) > (0), actual: 0 vs 0]]></failure>
    <failure
      message="test_latency.cpp:148&#x0A;Expected: (test_result.EdmaRtt02) &gt; (0), actual: 0 vs 0"
      type=""><![CDATA[test_latency.cpp:148
Expected: (test_result.EdmaRtt02) > (0), actual: 0 vs 0]]></failure>
    <properties>
      <property name="TestType" value="LATENCY_EDMA" />
      <property name="PktSize" value="64" />
      <property name="EdmaRttTimeout" value="0" />
      <property name="EdmaRttValid" value="0" />
      <property name="EdmaRtt01" value="0" />
      <property name="EdmaRtt02" value="0" />
      <property name="dma_error_reg" value="0" />
    </properties>
  </testcase>
</testsuite>

I need to extract the test properties there's a way to do it?
thanks,
Nicola

@EnricoMi
Copy link
Collaborator

@EnricoMi
Copy link
Collaborator

Looks like TestSuite has properties(), which provides access to properties of the suite, but TestCase does not have this. Should be easy to access this to TestCase, or your own derivative of TestCase.

Which flavour of XML dialect are you having there? Which tool generates this?

@nicola-lunghi
Copy link
Author

nicola-lunghi commented Oct 20, 2023 via email

@nicola-lunghi
Copy link
Author

nicola-lunghi commented Oct 20, 2023 via email

@weiwei
Copy link
Owner

weiwei commented Dec 26, 2023

Hi @nicola-lunghi, please check the example at https://github.com/weiwei/junitparser#handling-xml-with-custom-element to see if it helps.

@ZioTino
Copy link

ZioTino commented Jan 16, 2024

Hi,
pytest also allows for custom properties at testcase level.
The output format in xml is the same as the one above.
It would be nice to see the same feature valid for testsuite implemented for testcase.

@gilmatok
Copy link

gilmatok commented Feb 2, 2024

Hello to everyone reading this, I found a solution for this problem.

As you know, you can add custom properties to JUnit XML using Pytest's record_property function. Here's an example from my conftest.py:

@pytest.fixture(autouse=True)
def record_vm_info(request, record_property):
    vm_name = request.config.getoption('--vm-name')
    record_property('vm_name', vm_name)
    vm_ip = request.config.getoption('--vm-ip')
    record_property('vm_ip', vm_ip)

The above fixture will add the vm_name and vm_ip properties to the output JUnit XML file for each test case. You can also use record_testsuite_property to add properties to each test suite.

The output JUnit XML file will contain these properties like so:

<?xml version="1.0" encoding="utf-8"?>
...
<testcase name="test_name">
<properties>
<property name="vm_name" value="example" />
<property name="vm_ip" value="127.0.0.1" />
</properties>
</testcase>

To extract these properties with junitparser, you need to create two custom element classes:

class PropertiesElement(Element):
    _tag = 'properties'

class PropertyElement(Element):
    _tag = 'property'
    name = Attr()
    value = Attr()

Now to iterate over the properties, you need to iterate over the custom elements of the test case using iterchildren like so:

for suite in suite:
    for case in suite:
        properties = case.child(PropertiesElement)
        for property in properties.iterchildren(PropertyElement):
            print(f'Name: {property.name}, Value: {property.value}')

If you're using record_testsuite_property, you simply need to do the same for the test suite level.

@weiwei Despite the workaround, since properties is a feature supported by many platforms, I suggest adding support for this in junitparser.

@gilmatok
Copy link

gilmatok commented Feb 2, 2024

@nicola-lunghi @ZioTino Please see my solution above.

@weiwei
Copy link
Owner

weiwei commented Feb 6, 2024

@gilmatok If it's from pytest then we have a "flavor" for it in https://github.com/weiwei/junitparser/blob/master/junitparser/xunit2.py which is supposed to fully support pytest. I think it'll be fine to add it over there. Care to submit a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants