I'm trying to generate the below XML file using the data populated in a list:
<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="65" failures="0" disabled="0" errors="0" name="AllTests">
<testsuite name="Tests" tests="65" failures="0" disabled="0" skipped="0" errors="0">
<testcase name="TestInit" file="" status="run" result="completed" />
<testcase name="TestAlways" file="" status="run" result="completed" />
</testsuite>
...
</testsuites>
main.py:
import xml.etree.cElementTree as ET
class TestCase:
def __init__(self, name, result):
self.name = name
self.result = result
def __str__(self):
return f"Test Case: {self.name}, Status: {self.result}"
class TestSuite:
def __init__(self, name):
self.name = name
self.test_case_list = []
def __str__(self):
return f"Test Suite: {self.name}"
def main():
print("Hello from html-test-report-generator!")
test_suite_list = generate_html_report()
for test_suite in test_suite_list:
print(test_suite)
for test_case in test_suite.test_case_list:
print(test_case)
print()
generate_xml(test_suite_list)
def generate_html_report():
# generates a list of TestSuite objects and returns the list back to the caller
def generate_xml(test_suite_list):
testsuites = ET.Element("testsuites", tests=len(test_suite_list), failures="0", disabled="0", errors="0", name="AllTests")
for test_suite in test_suite_list:
testsuite = ET.SubElement(testsuites, "testsuite", name=test_suite.name, tests=len(test_suite.test_case_list), failures="0", disabled="0", skipped="0", errors="0")
for test_case in test_suite.test_case_list:
ET.SubElement(testsuite, "testcase", name=test_case.name, file="", line="", status="run", result=test_case.result)
tree = ET.ElementTree(testsuites)
tree.write("filename.xml")
Error:
Traceback (most recent call last):
File "/usr/lib/python3.12/xml/etree/ElementTree.py", line 743, in _get_writer
write = file_or_filename.write
^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'write'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/harry/html_test_report_generator/main.py", line 94, in <module>
main()
File "/home/harry/html_test_report_generator/main.py", line 28, in main
generate_xml(test_suite_list)
File "/home/harry/html_test_report_generator/main.py", line 37, in generate_xml
tree.write("filename.xml")
File "/usr/lib/python3.12/xml/etree/ElementTree.py", line 729, in write
serialize(write, self._root, qnames, namespaces,
File "/usr/lib/python3.12/xml/etree/ElementTree.py", line 885, in _serialize_xml
v = _escape_attrib(v)
^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/xml/etree/ElementTree.py", line 1050, in _escape_attrib
_raise_serialization_error(text)
File "/usr/lib/python3.12/xml/etree/ElementTree.py", line 1004, in _raise_serialization_error
raise TypeError(
TypeError: cannot serialize 23 (type int)
print()(andprint(type(...)),print(len(...)), etc.) to see which part of code is executed and what you really have in variables. It is called"print debugging"and it helps to see what code is really doing.minimal working codewith example data - so we could run it and see problem,'str' object has no attribute 'write'(maybe it needs opened file instead of filename. (2)cannot serialize 23 (type int)(maybe you have to convert all integer values into strings).testsuites.dump()after eachElement()orSubElement()call, to ensure the XML elements look correct at each point.