Best Python code snippet using lemoncheesecake
test_cmd_run.py
Source:test_cmd_run.py
1import os2import os.path as osp3try:4 # Python 35 from unittest.mock import patch6except ImportError:7 # Python 28 from mock import patch9import pytest10from callee import Any, Matcher11import lemoncheesecake.api as lcc12from lemoncheesecake.project import Project13from lemoncheesecake.suite import load_suite_from_class14from lemoncheesecake.cli import build_cli_args15from lemoncheesecake.cli.commands.run import run_suites_from_project16from lemoncheesecake.reporting import savingstrategy17from lemoncheesecake.exceptions import LemoncheesecakeException18from helpers.runner import generate_project, run_main19from helpers.cli import assert_run_output, cmdout20from helpers.utils import change_dir, tmp_cwd, env_vars21TEST_MODULE = """import lemoncheesecake.api as lcc22@lcc.suite("My Suite")23class mysuite:24 @lcc.test("My Test 1")25 def mytest1(self):26 lcc.log_error("failure")27 @lcc.test("My Test 2")28 def mytest2(self):29 pass30"""31FIXTURE_MODULE = """import lemoncheesecake.api as lcc32@lcc.fixture()33def fixt():34 return 4235"""36TEST_MODULE_USING_FIXTURES = """import lemoncheesecake.api as lcc37from lemoncheesecake.matching import *38@lcc.suite("My Suite")39class mysuite:40 @lcc.test("My Test 1")41 def mytest1(self, fixt):42 check_that("val", fixt, equal_to(42))43"""44@pytest.fixture()45def project(tmp_cwd):46 generate_project(tmp_cwd, "mysuite", TEST_MODULE)47@pytest.fixture()48def failing_project(project):49 return project50@pytest.fixture()51def project_with_fixtures(tmp_cwd):52 generate_project(tmp_cwd, "mysuite", TEST_MODULE_USING_FIXTURES, FIXTURE_MODULE)53@pytest.fixture()54def successful_project(project_with_fixtures):55 return project_with_fixtures56@pytest.fixture()57def run_project_mock(mocker):58 return mocker.patch("lemoncheesecake.cli.commands.run.run_project")59def test_run_with_filter(project, cmdout):60 assert run_main(["run", "mysuite.mytest1"]) == 061 assert_run_output(cmdout, "mysuite", failed_tests=["mytest1"])62def test_project_with_fixtures(project_with_fixtures, cmdout):63 assert run_main(["run", "mysuite.mytest1"]) == 064 assert_run_output(cmdout, "mysuite", successful_tests=["mytest1"])65def test_stop_on_failure(project, cmdout):66 assert run_main(["run", "--stop-on-failure"]) == 067 assert_run_output(cmdout, "mysuite", failed_tests=["mytest1"], skipped_tests=["mytest2"])68def test_cli_run(project, cmdout):69 assert run_main(["run"]) == 070 assert_run_output(cmdout, "mysuite", successful_tests=["mytest2"], failed_tests=["mytest1"])71def test_project_path(tmpdir, cmdout):72 generate_project(tmpdir.strpath, "mysuite", TEST_MODULE)73 assert run_main(["run", "-p", tmpdir.strpath]) == 074 assert_run_output(cmdout, "mysuite", successful_tests=["mytest2"], failed_tests=["mytest1"])75def test_cli_exit_error_on_failure_successful_suite(successful_project):76 assert run_main(["run", "--exit-error-on-failure"]) == 077def test_cli_exit_error_on_failure_failing_suite(failing_project):78 assert run_main(["run", "--exit-error-on-failure"]) == 179@lcc.suite("Sample Suite")80class SampleSuite:81 @lcc.test("test 1")82 def test_1(self):83 pass84class SampleProject(Project):85 def __init__(self, project_dir="."):86 Project.__init__(self, project_dir)87 def load_suites(self):88 return [load_suite_from_class(SampleSuite)]89def _test_run_suites_from_project(project, cli_args, expected_args):90 with patch("lemoncheesecake.cli.commands.run.run_project") as mocked:91 run_suites_from_project(project, build_cli_args(["run"] + cli_args))92 mocked.assert_called_with(*expected_args)93class ReportingBackendMatcher(Matcher):94 def __init__(self, *expected):95 self.expected = expected96 def match(self, actual):97 return sorted([b.get_name() for b in actual]) == sorted(self.expected)98def test_run_suites_from_project_default():99 project = SampleProject(".")100 _test_run_suites_from_project(101 project, [],102 (project, Any(), Any(), ReportingBackendMatcher("json", "html", "console"),103 osp.join(os.getcwd(), "report"), savingstrategy.save_at_each_failed_test_strategy, False, False, 1)104 )105def test_run_suites_from_project_thread_cli_args():106 _test_run_suites_from_project(107 SampleProject(), ["--threads", "4"],108 (Any(), Any(), Any(), Any(), Any(), Any(), Any(), Any(), 4)109 )110def test_run_suites_from_project_thread_env():111 with env_vars(LCC_THREADS="4"):112 _test_run_suites_from_project(113 SampleProject(), [],114 (Any(), Any(), Any(), Any(), Any(), Any(), Any(), Any(), 4)115 )116def test_run_suites_from_project_thread_cli_args_while_threaded_is_disabled():117 project = SampleProject()118 project.threaded = False119 with pytest.raises(LemoncheesecakeException, match="does not support multi-threading"):120 _test_run_suites_from_project(project, ["--threads", "4"], None)121def test_run_suites_from_project_saving_strategy_cli_args():122 _test_run_suites_from_project(123 SampleProject(), ["--save-report", "at_each_failed_test"],124 (Any(), Any(), Any(), Any(), Any(), savingstrategy.save_at_each_failed_test_strategy, Any(), Any(), Any())125 )126def test_run_suites_from_project_saving_strategy_env():127 with env_vars(LCC_SAVE_REPORT="at_each_failed_test"):128 _test_run_suites_from_project(129 SampleProject(), [],130 (Any(), Any(), Any(), Any(), Any(), savingstrategy.save_at_each_failed_test_strategy, Any(), Any(), Any())131 )132def test_run_suites_from_project_reporting_backends_cli_args():133 _test_run_suites_from_project(134 SampleProject(), ["--reporting", "^console"],135 (Any(), Any(), Any(), ReportingBackendMatcher("json", "html"), Any(), Any(), Any(), Any(), Any())136 )137def test_run_suites_from_project_reporting_backends_env():138 with env_vars(LCC_REPORTING="^console"):139 _test_run_suites_from_project(140 SampleProject(), [],141 (Any(), Any(), Any(), ReportingBackendMatcher("json", "html"), Any(), Any(), Any(), Any(), Any())142 )143def test_run_suites_from_project_custom_attr_default_reporting_backend_names():144 project = SampleProject()145 project.default_reporting_backend_names = ["json", "html"]146 _test_run_suites_from_project(147 project, [],148 (Any(), Any(), Any(), ReportingBackendMatcher("json", "html"), Any(), Any(), Any(), Any(), Any())149 )150def test_run_suites_from_project_force_disabled_set():151 _test_run_suites_from_project(152 SampleProject(), ["--force-disabled"],153 (Any(), Any(), Any(), Any(), Any(), Any(), True, Any(), Any())154 )155def test_run_suites_from_project_stop_on_failure_set():156 _test_run_suites_from_project(157 SampleProject(), ["--stop-on-failure"],158 (Any(), Any(), Any(), Any(), Any(), Any(), Any(), True, Any())159 )160def test_run_suites_from_project_report_dir_through_project(tmpdir):161 report_dir = tmpdir.join("other_report_dir").strpath162 class MyProject(SampleProject):163 def create_report_dir(self):164 return report_dir165 _test_run_suites_from_project(166 MyProject(), [],167 (Any(), Any(), Any(), Any(), report_dir, Any(), Any(), Any(), Any())168 )169def test_run_suites_from_project_report_dir_cli_args(tmpdir):170 report_dir = tmpdir.join("other_report_dir").strpath171 _test_run_suites_from_project(172 SampleProject(), ["--report-dir", report_dir],173 (Any(), Any(), Any(), Any(), report_dir, Any(), Any(), Any(), Any())174 )175def test_run_suites_from_project_report_dir_env(tmpdir):176 report_dir = tmpdir.join("other_report_dir").strpath177 with env_vars(LCC_REPORT_DIR=report_dir):178 _test_run_suites_from_project(179 SampleProject(), [],180 (Any(), Any(), Any(), Any(), report_dir, Any(), Any(), Any(), Any())...
savingstrategy.py
Source:savingstrategy.py
...19def save_at_each_suite_strategy(event, _):20 return isinstance(event, SuiteEndEvent)21def save_at_each_test_strategy(event, _):22 return _is_end_of_result_event(event) is not None23def save_at_each_failed_test_strategy(event, report):24 location = _is_end_of_result_event(event)25 if location:26 result = report.get(location)27 return result and result.status == "failed"28 else:29 return False30def save_at_each_log_strategy(event, _):31 return isinstance(event, SteppedEvent)32class SaveAtInterval(object):33 def __init__(self, interval):34 self.interval = interval35 self.last_saving = None36 def __call__(self, event, report):37 now = time.time()...
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!!