How to use _escape_attr method in avocado

Best Python code snippet using avocado_python

__init__.py

Source:__init__.py Github

copy

Full Screen

...60 return (_strip_invalid_chars(s)61 .replace("&", "&amp;")62 .replace("<", "&lt;")63 .replace("]]>", "]]&gt;"))64def _escape_attr(s):65 return (_strip_invalid_chars(s)66 .replace("&", "&amp;")67 .replace("<", "&lt;")68 .replace("]]>", "]]&gt;")69 .replace('"', "&quot;")70 .replace("\t", "&#x9;")71 .replace("\n", "&#xA;"))72class JUnitXmlResult(unittest.TestResult):73 """A TestResult which outputs JUnit compatible XML."""74 75 def __init__(self, stream):76 """Create a JUnitXmlResult.77 :param stream: A stream to write results to. Note that due to the78 nature of JUnit XML output, nnothing will be written to the stream79 until stopTestRun() is called.80 """81 self.__super = super(JUnitXmlResult, self)82 self.__super.__init__()83 # GZ 2010-09-03: We have a problem if passed a text stream in Python 384 # as really we want to write raw UTF-8 to ensure that85 # the encoding is not mangled later86 self._stream = stream87 self._results = []88 self._set_time = None89 self._test_start = None90 self._run_start = None91 self._tz_info = None92 def startTestRun(self):93 """Start a test run."""94 self._run_start = self._now()95 def _get_tzinfo(self):96 if self._tz_info is None:97 self._tz_info = LocalTimezone()98 return self._tz_info99 def _now(self):100 if self._set_time is not None:101 return self._set_time102 else:103 return datetime.datetime.now(self._get_tzinfo())104 def time(self, a_datetime):105 self._set_time = a_datetime106 if (self._run_start is not None and107 self._run_start > a_datetime):108 self._run_start = a_datetime109 def startTest(self, test):110 self.__super.startTest(test)111 self._test_start = self._now()112 def _duration(self, from_datetime):113 try:114 delta = self._now() - from_datetime115 except TypeError:116 n = self._now()117 delta = datetime.timedelta(-1)118 seconds = delta.days * 3600*24 + delta.seconds119 return seconds + 0.000001 * delta.microseconds120 def _test_case_string(self, test):121 duration = self._duration(self._test_start)122 test_id = test.id()123 # Split on the last dot not inside a parameter124 class_end = test_id.rfind(".", 0, test_id.find("("))125 if class_end == -1:126 classname, name = "", test_id127 else:128 classname, name = test_id[:class_end], test_id[class_end+1:]129 self._results.append('<testcase classname="%s" name="%s" '130 'time="%0.3f"' % (_escape_attr(classname), _escape_attr(name), duration))131 def stopTestRun(self):132 """Stop a test run.133 This allows JUnitXmlResult to output the XML representation of the test134 run.135 """136 duration = self._duration(self._run_start)137 self._stream.write('<testsuite errors="%d" failures="%d" name="" '138 'skips="%d" tests="%d" time="%0.3f">\n' % (len(self.errors),139 len(self.failures) + len(getattr(self, "unexpectedSuccesses", ())),140 len(self.skipped), self.testsRun, duration))141 self._stream.write(''.join(self._results))142 self._stream.write('</testsuite>\n')143 def addError(self, test, error):144 self.__super.addError(test, error)145 self._test_case_string(test)146 self._results.append('>\n')147 self._results.append('<error type="%s">%s</error>\n</testcase>\n' % (148 _escape_attr(_error_name(error[0])),149 _escape_content(self._exc_info_to_string(error, test))))150 def addFailure(self, test, error):151 self.__super.addFailure(test, error)152 self._test_case_string(test)153 self._results.append('>\n')154 self._results.append('<failure type="%s">%s</failure>\n</testcase>\n' %155 (_escape_attr(_error_name(error[0])),156 _escape_content(self._exc_info_to_string(error, test))))157 def addSuccess(self, test):158 self.__super.addSuccess(test)159 self._test_case_string(test)160 self._results.append('/>\n')161 def addSkip(self, test, reason):162 try:163 self.__super.addSkip(test, reason)164 except AttributeError:165 # Python < 2.7|3.1166 pass167 self._test_case_string(test)168 self._results.append('>\n')169 self._results.append('<skipped message="%s"></skipped>\n</testcase>\n'% _escape_attr(reason))170 def addUnexpectedSuccess(self, test):171 try:172 self.__super.addUnexpectedSuccess(test)173 except AttributeError:174 # Python < 2.7|3.1175 pass176 self._test_case_string(test)177 self._results.append('>\n')178 self._results.append('<failure type="unittest.case._UnexpectedSuccess"/>\n</testcase>\n')179 def addExpectedFailure(self, test, error):180 try:181 self.__super.addExpectedFailure(test, error)182 except AttributeError:183 # Python < 2.7|3.1...

