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

Issue when parsing the xml report with current pytest 5.1.1 #5791

Closed
kobenguyent opened this issue Aug 27, 2019 · 1 comment
Closed

Issue when parsing the xml report with current pytest 5.1.1 #5791

kobenguyent opened this issue Aug 27, 2019 · 1 comment
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@kobenguyent
Copy link

So my scenario would be running the tests with pytest, then generating the xml report, after that collecting the info from xml report to publish results to testrail.

xml report is something like this:

<?xml version="1.0" encoding="utf-8"?>
<testsuite errors="0" failures="0" name="pytest" skipped="0" tests="4" time="55.656">
    <testcase classname="tests.admin.test_massupload.TestMassUpload" file="tests/admin/test_massupload.py" line="98" name="test_upload_price_locals_massupload_de" time="51.519">
        <properties>
            <property name="testrail" value="C53"/>
        </properties>
        <system-out>
        </system-out>
    </testcase>
    <testcase classname="tests.admin.test_massupload.TestMassUpload" file="tests/admin/test_massupload.py" line="76" name="test_upload_price_locals_massupload_us" time="54.474">
        <properties>
            <property name="testrail" value="C53"/>
        </properties>
        <system-out>
        </system-out>
    </testcase>
    <testcase classname="tests.admin.test_massupload.TestMassUpload" file="tests/admin/test_massupload.py" line="54" name="test_upload_price_locals_massupload_uk" time="49.676">
        <properties>
            <property name="testrail" value="C53"/>
        </properties>
        <system-out>
        </system-out>
    </testcase>
    <testcase classname="tests.admin.test_massupload.TestMassUpload" file="tests/admin/test_massupload.py" line="38" name="test_create_product_global_massupload" time="50.935">
        <properties>
            <property name="testrail" value="C23"/>
        </properties>
        <system-out>
        </system-out>
    </testcase>
</testsuite>

Here is a function to collect info:

def collect_test_cases(ctx, report):
    """
    Collect test cases with TestRail ID
    :param ctx: CLI Context object
    :param report: path to report file
    :return (list):
    """
    test_cases = []
    try:
        tree = ET.parse(report)
        root = tree.getroot()
    except Exception as exc:  # pylint: disable=broad-except
        ctx.vlog(exc)
        exit(1)

    for case in root:
        count = len(case.findall('failure'))
        if count == 1:
            if len(case.findall('properties/property')) != 0:
                tr_ids = []
                for tr_id in case.findall('properties/property'):
                    tr_ids.append(int(tr_id.attrib['value'].split('C')[-1]))
                test_cases.append({'name': case.attrib['name'], 'status': TESTRAIL_TEST_STATUS['failed'], 'log': case.findall(
                    'failure')[0].text, 'testrail_id': tr_ids})
            else:
                test_cases.append(
                    {'name': case.attrib['name'], 'status': TESTRAIL_TEST_STATUS['failed'], 'log': case.findall('failure')[0].text})
        else:
            if len(case.findall('properties/property')) != 0:
                tr_ids = []
                for tr_id in case.findall('properties/property'):
                    tr_ids.append(int(tr_id.attrib['value'].split('C')[-1]))
                test_cases.append(
                    {'name': case.attrib['name'], 'status': TESTRAIL_TEST_STATUS['passed'], 'testrail_id': tr_ids})
            else:
                test_cases.append(
                    {'name': case.attrib['name'], 'status': TESTRAIL_TEST_STATUS['passed']})

    return test_cases

With pytest 5.0.1, the collection is proper:

[{'name': 'test_upload_price_locals_massupload_de', 'status': 1, 'testrail_id': [53]}, {'name': 'test_upload_price_locals_massupload_us', 'status': 1, 'testrail_id': [53]}, {'name': 'test_upload_price_locals_massupload_uk', 'status': 1, 'testrail_id': [53]}, {'name': 'test_create_product_global_massupload', 'status': 1, 'testrail_id': [23]}]

However, with pytest 5.1.1, I got this:

[{'name': 'pytest', 'status': 1}]

Maybe something has changed that impacts this or probably I miss something to do.

@nicoddemus
Copy link
Member

Hi @peterngtr,

It is probably related to:

#5477: The XML file produced by --junitxml now correctly contain a root element.

Previously the xml file would have a <testsuite> tag, but the xunit standard says it should contain a <testsuites> tag, with 1 or more <testsuite> tags inside it.

@nicoddemus nicoddemus added the type: question general question, might be closed after 2 weeks of inactivity label Aug 27, 2019
@Zac-HD Zac-HD closed this as completed Sep 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

3 participants