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

**URL:** <https://forum.robotframework.org/t/reading-xml-source-failed-incompatible-child-element-statistics-for-kw/6760>\
**Category:** Robot Framework\
**Created:** [1 February 2024 11:05 UTC](https://forum.robotframework.org/t/reading-xml-source-failed-incompatible-child-element-statistics-for-kw/6760 "2024-02-01T11:05:41Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barb](https://avatars.discourse-cdn.com/v4/letter/b/58956e/32.png) [@Barb](https://forum.robotframework.org/u/Barb)\
**Post date:** [1 February 2024 11:05 UTC](https://forum.robotframework.org/t/reading-xml-source-failed-incompatible-child-element-statistics-for-kw/6760/1 "2024-02-01T11:05:41Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Many](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/many/32/658_2.png) [@Many](https://forum.robotframework.org/u/Many)\
**Post date:** [1 February 2024 11:38 UTC](https://forum.robotframework.org/t/reading-xml-source-failed-incompatible-child-element-statistics-for-kw/6760/2 "2024-02-01T11:38:40Z")

</div>

Hi Barbara,

I saw a similar issue raised here

> <https://github.com/robotframework/robotframework/issues/5026>
>
> robotframework: 6.1.1
> python: 3.7.9
> 
> hello, when I run robot with multi threa…d has error: \[ERROR \] Reading XML source 'D:\\result\\output.xml' failed: Incompatible child element 'statistics' for 'kw'.
> I start 4 threads to execute run(robot\_file\_path, outputdir=result\_path) at the same time, i get a error message: \[ERROR \] Reading XML source 'D:\\result\\output.xml' failed: Incompatible child element 'statistics' for 'kw'.
> 
> !\[Uploading DD97FFE0-905C-417e-8E06-F68FBCB0C133.png…\](output.xml)
> 
> below is my code script:
> 
> import threading
> 
> from robot import run
> 
> 
> class MultiThreadings(object):
> 
> def \_\_init\_\_(self):
> self.thread = threading
> 
> def run(self, robot\_file\_path, result\_path):
> run(robot\_file\_path, outputdir=result\_path)
> threading\_id\_str = "thread" + str(self.thread.get\_ident())
> 
> def multi(self, robot\_file\_path, result\_path):
> threads = \[\]
> for i in range(0, 4):
> t = self.thread.Thread(target=self.run, args=(robot\_file\_path, result\_path + "-" + str(i)))
> threads.append(t)
> for t in threads:
> t.start()
> 
> 
> robot\_file\_path = "./01.robot"
> result\_path = "./result"
> multi = MultiThreadings()
> multi.multi(robot\_file\_path, result\_path)
> print("ok")

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?

---

<div class="post-metadata">

**Author:** ![Many](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/many/32/658_2.png) [@Many](https://forum.robotframework.org/u/Many)\
**Post date:** [1 February 2024 11:39 UTC](https://forum.robotframework.org/t/reading-xml-source-failed-incompatible-child-element-statistics-for-kw/6760/3 "2024-02-01T11:39:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Many](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/many/32/658_2.png) [@Many](https://forum.robotframework.org/u/Many)\
**Post date:** [1 February 2024 11:47 UTC](https://forum.robotframework.org/t/reading-xml-source-failed-incompatible-child-element-statistics-for-kw/6760/4 "2024-02-01T11:47:11Z")

</div>

> [@Barb](#):
>
> @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"
> 
> ```python
> 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
> 
> ```

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?

---

<div class="post-metadata">

**Author:** ![Barb](https://avatars.discourse-cdn.com/v4/letter/b/58956e/32.png) [@Barb](https://forum.robotframework.org/u/Barb)\
**Post date:** [1 February 2024 12:16 UTC](https://forum.robotframework.org/t/reading-xml-source-failed-incompatible-child-element-statistics-for-kw/6760/5 "2024-02-01T12:16:05Z")

</div>

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](https://robotframework.org)  
Author: Pekka Klärck  
Author-email: [peke@eliga.fi](mailto:peke@eliga.fi)  
License: Apache License 2.0

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

---

<div class="post-metadata">

**Author:** ![Many](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/many/32/658_2.png) [@Many](https://forum.robotframework.org/u/Many)\
**Post date:** [1 February 2024 12:30 UTC](https://forum.robotframework.org/t/reading-xml-source-failed-incompatible-child-element-statistics-for-kw/6760/6 "2024-02-01T12:30:21Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![Barb](https://avatars.discourse-cdn.com/v4/letter/b/58956e/32.png) [@Barb](https://forum.robotframework.org/u/Barb)\
**Post date:** [1 February 2024 12:54 UTC](https://forum.robotframework.org/t/reading-xml-source-failed-incompatible-child-element-statistics-for-kw/6760/7 "2024-02-01T12:54:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Many](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/many/32/658_2.png) [@Many](https://forum.robotframework.org/u/Many)\
**Post date:** [1 February 2024 13:07 UTC](https://forum.robotframework.org/t/reading-xml-source-failed-incompatible-child-element-statistics-for-kw/6760/8 "2024-02-01T13:07:41Z")

</div>

> [@Barb](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![rasjani](https://avatars.discourse-cdn.com/v4/letter/r/858c86/32.png) [@rasjani](https://forum.robotframework.org/u/rasjani)\
**Post date:** [1 February 2024 13:08 UTC](https://forum.robotframework.org/t/reading-xml-source-failed-incompatible-child-element-statistics-for-kw/6760/9 "2024-02-01T13:08:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Barb](https://avatars.discourse-cdn.com/v4/letter/b/58956e/32.png) [@Barb](https://forum.robotframework.org/u/Barb)\
**Post date:** [1 February 2024 16:58 UTC](https://forum.robotframework.org/t/reading-xml-source-failed-incompatible-child-element-statistics-for-kw/6760/10 "2024-02-01T16:58:50Z")

</div>

Thank you very much

---

<div class="post-metadata">

**Author:** ![Barb](https://avatars.discourse-cdn.com/v4/letter/b/58956e/32.png) [@Barb](https://forum.robotframework.org/u/Barb)\
**Post date:** [1 February 2024 17:07 UTC](https://forum.robotframework.org/t/reading-xml-source-failed-incompatible-child-element-statistics-for-kw/6760/11 "2024-02-01T17:07:39Z")

</div>

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
