Best Python code snippet using unittest-xml-reporting_python
builder.py
Source:builder.py
...18_nontext_sub = re.compile(19 six.u(r'[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD%s]') % _char_tail,20 re.U21).sub22def replace_nontext(text, replacement=six.u('\uFFFD')):23 return _nontext_sub(replacement, text)24class TestXMLContext(object):25 """A XML report file have a distinct hierarchy. The outermost element is26 'testsuites', which contains one or more 'testsuite' elements. The role of27 these elements is to give the proper context to 'testcase' elements.28 These contexts have a few things in common: they all have some sort of29 counters (i.e. how many testcases are inside that context, how many failed,30 and so on), they all have a 'time' attribute indicating how long it took31 for their testcases to run, etc.32 The purpose of this class is to abstract the job of composing this33 hierarchy while keeping track of counters and how long it took for a34 context to be processed.35 """36 # Allowed keys for self.counters37 _allowed_counters = ('tests', 'errors', 'failures', 'skipped',)38 def __init__(self, xml_doc, parent_context=None):39 """Creates a new instance of a root or nested context (depending whether40 `parent_context` is provided or not).41 """42 self.xml_doc = xml_doc43 self.parent = parent_context44 self._start_time = self._stop_time = 045 self.counters = {}46 def element_tag(self):47 """Returns the name of the tag represented by this context.48 """49 return self.element.tagName50 def begin(self, tag, name):51 """Begins the creation of this context in the XML document by creating52 an empty tag <tag name='param'>.53 """54 self.element = self.xml_doc.createElement(tag)55 self.element.setAttribute('name', replace_nontext(name))56 self._start_time = time.time()57 def end(self):58 """Closes this context (started with a call to `begin`) and creates an59 attribute for each counter and another for the elapsed time.60 """61 self._stop_time = time.time()62 self.element.setAttribute('time', self.elapsed_time())63 self._set_result_counters()64 return self.element65 def _set_result_counters(self):66 """Sets an attribute in this context's tag for each counter considering67 what's valid for each tag name.68 """69 tag = self.element_tag()70 for counter_name in TestXMLContext._allowed_counters:71 valid_counter_for_element = False72 if counter_name == 'skipped':73 valid_counter_for_element = (74 tag == 'testsuite'75 )76 else:77 valid_counter_for_element = (78 tag in ('testsuites', 'testsuite')79 )80 if valid_counter_for_element:81 value = six.text_type(82 self.counters.get(counter_name, 0)83 )84 self.element.setAttribute(counter_name, value)85 def increment_counter(self, counter_name):86 """Increments a counter named by `counter_name`, which can be any one87 defined in `_allowed_counters`.88 """89 if counter_name in TestXMLContext._allowed_counters:90 self.counters[counter_name] = \91 self.counters.get(counter_name, 0) + 192 def elapsed_time(self):93 """Returns the time the context took to run between the calls to94 `begin()` and `end()`, in seconds.95 """96 return format(self._stop_time - self._start_time, '.3f')97class TestXMLBuilder(object):98 """This class encapsulates most rules needed to create a XML test report99 behind a simple interface.100 """101 def __init__(self):102 """Creates a new instance.103 """104 self._xml_doc = Document()105 self._current_context = None106 def current_context(self):107 """Returns the current context.108 """109 return self._current_context110 def begin_context(self, tag, name):111 """Begins a new context in the XML test report, which usually is defined112 by one on the tags 'testsuites', 'testsuite', or 'testcase'.113 """114 context = TestXMLContext(self._xml_doc, self._current_context)115 context.begin(tag, name)116 self._current_context = context117 def context_tag(self):118 """Returns the tag represented by the current context.119 """120 return self._current_context.element_tag()121 def _create_cdata_section(self, content):122 """Returns a new CDATA section containing the string defined in123 `content`.124 """125 filtered_content = replace_nontext(content)126 return self._xml_doc.createCDATASection(filtered_content)127 def append_cdata_section(self, tag, content):128 """Appends a tag in the format <tag>CDATA</tag> into the tag represented129 by the current context. Returns the created tag.130 """131 element = self._xml_doc.createElement(tag)132 pos = content.find(']]>')133 while pos >= 0:134 tmp = content[0:pos+2]135 element.appendChild(self._create_cdata_section(tmp))136 content = content[pos+2:]137 pos = content.find(']]>')138 element.appendChild(self._create_cdata_section(content))139 self._append_child(element)140 return element141 def append(self, tag, content, **kwargs):142 """Apends a tag in the format <tag attr='val' attr2='val2'>CDATA</tag>143 into the tag represented by the current context. Returns the created144 tag.145 """146 element = self._xml_doc.createElement(tag)147 for key, value in kwargs.items():148 filtered_value = replace_nontext(six.text_type(value))149 element.setAttribute(key, filtered_value)150 if content:151 element.appendChild(self._create_cdata_section(content))152 self._append_child(element)153 return element154 def _append_child(self, element):155 """Appends a tag object represented by `element` into the tag156 represented by the current context.157 """158 if self._current_context:159 self._current_context.element.appendChild(element)160 else:161 self._xml_doc.appendChild(element)162 def increment_counter(self, counter_name):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!