python junit xml output module / JunitOutput.py

#!/usr/bin/env python
import xml.etree.ElementTree as ET
import xml.dom.minidom

class JunitXml(object):
    """ A class which is designed to create a junit test xml file.
        Note: currently this class is designed to return the junit xml file
        in a string format (through the dump method).
    """
    def __init__(self, testsuit_name, test_cases, total_tests=None, total_failures=None):
        self.testsuit_name = testsuit_name
        self.test_cases = test_cases
        self.failing_test_cases = self._get_failing_test_cases()

        self.total_tests = total_tests
        self.total_failures = total_failures

        if total_tests is None:
            self.total_tests = len(self.test_cases)
        if total_failures is None:
            self.total_failures = len(self.failing_test_cases)

        self.root = ET.Element("testsuite", {'name' : str(self.testsuit_name),
            "failures": str(self.total_failures), "tests" : str(self.total_tests)})
        self.build_junit_xml()

    def _get_failing_test_cases(self):
        return set([case for case in self.test_cases if case.is_failure() ])

    def build_junit_xml(self):
        """ create the xml tree from the given testsuite name and testcase """
        for case in self.test_cases:
            test_case_element = ET.SubElement(self.root, "testcase", \
                {'name' : str(case.name)} )
            if case.is_failure():
                failure_element = ET.Element("failure")
                failure_element.text = case.contents
                test_case_element.append(failure_element)

    def dump(self, pretty=True):
        """ returns a string representation of the junit xml tree. """
        out = ET.tostring(self.root)
        if pretty:
            dom = xml.dom.minidom.parseString(out)
            out = dom.toprettyxml()
        return out

class TestCase(object):
    """ A junit test case representation class.
        The JunitXml accepts a set of these and uses them to create
        the junit test xml tree
    """
    def __init__(self, name, contents, test_type=""):
        self.name = name
        self.contents = contents
        self.test_type = test_type

    def is_failure(self):
        """ returns true if this test case is a 'failure' type """
        return self.test_type == "failure"

def main():
    """ a short example of how to use this module """
    test_cases = []
    for i in range(0, 5):
        type_c = ""
        if i % 2 == 0:
            type_c = "failure"
        test_cases.append(TestCase(i, str(i) + "contents", type_c) )

    junit_xml = JunitXml("demo test example", test_cases)
    print junit_xml.dump()


if __name__=="__main__":
    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.