Best Python code snippet using lemoncheesecake
test_filter.py
Source:test_filter.py
...1197 filtr = make_test_filter(prepare_cli_args(args, add_test_filter_cli_args))1198 assert filtr._tests == expected1199 report = make_report(1200 suites=[1201 make_suite_result(1202 name="tests",1203 tests=(1204 make_test_result(name="passed", status="passed"),1205 make_test_result(name="failed", status="failed"),1206 make_test_result(name="skipped", status="skipped"),1207 make_test_result(1208 name="grepable", status="passed",1209 steps=[1210 make_step(logs=[Log(Log.LEVEL_INFO, "this is grepable", ts=0)])1211 ]1212 )1213 )1214 )1215 ]...
report.py
Source:report.py
...63 test = TestResult(name or "test_%d" % _counter, description or "Test %d" % _counter)64 _counter += 165 _set_result(test, steps, status, start_time, end_time)66 return test67def make_suite_result(name=None, description=None, tests=(), sub_suites=(), setup=None, teardown=None,68 start_time=NotImplemented, end_time=NotImplemented):69 global _counter70 suite = SuiteResult(name or "suite_%d" % _counter, description or "Suite %d" % _counter)71 _counter += 172 if setup:73 suite.suite_setup = setup74 if teardown:75 suite.suite_teardown = teardown76 for test in tests:77 suite.add_test(test)78 for sub_suite in sub_suites:79 suite.add_suite(sub_suite)80 results = list(flatten_results([suite]))81 if start_time is not NotImplemented:82 suite.start_time = start_time83 elif results:84 suite.start_time = results[0].start_time85 else:86 suite.start_time = time.time()87 if end_time is not NotImplemented:88 suite.end_time = end_time89 elif results:90 suite.end_time = results[-1].end_time91 else:92 suite.end_time = time.time()93 return suite94def make_report(suites=(), setup=None, teardown=None):95 report = Report()96 if setup:97 report.test_session_setup = setup98 for suite in suites:99 report.add_suite(suite)100 if teardown:101 report.test_session_teardown = teardown102 results = list(report.all_results())103 if results:104 report.start_time = results[0].start_time105 report.end_time = results[-1].end_time106 report.saving_time = time.time()107 else:108 report.start_time = report.end_time = report.saving_time = time.time()109 return report110###111# Some helpful fixtures112###113def make_report_in_progress():114 # create a pseudo report where all elements that can be "in-progress" (meaning without115 # an end time) are present in the report116 now = time.time()117 return make_report(118 setup=make_result(start_time=now, end_time=None),119 teardown=make_result(start_time=now, end_time=None),120 suites=[make_suite_result(121 "suite", "suite",122 start_time=now, end_time=None,123 setup=make_result(start_time=now, end_time=None),124 teardown=make_result(start_time=now, end_time=None),125 tests=[126 make_test_result(127 "test_1", "test_1", start_time=now, end_time=None, status=None,128 steps=[make_step("step", start_time=now, end_time=None, logs=[make_log("info", "message", ts=now)])]129 ),130 make_test_result(131 "test_2", "test_2", start_time=now, end_time=now+1, status="passed",132 steps=[make_step("step", start_time=now, end_time=now+1, logs=[make_log("info", "message", ts=now)])]133 )134 ]...
test_report.py
Source:test_report.py
...22def test_format_and_parse_iso8601_time_5():23 _test_timestamp_round(1485105524.353112, 1485105524.353)24def test_report_stats_simple():25 report = make_report(suites=[26 make_suite_result(tests=[27 make_test_result(steps=[make_step(logs=[make_check(True)])])28 ])29 ])30 assert_report_stats(report, expected_passed_tests=1)31def test_report_stats_failure():32 report = make_report(suites=[33 make_suite_result(tests=[34 make_test_result(steps=[make_step(logs=[make_check(True), make_log("error")])])35 ])36 ])37 assert_report_stats(report, expected_failed_tests=1)38def test_report_stats_skipped():39 report = make_report(suites=[40 make_suite_result(tests=[41 make_test_result(status="skipped")42 ])43 ])44 assert_report_stats(report, expected_skipped_tests=1)45def test_test_duration_unfinished():46 test = TstResult("test", "test")47 test.start_time = NOW48 assert test.duration is None49def test_test_duration_finished():50 test = TstResult("test", "test")51 test.start_time = NOW52 test.end_time = NOW + 1.053 assert test.duration == 1.054def test_step_duration_unfinished():55 step = Step("step")56 step.start_time = NOW57 assert step.duration is None58def test_step_duration_finished():59 step = Step("step")60 step.start_time = NOW61 step.end_time = NOW + 1.062 assert step.duration == 1.063def test_suite_duration():64 suite = make_suite_result(tests=[65 make_test_result(start_time=NOW, end_time=NOW+1),66 make_test_result(start_time=NOW+1, end_time=NOW+2)67 ])68 assert suite.duration == 269def test_suite_duration_with_setup():70 suite = make_suite_result(71 setup=make_result(start_time=NOW, end_time=NOW+1),72 tests=[make_test_result(start_time=NOW+1, end_time=NOW+2)],73 )74 assert suite.duration == 275def test_suite_duration_with_teardown():76 suite = make_suite_result(77 tests=[make_test_result(start_time=NOW+1, end_time=NOW+2)],78 teardown=make_result(start_time=NOW+2, end_time=NOW+3)79 )80 assert suite.duration == 281def test_is_successful_is_true():82 @lcc.suite("suite")83 class suite:84 @lcc.test("test")85 def test(self):86 lcc.log_info("some log")87 report = run_suite_class(suite)88 assert report.is_successful()89def test_is_successful():90 @lcc.suite("suite")91 class suite:92 @lcc.test("test")93 def test(self):94 lcc.log_error("some error")95 report = run_suite_class(suite)96 assert not report.is_successful()97def test_is_successful_with_disabled_test():98 @lcc.suite("suite")99 class suite:100 @lcc.test("test_1")101 def test_1(self):102 lcc.log_info("some log")103 @lcc.test("test_2")104 @lcc.disabled()105 def test_2(self):106 lcc.log_info("some log")107 report = run_suite_class(suite)108 assert report.is_successful()109def test_is_successful_with_failed_test():110 @lcc.suite("suite")111 class suite:112 @lcc.test("test")113 def test(self):114 lcc.log_error("some error")115 report = run_suite_class(suite)116 assert not report.is_successful()117def test_is_successful_with_failed_teardown():118 @lcc.suite("suite")119 class suite:120 @lcc.test("test")121 def test(self):122 lcc.log_info("some log")123 def teardown_suite(self):124 lcc.log_error("some error")125 report = run_suite_class(suite)126 assert not report.is_successful()127def test_check_report_message_template_ok():128 template = "{passed} test passed"129 assert check_report_message_template(template) == template130def test_check_report_message_template_ko():131 with pytest.raises(ValueError):132 assert check_report_message_template("{invalid_var}'")133@pytest.fixture()134def report_sample():135 @lcc.suite()136 class suite:137 @lcc.test()138 def test_1(self):139 pass140 @lcc.test()141 @lcc.disabled()142 def test_2(self):143 pass144 @lcc.test()145 def test_3(self):146 lcc.log_error("some issue")147 @lcc.test()148 def test_4(self):149 lcc.log_info("everything ok")150 return run_suite_class(suite)151def test_report_build_message_with_start_time(report_sample):152 assert str(CURRENT_YEAR) in report_sample.build_message("{start_time}")153def test_report_build_message_with_end_time(report_sample):154 assert str(CURRENT_YEAR) in report_sample.build_message("{end_time}")155def test_report_build_message_with_duration(report_sample):156 report_sample.end_time = report_sample.start_time + 1157 assert report_sample.build_message("{duration}") == "1s"158def test_report_build_message_with_total(report_sample):159 assert report_sample.build_message("{total}") == "4"160def test_report_build_message_with_enabled(report_sample):161 assert report_sample.build_message("{enabled}") == "3"162def test_report_build_message_with_passed(report_sample):163 assert report_sample.build_message("{passed}") == "2"164def test_report_build_message_with_passed_pct(report_sample):165 assert report_sample.build_message("{passed_pct}") == "66%"166def test_report_build_message_with_failed(report_sample):167 assert report_sample.build_message("{failed}") == "1"168def test_report_build_message_with_failed_pct(report_sample):169 assert report_sample.build_message("{failed_pct}") == "33%"170def test_report_build_message_with_skipped():171 report = make_report([172 make_suite_result(tests=[173 make_test_result(status="passed"), make_test_result(status="passed"), make_test_result(status="skipped")174 ])175 ])176 assert report.build_message("{skipped}") == "1"177def test_report_build_message_with_skipped_pct():178 report = make_report([179 make_suite_result(tests=[180 make_test_result(status="passed"), make_test_result(status="passed"), make_test_result(status="skipped")181 ])182 ])183 assert report.build_message("{skipped_pct}") == "33%"184def test_report_build_message_with_disabled(report_sample):185 assert report_sample.build_message("{disabled}") == "1"186def test_report_build_message_with_disabled_pct(report_sample):187 assert report_sample.build_message("{disabled_pct}") == "25%"...
test_cmd_top.py
Source:test_cmd_top.py
...9from helpers.runner import run_suite_class10from helpers.report import make_test_result, make_suite_result, make_step, make_report11def test_get_top_suites():12 report = make_report([13 make_suite_result("suite1", tests=[make_test_result("test", start_time=0.0, end_time=1.0)]),14 make_suite_result("suite2", tests=[make_test_result("test", start_time=1.0, end_time=4.0)]),15 ])16 top_suites = TopSuites.get_top_suites(report, ResultFilter())17 assert len(top_suites) == 218 assert top_suites[0][0] == "suite2"19 assert top_suites[0][1] == 120 assert top_suites[0][2] == "3.000s"21 assert top_suites[0][3] == "75%"22 assert top_suites[1][0] == "suite1"23 assert top_suites[1][1] == 124 assert top_suites[1][2] == "1.000s"25 assert top_suites[1][3] == "25%"26def test_get_top_suites_with_suite_setup():27 @lcc.suite("suite")28 class suite:29 def setup_suite(self):30 lcc.log_info("foobar")31 @lcc.test("test")32 def test(self):33 pass34 report = run_suite_class(suite)35 top_suites = TopSuites.get_top_suites(report, ResultFilter(grep=re.compile("foobar")))36 assert len(top_suites) == 137 assert top_suites[0][0] == "suite"38 assert top_suites[0][1] == 039 assert top_suites[0][3] == "100%"40def test_top_suites_cmd(tmpdir, cmdout):41 report = make_report([42 make_suite_result("suite1", tests=[make_test_result("test", start_time=0.1, end_time=1.0)]),43 make_suite_result("suite2", tests=[make_test_result("test", start_time=1.0, end_time=4.0)]),44 ])45 report_path = tmpdir.join("report.json").strpath46 save_report_into_file(report, report_path)47 assert main(["top-suites", report_path]) == 048 lines = cmdout.get_lines()49 assert "suite2" in lines[4]50def test_top_suites_cmd_test_run_in_progress(report_in_progress_path, cmdout):51 assert main(["top-suites", report_in_progress_path]) == 052 cmdout.dump()53 cmdout.assert_substrs_anywhere(["suite"])54def test_get_top_tests():55 report = make_report([56 make_suite_result("suite1", tests=[make_test_result("test", start_time=0.0, end_time=1.0)]),57 make_suite_result("suite2", tests=[make_test_result("test", start_time=1.0, end_time=4.0)]),58 ])59 top_suites = TopTests.get_top_tests(report, ResultFilter())60 assert len(top_suites) == 261 assert top_suites[0][0] == "suite2.test"62 assert top_suites[0][1] == "3.000s"63 assert top_suites[0][2] == "75%"64 assert top_suites[1][0] == "suite1.test"65 assert top_suites[1][1] == "1.000s"66 assert top_suites[1][2] == "25%"67def test_top_tests_cmd(tmpdir, cmdout):68 report = make_report([69 make_suite_result("suite1", tests=[make_test_result("test", start_time=0.1, end_time=1.0)]),70 make_suite_result("suite2", tests=[make_test_result("test", start_time=1.0, end_time=4.0)]),71 ])72 report_path = tmpdir.join("report.json").strpath73 save_report_into_file(report, report_path)74 assert main(["top-tests", report_path]) == 075 lines = cmdout.get_lines()76 assert "suite2.test" in lines[4]77def test_top_tests_cmd_test_run_in_progress(report_in_progress_path, cmdout):78 assert main(["top-tests", report_in_progress_path]) == 079 cmdout.dump()80 cmdout.assert_substrs_anywhere(["suite.test_2"])81def test_get_top_steps():82 report = make_report([83 make_suite_result("suite1", tests=[make_test_result(steps=[84 make_step("step1", start_time=0.0, end_time=1.0),85 make_step("step1", start_time=1.0, end_time=3.0),86 ])]),87 make_suite_result("suite2", tests=[make_test_result(steps=[88 make_step("step2", start_time=3.0, end_time=4.0)89 ])]),90 ])91 top_steps = TopSteps.get_top_steps(report, StepFilter())92 assert len(top_steps) == 293 assert top_steps[0][0] == "step1"94 assert top_steps[0][1] == "2"95 assert top_steps[0][2] == "1.000s"96 assert top_steps[0][3] == "2.000s"97 assert top_steps[0][4] == "1.500s"98 assert top_steps[0][5] == "3.000s"99 assert top_steps[0][6] == "75%"100 assert top_steps[1][0] == "step2"101 assert top_steps[1][1] == "1"102 assert top_steps[1][2] == "1.000s"103 assert top_steps[1][3] == "1.000s"104 assert top_steps[1][4] == "1.000s"105 assert top_steps[1][5] == "1.000s"106 assert top_steps[1][6] == "25%"107def test_get_top_steps_with_test_session_setup_and_grep():108 @lcc.fixture(scope="session")109 def fixt():110 lcc.set_step("mystep")111 lcc.log_info("foobar")112 @lcc.suite("suite")113 class suite:114 @lcc.test("test")115 def test(self, fixt):116 pass117 report = run_suite_class(suite, fixtures=[fixt])118 top_steps = TopSteps.get_top_steps(report, StepFilter(grep=re.compile("foobar")))119 assert len(top_steps) == 1120 assert top_steps[0][0] == "mystep"121def test_get_top_steps_filter_on_passed():122 @lcc.suite("suite")123 class suite:124 @lcc.test("test")125 def test(self):126 lcc.set_step("something ok")127 lcc.log_info("info")128 lcc.set_step("something not ok")129 lcc.log_error("error")130 report = run_suite_class(suite)131 top_steps = TopSteps.get_top_steps(report, StepFilter(passed=True))132 assert len(top_steps) == 1133 assert top_steps[0][0] == "something ok"134def test_get_top_steps_filter_on_grep():135 @lcc.suite("suite")136 class suite:137 @lcc.test("test")138 def test(self):139 lcc.set_step("something ok")140 lcc.log_info("info")141 lcc.set_step("something not ok")142 lcc.log_error("error")143 report = run_suite_class(suite)144 top_steps = TopSteps.get_top_steps(report, StepFilter(grep=re.compile("error")))145 assert len(top_steps) == 1146 assert top_steps[0][0] == "something not ok"147def test_top_steps_cmd(tmpdir, cmdout):148 report = make_report([149 make_suite_result("suite1", tests=[150 make_test_result(steps=[make_step("step1", start_time=0.1, end_time=1.0)])151 ])152 ])153 report_path = tmpdir.join("report.json").strpath154 save_report_into_file(report, report_path)155 assert main(["top-steps", report_path]) == 0156 lines = cmdout.get_lines()157 assert "step1" in lines[4]158def test_top_steps_cmd_test_run_in_progress(report_in_progress_path, cmdout):159 assert main(["top-steps", report_in_progress_path]) == 0160 cmdout.dump()...
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!!