Best Python code snippet using lisa_python
test_testsuite.py
Source:test_testsuite.py
...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()337 assert self.default_env338 self.default_env.status = EnvironmentStatus.Deployed339 self.default_env._is_initialized = True340 for node in self.default_env.nodes.list():341 node.os = Posix(node)342 for result in self.case_results:343 check_result = result.check_environment(self.default_env)344 self.assertTrue(check_result)345 def test_result_check_env_os_type_not_unsupported(self) -> None:346 _ = self.generate_suite_instance()347 assert self.default_env348 self.default_env.status = EnvironmentStatus.Connected349 self.default_env._is_initialized = True350 case_metadata = self.case_results[0].runtime_data.metadata351 case_metadata.requirement = simple_requirement(352 min_count=2, unsupported_os=[Posix]353 )354 case_metadata = self.case_results[1].runtime_data.metadata355 case_metadata.requirement = simple_requirement(min_count=2, unsupported_os=[])356 for node in self.default_env.nodes.list():357 node.os = Windows(node)358 for result in self.case_results:359 check_result = result.check_environment(self.default_env)360 self.assertTrue(check_result)361 def test_result_check_env_os_type_supported(self) -> None:362 _ = self.generate_suite_instance()363 assert self.default_env364 self.default_env.status = EnvironmentStatus.Connected365 self.default_env._is_initialized = True366 case_metadata = self.case_results[0].runtime_data.metadata367 case_metadata.requirement = simple_requirement(368 min_count=2, supported_os=[Posix]369 )370 for node in self.default_env.nodes.list():371 node.os = Posix(node)372 for result in self.case_results:373 check_result = result.check_environment(self.default_env)374 self.assertTrue(check_result)375 def test_result_check_env_os_type_not_supported(self) -> None:376 _ = self.generate_suite_instance()377 assert self.default_env378 self.default_env.status = EnvironmentStatus.Connected379 self.default_env._is_initialized = True380 case_metadata = self.case_results[0].runtime_data.metadata381 case_metadata.requirement = simple_requirement(382 min_count=2, supported_os=[Posix]383 )384 case_metadata = self.case_results[1].runtime_data.metadata385 case_metadata.requirement = simple_requirement(386 min_count=2, supported_os=[Windows]387 )388 for node in self.default_env.nodes.list():389 node.os = Windows(node)390 with self.assertRaises(SkippedException) as cm:391 self.case_results[0].check_environment(self.default_env)392 self.assertEqual("OS type mismatch:", str(cm.exception)[:17])393 check_result = self.case_results[1].check_environment(self.default_env)394 self.assertTrue(check_result)395 def test_skipped_not_meet_req(self) -> None:396 _ = self.generate_suite_instance()397 assert self.default_env398 self.default_env.status = EnvironmentStatus.Deployed399 case_metadata = self.case_results[0].runtime_data.metadata400 case_metadata.requirement = simple_requirement(min_count=3)401 result = self.case_results[0]402 check_result = result.check_environment(self.default_env, save_reason=True)403 self.assertFalse(check_result)404 # only save reason, but not set final status, so that it can try next env405 self.assertEqual(TestStatus.QUEUED, result.status)406 assert result.check_results407 self.assertFalse(result.check_results.result)408 self.assertListEqual(409 ["no enough nodes, requirement: 3, capability: 2."],410 result.check_results.reasons,...
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!!