0

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)
5
  • Maybe first use print() (and print(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. Commented Aug 10 at 12:26
  • better create minimal working code with example data - so we could run it and see problem, Commented Aug 10 at 12:27
  • 1
    it seems you have two problems in code - (1) '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). Commented Aug 10 at 12:29
  • The error indicates the Write method is failing. The Write is trying to serialize the object tree and failing. So tree is not valid. You need to see what tree contains to isolate the issue. Commented Aug 10 at 12:32
  • For debugging, try running testsuites.dump() after each Element() or SubElement() call, to ensure the XML elements look correct at each point. Commented Aug 10 at 12:42

1 Answer 1

0

It seems problem makes tests=len(...).
It needs to convert it to string tests=str(len(...)).

This minimal code generates the same error TypeError: cannot serialize 23 (type int)

import xml.etree.cElementTree as ET

testsuites = ET.Element("testsuites", tests=23)
tree = ET.ElementTree(testsuites)
tree.write("filename.xml")

But this code with str() works

import xml.etree.cElementTree as ET

testsuites = ET.Element("testsuites", tests=str(23))
tree = ET.ElementTree(testsuites)
tree.write("filename.xml")

You have to fix tests=len(...) in two places: in ET.Element() and in ET.SubElement()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.