Full Screen

Full Screen

xunit.py

Source:xunit.py Github

copy

Full Screen

...25 description = 'XUnit result support'26 UNKNOWN = '<unknown>'27 PRINTABLE = string.ascii_letters + string.digits + string.punctuation + \28 '\n\r '29 def _escape_attr(self, attrib):30 attrib = ''.join(_ if _ in self.PRINTABLE else "\\x%02x" % ord(_)31 for _ in str(attrib))32 return attrib33 def _escape_cdata(self, cdata):34 cdata = ''.join(_ if _ in self.PRINTABLE else "\\x%02x" % ord(_)35 for _ in str(cdata))36 return cdata.replace(']]>', ']]>]]&gt;<![CDATA[')37 def _get_attr(self, container, attrib):38 return self._escape_attr(container.get(attrib, self.UNKNOWN))39 def _create_testcase_element(self, document, state):40 testcase = document.createElement('testcase')41 testcase.setAttribute('classname',42 self._get_attr(state, 'class_name'))43 testcase.setAttribute('name', self._get_attr(state, 'name'))44 testcase.setAttribute('time', self._get_attr(state, 'time_elapsed'))45 return testcase46 def _create_failure_or_error(self, document, test, element_type):47 element = Element(element_type)48 element.setAttribute('type', self._get_attr(test, 'fail_class'))49 element.setAttribute('message', self._get_attr(test, 'fail_reason'))50 traceback_content = self._escape_cdata(51 test.get('traceback', self.UNKNOWN))52 traceback = document.createCDATASection(traceback_content)53 element.appendChild(traceback)54 system_out = Element('system-out')55 system_out_cdata_content = self._escape_cdata(56 test.get('text_output', self.UNKNOWN))57 system_out_cdata = document.createCDATASection(58 system_out_cdata_content)59 system_out.appendChild(system_out_cdata)60 element.appendChild(system_out)61 return element62 def _render(self, result):63 document = Document()64 testsuite = document.createElement('testsuite')65 testsuite.setAttribute('name', 'avocado')66 testsuite.setAttribute('tests', self._escape_attr(result.tests_total))67 testsuite.setAttribute('errors', self._escape_attr(68 result.errors + result.interrupted))69 testsuite.setAttribute('failures', self._escape_attr(result.failed))70 testsuite.setAttribute('skipped', self._escape_attr(result.skipped))71 testsuite.setAttribute('time',72 self._escape_attr(result.tests_total_time))73 testsuite.setAttribute('timestamp',74 self._escape_attr(datetime.datetime.now()))75 document.appendChild(testsuite)76 for test in result.tests:77 testcase = self._create_testcase_element(document, test)78 status = test.get('status', 'ERROR')79 if status in ('PASS', 'WARN'):80 pass81 elif status == 'SKIP':82 testcase.appendChild(Element('skipped'))83 elif status == 'FAIL':84 element = self._create_failure_or_error(document, test,85 'failure')86 testcase.appendChild(element)87 else:88 element = self._create_failure_or_error(document, test,...

Full Screen

Full Screen

_graph_viz_lines_via_big_index.py

Source:_graph_viz_lines_via_big_index.py Github

copy

Full Screen

...3 yield "digraph g {\n"4 assert bi.node_names_seen5 ch_indent = '' # child indent. empty string for flush-left6 if app_label:7 label = _escape_attr(app_label)8 yield f'label="\\n{label}"\n'9 yield '/* Nodes */\n'10 for k in bi.node_names_seen.keys():11 for line in _lines_via_node(bi.nodes[k], ch_indent):12 yield line13 yield '/* Transitions */\n'14 for trans in bi.transitions:15 for line in _lines_via_transition(trans, ch_indent):16 yield line17 yield "}\n"18def _lines_via_transition(trans, indent):19 from_key, to_key = trans.initial_state, trans.result_state20 _assert_key(from_key)21 _assert_key(to_key)22 label = _escape_attr(_label_via_natural_key(trans.transition_type))23 yield f'{indent}{from_key}->{to_key}[label="{label}"]\n'24def _lines_via_node(node, indent):25 _assert_key(node.natural_key)26 label = _escape_attr(_label_via_natural_key(node.natural_key))27 yield f'{indent}{node.natural_key}[label="{label}"]\n'28def _escape_attr(s):29 assert '"' not in s # ..30 return s31def _label_via_natural_key(nk):32 return nk.replace('_', ' ')33def _assert_key(key):34 assert re.match(r'^[a-zA-Z_]+$', key)...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful