Reading XML source failed: Incompatible child element 'statistics' for 'kw'

I have a python library that implements robot framework keywords. I’m generating a test suite automatically given a yaml file and I have one last function to run the suite.

@keyword(“Run Robot Tests”)
def run_robot_tests(test_suite):
strong text
test_run_id = f"{datetime.datetime.now().timestamp()}"
output_path = f"{OUTPUT_PATH_PREFIX}{test_run_id}-output.xml"

try:
    result = test_suite.run(output=output_path)
    stats = result.statistics
    if stats.total.failed:
        raise Exception(f"At least one test failed. Output: {output_path}")
except FileNotFoundError:
    pass  # Ignore if the file does not exist

return result

When I execute the code, everything is correct and my tests are running, but in the end I get this error:

  • Reading XML source failed: Incompatible child element ‘statistics’ for ‘kw’.

I already check the robot versions and they seem to be correct.
Any ideias? Thank you in advance!

Hi Barbara,

I saw a similar issue raised here

Could you please check which Robot Framework Version you have installed?
You can just do a pip show robotframework to get the details about the installed package.

However, what I can say:
The message complains about finding an statistics element for a kw (keyword).
That error message makes sense to me, as there should indeed not be an statistics element for a kw . Statistics should only exist on the root suite element.
So something is maybe corrupt in your output.xml file.
Can you check it manually and search for statistics elements in there? And check where they appear?

The file could be corrupted if maybe multiple processes are writing to it at once. Not sure if that’s a possibility in your case.

Question about that function:
Do I see it correctly that is is actually a Robot Framework Keyword (@keyword(“Run Robot Tests”) in Python which then again executed a test_suite which is handed over as an argument ?
And you call that Keyword Run Robot Tests from inside anothter Robot Framework Test Case?

Yes, I call the keywords from another robot framework file. This is the code of that file:

*** Settings ***
Library …/Resources/TestSuiteGenerator.py
Library BuiltIn
Library OperatingSystem

*** Test Cases ***
Generate And Run Test Cases
${file_path}= Set variable ${CURDIR}${/}tests_for_api_name.yml
${test_cases}= Read Yaml Test File ${file_path}
${suite}= Generate Robot Tests ${test_cases} ${file_path}
Run Robot Tests ${suite}

So basically in my python file I have 4 keywords: “Read Yaml Test File”, “Generate Robot Tests”, “Run Robot Tests” and “Request Api Call” which I only call from the “Generate Robot Tests” keyword.

I believe the problem is in the “Generate Robot Tests” keyword, which I showed in the post.

In my case I don’t have multiple processes writing the file.
I checked the version that i have installed and it is this one:

Name: robotframework
Version: 7.0
Summary: Generic automation framework for acceptance testing and robotic process automation (RPA)
Home-page: https://robotframework.org
Author: Pekka Klärck
Author-email: peke@eliga.fi
License: Apache License 2.0

but i’ll check the post you sent me for more information.

In my company we have one project with similar framework in which we generate test cases dynamically from a .yaml file and afterwards run them in Robot Framework.
However, we trigger that whole process for test case generation and execution from a python script (and not from inside a Robot Framework Test Suite).
So only the actual test cases will later be executed in Robot Framework (by using its api, similar like you do in the Run Robot Tests Keyword).

As your Keywords Read Yaml Test File , Generate Robot Tests and Run Robot Tests are also written in Python, I recommend to try running them from a Python script instead and not from inside Robot Framework. Maybe this would already solve some weird problems about the output.xml

I did it using robot framework because I believe I didn’t have any more options due to the framework we use. But I’ll search a little bit more to see if I can change that. It would simplify a lot if I could only use Python.
Thanks for you time

Don’t get me wrong, I strongly recommend to stay with Robot Framework as your test execution framework.
I would just slightly change how you “call” the process for the Test Case Generation and the Start of the Execution.
I will try to share an example later what I mean

1 Like

This particular error message typically happens when multiple robot instances write to same output.xml file and it ends up corrupted.

Since i don’t quite get the whole picture of what and more importantly how your stuff is supposed to work, my assumption is that:

  1. You are running a robot.
  2. Your robot instance dynamically creates a test suite and runs it while the execution path of current robot instance is executing a keyword.
  3. While keyword is being executed, new suite starts from your code and that “corrupts” the output.

So, it feels like your approach on how to want to archive dynamic execution of testsuites is not working.

And disclaimer; this is pure speculation.

1 Like

Thank you very much

I believe that is exactly what is happening.
Basically I am running this is a suite:

*** Test Cases ***
Generate And Run Test Cases
${file_path}= Set variable ${CURDIR}${/}tests_for_api_name.yml
${test_cases}= Read Yaml Test File ${file_path}
${suite}= Generate Robot Tests ${test_cases} ${file_path}
Run Robot Tests ${suite}

and then when I call Generate Robot Test keyword, I am creating another suite with some other tests to execute. That’s why the output gets “corrupted”.
I think I am going to try to change my approach. Thank you