Best Python code snippet using lemoncheesecake
report.py
Source:report.py
...560 @property561 def successful_tests_percentage(self):562 return (float(self.tests_nb_by_status["passed"]) / self.tests_enabled_nb * 100) if self.tests_enabled_nb else 0563 @property564 def duration_cumulative_description(self):565 description = humanize_duration(self.duration_cumulative)566 if self.duration:567 description += " (parallelization speedup factor is %.1f)" % (float(self.duration_cumulative) / self.duration)568 return description569 @classmethod570 def from_results(cls, results, duration):571 # type: (List[Result], Any[int, None]) -> ReportStats572 stats = cls()573 stats.duration = duration574 stats.duration_cumulative = sum(result.duration or 0 for result in results)575 tests = list(filter(lambda r: isinstance(r, TestResult), results))576 stats.tests_nb = len(tests)577 for test in tests:578 if test.status:...
console.py
Source:console.py
1'''2Created on Mar 19, 20163@author: nicolas4'''5from __future__ import print_function6import sys7from termcolor import colored8import six9from lemoncheesecake.testtree import filter_suites, flatten_suites10from lemoncheesecake.reporting.backend import ReportingBackend, ReportingSession, ReportingSessionBuilderMixin11from lemoncheesecake.reporting.report import ReportStats12from lemoncheesecake.helpers.time import humanize_duration13from lemoncheesecake.helpers.text import ensure_single_line_text14from lemoncheesecake.helpers import terminalsize15from lemoncheesecake.reporting.console import test_status_to_color16class LinePrinter:17 def __init__(self, terminal_width):18 self.terminal_width = terminal_width19 self.prev_len = 020 def print_line(self, line, force_len=None):21 value_len = force_len if force_len else len(line)22 if six.PY2:23 if type(line) is unicode:24 line = line.encode("utf-8")25 if value_len >= self.terminal_width - 1:26 line = line[:self.terminal_width - 5] + "..."27 value_len = len(line)28 sys.stdout.write("\r")29 sys.stdout.write(line)30 if self.prev_len > value_len:31 sys.stdout.write(" " * (self.prev_len - value_len))32 sys.stdout.flush()33 self.prev_len = value_len34 def new_line(self):35 self.prev_len = 036 sys.stdout.write("\n")37 sys.stdout.flush()38 def erase_line(self):39 sys.stdout.write("\r")40 sys.stdout.write(" " * self.prev_len)41 sys.stdout.write("\r")42 self.prev_len = 043def _make_suite_header_line(suite, terminal_width):44 suite_name = suite.path45 max_width = min((terminal_width, 80))46 # -2 corresponds to the two space characters at the left and right of suite path + another character to avoid47 # an extra line after the suite line on Windows terminal having width <= 8048 padding_total = max_width - 3 - len(suite_name) if len(suite_name) <= (max_width - 3) else 049 padding_left = padding_total // 250 padding_right = padding_total // 2 + padding_total % 251 return "=" * padding_left + " " + colored(suite_name, attrs=["bold"]) + " " + "=" * padding_right52def _make_test_status_label(status):53 if status == "passed":54 label = "OK"55 elif status in ("skipped", "disabled", None):56 label = "--"57 else:58 label = "KO"59 return colored(label, test_status_to_color(status), attrs=["bold"])60def _make_test_result_line(name, num, status):61 line = " %s %2s # %s" % (_make_test_status_label(status), num, name)62 raw_line = "%s %2s # %s" % ("OK" if status == "passed" else "KO", num, name)63 return line, len(raw_line)64def _print_summary(stats, parallel=False):65 print()66 print(colored("Statistics", attrs=["bold"]), ":")67 print(" * Duration: %s" % (humanize_duration(stats.duration) if stats.duration is not None else "n/a"))68 if parallel:69 print(" * Cumulative duration: %s" % stats.duration_cumulative_description)70 print(" * Tests: %d" % stats.tests_nb)71 print(" * Successes: %d (%d%%)" % (stats.tests_nb_by_status["passed"], stats.successful_tests_percentage))72 print(" * Failures: %d" % (stats.tests_nb_by_status["failed"]))73 if stats.tests_nb_by_status["skipped"]:74 print(" * Skipped: %d" % (stats.tests_nb_by_status["skipped"]))75 if stats.tests_nb_by_status["disabled"]:76 print(" * Disabled: %d" % (stats.tests_nb_by_status["disabled"]))77 print()78class SequentialConsoleReportingSession(ReportingSession):79 def __init__(self, terminal_width, show_test_full_path, report):80 self.terminal_width = terminal_width81 self.show_test_full_path = show_test_full_path82 self.report = report83 self.lp = LinePrinter(self.terminal_width)84 self.context = None85 self.custom_step_prefix = None86 self.current_suite = None87 def get_test_label(self, test):88 if self.show_test_full_path:89 return test.path90 return test.name91 def ensure_suite_header_is_displayed(self, suite):92 if suite == self.current_suite:93 return94 self.current_suite = suite95 self.current_test_idx = 196 if self.previous_obj:97 sys.stdout.write("\n")98 sys.stdout.write(_make_suite_header_line(suite, self.terminal_width) + "\n")99 self.previous_obj = suite100 def on_test_session_start(self, event):101 self.previous_obj = None102 def on_suite_setup_start(self, event):103 self.ensure_suite_header_is_displayed(event.suite)104 self.step_prefix = " => setup suite: "105 self.lp.print_line(self.step_prefix + "...")106 def on_suite_teardown_start(self, event):107 self.step_prefix = " => teardown suite: "108 self.lp.print_line(self.step_prefix + "...")109 def on_test_session_setup_start(self, event):110 self.step_prefix = " => setup test session: "111 self.lp.print_line(self.step_prefix + "...")112 def on_test_session_teardown_start(self, event):113 self.step_prefix = " => teardown test session: "114 self.lp.print_line(self.step_prefix + "...")115 def on_suite_setup_end(self, event):116 self.lp.erase_line()117 self.custom_step_prefix = None118 on_suite_teardown_end = on_suite_setup_end119 def on_test_session_setup_end(self, event):120 self.lp.erase_line()121 self.custom_step_prefix = None122 on_test_session_teardown_end = on_test_session_setup_end123 def on_test_start(self, event):124 self.ensure_suite_header_is_displayed(event.test.parent_suite)125 self.step_prefix = " -- %2s # %s" % (self.current_test_idx, self.get_test_label(event.test))126 self.lp.print_line(self.step_prefix + "...")127 self.previous_obj = event.test128 def on_test_end(self, event):129 test_data = self.report.get_test(event.test)130 line, raw_line_len = _make_test_result_line(131 self.get_test_label(event.test), self.current_test_idx, test_data.status132 )133 self.lp.print_line(line, force_len=raw_line_len)134 self.lp.new_line()135 self.current_test_idx += 1136 def _bypass_test(self, test, status):137 self.ensure_suite_header_is_displayed(test.parent_suite)138 line = " %s %2s # %s" % (_make_test_status_label(status), self.current_test_idx, self.get_test_label(test))139 raw_line = "%s %2s # %s" % ("KO", self.current_test_idx, self.get_test_label(test))140 self.lp.print_line(line, force_len=len(raw_line))141 self.lp.new_line()142 self.current_test_idx += 1143 def on_test_skipped(self, event):144 self._bypass_test(event.test, "skipped")145 def on_test_disabled(self, event):146 self._bypass_test(event.test, "disabled")147 def on_step_start(self, event):148 self.lp.print_line("%s (%s...)" % (self.step_prefix, ensure_single_line_text(event.step_description)))149 def on_test_session_end(self, event):150 _print_summary(ReportStats.from_report(self.report), self.report.parallelized)151class ParallelConsoleReportingSession(ReportingSession):152 def __init__(self, terminal_width, report):153 self.terminal_width = terminal_width154 self.report = report155 self.lp = LinePrinter(self.terminal_width)156 self.current_test_idx = 1157 def on_test_end(self, event):158 test_data = self.report.get_test(event.test)159 line, _ = _make_test_result_line(160 event.test.path, self.current_test_idx, test_data.status161 )162 print(line)163 self.current_test_idx += 1164 def _bypass_test(self, test, status):165 line = " %s %2s # %s" % (_make_test_status_label(status), self.current_test_idx, test.path)166 print(line)167 self.current_test_idx += 1168 def on_test_skipped(self, event):169 self._bypass_test(event.test, "skipped")170 def on_test_disabled(self, event):171 self._bypass_test(event.test, "disabled")172 def on_test_session_end(self, event):173 _print_summary(ReportStats.from_report(self.report), self.report.parallelized)174class ConsoleBackend(ReportingBackend, ReportingSessionBuilderMixin):175 def __init__(self):176 width, height = terminalsize.get_terminal_size()177 self.terminal_width = width178 self.show_test_full_path = True179 def get_name(self):180 return "console"181 def create_reporting_session(self, report_dir, report, parallel, saving_strategy):182 return \183 ParallelConsoleReportingSession(self.terminal_width, report) if parallel else \184 SequentialConsoleReportingSession(self.terminal_width, self.show_test_full_path, report)185def print_report_as_test_run(report, test_filter):186 suites = filter_suites(report.get_suites(), test_filter)187 ###188 # Setup terminal189 ###190 terminal_width, _ = terminalsize.get_terminal_size()191 ###192 # Display suite results193 ###194 suite_idx = 0195 for suite in flatten_suites(suites):196 if len(suite.get_tests()) == 0:197 continue198 if suite_idx > 0:199 print()200 header_line = _make_suite_header_line(suite, terminal_width)201 print(header_line)202 for test_idx, test in enumerate(suite.get_tests()):203 test_result_line, _ = _make_test_result_line(test.path, num=test_idx+1, status=test.status)204 print(test_result_line)205 suite_idx += 1206 ###207 # Display summary208 ###209 if suite_idx > 0:210 if test_filter:211 stats = ReportStats.from_suites(suites, report.parallelized)212 else:213 stats = ReportStats.from_report(report)214 _print_summary(stats, report.parallelized)215 else:...
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!!