How to use set_fail_phase method in lisa

Best Python code snippet using lisa_python

test_testsuite.py

Source:test_testsuite.py Github

copy

Full Screen

...45 self.queued = queued46 self.partial_pass = partial_pass47 self.fail_case_count = fail_case_count48 self.check_variable = check_variable49 def set_fail_phase(50 self,51 fail_on_before_suite: bool = False,52 fail_on_after_suite: bool = False,53 fail_on_before_case: bool = False,54 fail_on_after_case: bool = False,55 partial_pass: bool = False,56 skipped: bool = False,57 fail_case_count: int = 0,58 check_variable: bool = False,59 ) -> None:60 self.fail_on_before_suite = fail_on_before_suite61 self.fail_on_after_suite = fail_on_after_suite62 self.fail_on_before_case = fail_on_before_case63 self.fail_on_after_case = fail_on_after_case64 self.partial_pass = partial_pass65 self.skipped = skipped66 self.fail_case_count = fail_case_count67 self.check_variable = check_variable68 def before_suite(self, log: Logger, **kwargs: Any) -> None:69 if self.fail_on_before_suite:70 raise LisaException("failed")71 def after_suite(self, log: Logger, **kwargs: Any) -> None:72 if self.fail_on_after_suite:73 raise LisaException("failed")74 def before_case(self, log: Logger, **kwargs: Any) -> None:75 if self.fail_on_before_case:76 raise LisaException("failed")77 def after_case(self, log: Logger, **kwargs: Any) -> None:78 if self.fail_on_after_case:79 raise LisaException("failed")80 def mock_ut1(self, *args: Any, **kwargs: Any) -> None:81 if self.partial_pass:82 raise PassedException("mock_ut1 passed with warning")83 if self.skipped:84 raise SkippedException("mock_ut1 skipped this run")85 while self.fail_case_count > 0:86 self.fail_case_count -= 187 raise LisaException("mock_ut1 failed")88 def mock_ut2(self, variables: Dict[str, Any], **kwargs: Any) -> None:89 if self.check_variable:90 assert_that(variables).described_as("variable must exists").contains_entry(91 {"var": 1}92 )93 else:94 assert_that(variables).described_as("variable must not exists").is_empty()95class MockTestSuite2(TestSuite):96 def mock_ut3(self, *args: Any, **kwargs: Any) -> None:97 ...98def cleanup_cases_metadata() -> None:99 get_cases_metadata().clear()100 get_suites_metadata().clear()101def generate_cases_metadata() -> List[TestCaseMetadata]:102 ut_cases = [103 TestCaseMetadata(104 "ut1",105 0,106 requirement=simple_requirement(min_count=2),107 ),108 TestCaseMetadata("ut2", 1),109 TestCaseMetadata("ut3", 2),110 ]111 suite_metadata1 = TestSuiteMetadata("a1", "c1", "des1", ["t1", "t2"])112 suite_metadata1(MockTestSuite)113 ut_cases[0](MockTestSuite.mock_ut1)114 ut_cases[1](MockTestSuite.mock_ut2)115 suite_metadata2 = TestSuiteMetadata(116 "a2",117 "c2",118 "des2",119 ["t2", "t3"],120 requirement=node_requirement(node=schema.NodeSpace(core_count=8)),121 )122 suite_metadata2(MockTestSuite2)123 ut_cases[2](MockTestSuite2.mock_ut3)124 return ut_cases125def generate_cases_result() -> List[TestResult]:126 case_metadata = generate_cases_metadata()127 case_results = [TestResult("0", TestCaseRuntimeData(x)) for x in case_metadata]128 return case_results129def select_and_check(130 ut: TestCase, case_runbook: List[Any], expected_descriptions: List[str]131) -> List[TestCaseRuntimeData]:132 runbook = RunbookBuilder._validate_and_load({constants.TESTCASE: case_runbook})133 case_metadata = generate_cases_metadata()134 runbook.testcase = parse_testcase_filters(runbook.testcase_raw)135 filters = cast(List[schema.TestCase], runbook.testcase)136 selected = select_testcases(filters, case_metadata)137 ut.assertListEqual(expected_descriptions, [case.description for case in selected])138 return selected139class TestSuiteTestCase(TestCase):140 def generate_suite_instance(self) -> MockTestSuite:141 case_results = generate_cases_result()142 self.case_results = case_results[:2]143 suite_metadata = case_results[0].runtime_data.metadata.suite144 runbook = generate_runbook(is_single_env=True, local=True, remote=True)145 envs = load_environments(runbook)146 self.default_env = list(envs.values())[0]147 assert self.default_env148 test_suite = MockTestSuite(149 metadata=suite_metadata,150 )151 return test_suite152 def setUp(self) -> None:153 cleanup_cases_metadata()154 def test_expanded_nodespace(self) -> None:155 cases = generate_cases_metadata()156 for case in cases:157 self.assertIsNotNone(case.requirement)158 assert case.requirement.environment159 for node in case.requirement.environment.nodes:160 self.assertEqual(161 1, node.node_count, "node count should be expanded to 1"162 )163 def test_case_override_suite(self) -> None:164 cases = generate_cases_metadata()165 case1_found = False166 case2_found = False167 for case in cases:168 assert case.requirement.environment169 assert case.suite.requirement.environment170 if case.name == "mock_ut1":171 self.assertEqual(2, len(case.requirement.environment.nodes))172 self.assertEqual(1, len(case.suite.requirement.environment.nodes))173 case1_found = True174 if case.name == "mock_ut2":175 self.assertEqual(1, len(case.requirement.environment.nodes))176 self.assertEqual(1, len(case.suite.requirement.environment.nodes))177 case2_found = True178 self.assertEqual(True, case1_found)179 self.assertEqual(True, case2_found)180 def test_test_result_canrun(self) -> None:181 runbook = [{constants.TESTCASE_CRITERIA: {"priority": [0, 1, 2]}}]182 cases = select_and_check(self, runbook, ["ut1", "ut2", "ut3"])183 case = cases[0]184 for status in TestStatus:185 result = TestResult("0", case)186 result.set_status(status, f"set_{status}")187 self.assertEqual(f"set_{status}", result.message)188 self.assertEqual(status, result.status)189 if status == TestStatus.QUEUED:190 self.assertEqual(True, result.is_queued)191 else:192 self.assertEqual(False, result.is_queued)193 def test_skip_before_suite_failed(self) -> None:194 test_suite = self.generate_suite_instance()195 test_suite.set_fail_phase(fail_on_before_suite=True)196 test_suite.start(197 environment=self.default_env,198 case_results=self.case_results,199 case_variables={},200 )201 for result in self.case_results:202 self.assertEqual(TestStatus.SKIPPED, result.status)203 self.assertEqual("before_suite: failed", result.message)204 def test_pass_after_suite_failed(self) -> None:205 test_suite = self.generate_suite_instance()206 test_suite.set_fail_phase(fail_on_after_suite=True)207 test_suite.start(208 environment=self.default_env,209 case_results=self.case_results,210 case_variables={},211 )212 for result in self.case_results:213 self.assertEqual(TestStatus.PASSED, result.status)214 self.assertEqual("", result.message)215 def test_variable_exists(self) -> None:216 test_suite = self.generate_suite_instance()217 test_suite.set_fail_phase(check_variable=True)218 test_suite.start(219 environment=self.default_env,220 case_results=self.case_results,221 case_variables={"var": 1},222 )223 for result in self.case_results:224 self.assertEqual(TestStatus.PASSED, result.status)225 self.assertEqual("", result.message)226 def test_skip_before_case_failed(self) -> None:227 test_suite = self.generate_suite_instance()228 test_suite.set_fail_phase(fail_on_before_case=True)229 test_suite.start(230 environment=self.default_env,231 case_results=self.case_results,232 case_variables={},233 )234 for result in self.case_results:235 self.assertEqual(TestStatus.SKIPPED, result.status)236 self.assertEqual("before_case: failed", result.message)237 def test_pass_after_case_failed(self) -> None:238 test_suite = self.generate_suite_instance()239 test_suite.set_fail_phase(fail_on_after_case=True)240 test_suite.start(241 environment=self.default_env,242 case_results=self.case_results,243 case_variables={},244 )245 for result in self.case_results:246 self.assertEqual(TestStatus.PASSED, result.status)247 self.assertEqual("", result.message)248 def test_skip_case_failed(self) -> None:249 test_suite = self.generate_suite_instance()250 test_suite.set_fail_phase(fail_case_count=1)251 test_suite.start(252 environment=self.default_env,253 case_results=self.case_results,254 case_variables={},255 )256 result = self.case_results[0]257 self.assertEqual(TestStatus.FAILED, result.status)258 self.assertEqual("failed. LisaException: mock_ut1 failed", result.message)259 result = self.case_results[1]260 self.assertEqual(TestStatus.PASSED, result.status)261 self.assertEqual("", result.message)262 def test_retry_passed(self) -> None:263 test_suite = self.generate_suite_instance()264 test_suite.set_fail_phase(fail_case_count=1)265 result = self.case_results[0]266 result.runtime_data.retry = 1267 test_suite.start(268 environment=self.default_env,269 case_results=self.case_results,270 case_variables={},271 )272 self.assertEqual(TestStatus.PASSED, result.status)273 self.assertEqual("", result.message)274 result = self.case_results[1]275 self.assertEqual(TestStatus.PASSED, result.status)276 self.assertEqual("", result.message)277 def test_partial_passed(self) -> None:278 test_suite = self.generate_suite_instance()279 test_suite.set_fail_phase(partial_pass=True)280 result = self.case_results[0]281 test_suite.start(282 environment=self.default_env,283 case_results=self.case_results,284 case_variables={},285 )286 self.assertEqual(TestStatus.PASSED, result.status)287 self.assertEqual("warning: mock_ut1 passed with warning", result.message)288 result = self.case_results[1]289 self.assertEqual(TestStatus.PASSED, result.status)290 self.assertEqual("", result.message)291 def test_skipped(self) -> None:292 test_suite = self.generate_suite_instance()293 test_suite.set_fail_phase(skipped=True)294 result = self.case_results[0]295 test_suite.start(296 environment=self.default_env,297 case_results=self.case_results,298 case_variables={},299 )300 self.assertEqual(TestStatus.SKIPPED, result.status)301 self.assertEqual("skipped: mock_ut1 skipped this run", result.message)302 result = self.case_results[1]303 self.assertEqual(TestStatus.PASSED, result.status)304 self.assertEqual("", result.message)305 def test_retry_notenough_failed(self) -> None:306 test_suite = self.generate_suite_instance()307 test_suite.set_fail_phase(fail_case_count=2)308 result = self.case_results[0]309 result.runtime_data.retry = 1310 test_suite.start(311 environment=self.default_env,312 case_results=self.case_results,313 case_variables={},314 )315 self.assertEqual(TestStatus.FAILED, result.status)316 self.assertEqual("failed. LisaException: mock_ut1 failed", result.message)317 result = self.case_results[1]318 self.assertEqual(TestStatus.PASSED, result.status)319 self.assertEqual("", result.message)320 def test_attempt_ignore_failure(self) -> None:321 test_suite = self.generate_suite_instance()322 test_suite.set_fail_phase(fail_case_count=2)323 result = self.case_results[0]324 result.runtime_data.ignore_failure = True325 test_suite.start(326 environment=self.default_env,327 case_results=self.case_results,328 case_variables={},329 )330 self.assertEqual(TestStatus.ATTEMPTED, result.status)331 self.assertEqual("mock_ut1 failed", result.message)332 result = self.case_results[1]333 self.assertEqual(TestStatus.PASSED, result.status)334 self.assertEqual("", result.message)335 def test_result_check_env_not_ready_os_type(self) -> None:336 _ = self.generate_suite_instance()...

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 lisa 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