Best Python code snippet using avocado_python
loader.py
Source:loader.py
...420 MissingTest: output.TERM_SUPPORT.fail_header_str,421 BrokenSymlink: output.TERM_SUPPORT.fail_header_str,422 AccessDeniedPath: output.TERM_SUPPORT.fail_header_str}423 @staticmethod424 def _is_matching_test_class(tst, test_class):425 return issubclass(tst, test_class)426 def discover(self, reference, which_tests=DiscoverMode.DEFAULT):427 """428 Discover (possible) tests from a directory.429 Recursively walk in a directory and find tests params.430 The tests are returned in alphabetic order.431 Afterwards when "allowed_test_types" is supplied it verifies if all432 found tests are of the allowed type. If not return None (even on433 partial match).434 :param reference: the directory path to inspect.435 :param which_tests: Limit tests to be displayed436 :type which_tests: :class:`DiscoverMode`437 :return: list of matching tests438 """439 tests = self._discover(reference, which_tests)440 if self.test_type:441 mapping = self.get_type_label_mapping()442 test_class = next(key for key, value in mapping.items()443 if value == self.test_type)444 for tst in tests:445 if not self._is_matching_test_class(tst[0], test_class):446 return None447 return tests448 def _discover(self, reference, which_tests=DiscoverMode.DEFAULT):449 """450 Recursively walk in a directory and find tests params.451 The tests are returned in alphabetic order.452 :param reference: the directory path to inspect.453 :param which_tests: Limit tests to be displayed454 :type which_tests: :class:`DiscoverMode`455 :return: list of matching tests456 """457 if reference is None:458 if which_tests == DiscoverMode.DEFAULT:459 return [] # Return empty set when not listing details460 else:461 reference = data_dir.get_test_dir()462 ignore_suffix = ('.data', '.pyc', '.pyo', '__init__.py',463 '__main__.py')464 # Look for filename:test_method pattern465 reference, subtests_filter = reference_split(reference)466 if subtests_filter is not None:467 subtests_filter = re.compile(subtests_filter)468 if not os.path.isdir(reference): # Single file469 return self._make_tests(reference, which_tests == DiscoverMode.ALL,470 subtests_filter)471 tests = []472 def add_test_from_exception(exception):473 """ If the exc.filename is valid test it's added to tests """474 tests.extend(self._make_tests(exception.filename,475 which_tests == DiscoverMode.ALL))476 def skip_non_test(exception): # pylint: disable=W0613477 """ Always return None """478 return None479 if which_tests == DiscoverMode.ALL:480 onerror = add_test_from_exception481 else: # DEFAULT, AVAILABLE => skip missing tests482 onerror = skip_non_test483 for dirpath, dirs, filenames in os.walk(reference, onerror=onerror):484 dirs.sort()485 for file_name in sorted(filenames):486 if file_name.startswith('.') or file_name.endswith(ignore_suffix):487 continue488 pth = os.path.join(dirpath, file_name)489 tests.extend(self._make_tests(pth,490 which_tests == DiscoverMode.ALL,491 subtests_filter))492 return tests493 def _make_simple_test(self, test_path, subtests_filter):494 return self._make_test(test.SimpleTest, test_path,495 subtests_filter=subtests_filter,496 executable=test_path)497 def _make_simple_or_broken_test(self, test_path, subtests_filter, make_broken):498 if os.access(test_path, os.X_OK):499 return self._make_simple_test(test_path, subtests_filter)500 else:501 return make_broken(NotATest, test_path,502 self.NOT_TEST_STR)503 def _make_existing_file_tests(self, test_path, make_broken,504 subtests_filter):505 return self._make_simple_or_broken_test(test_path, subtests_filter, make_broken)506 def _make_nonexisting_file_tests(self, test_path, make_broken,507 subtests_filter, test_name): # pylint: disable=W0613508 return make_broken(NotATest, test_name, "File not found "509 "('%s'; '%s')" % (test_name, test_path))510 @staticmethod511 def _make_test(klass, uid, description=None, subtests_filter=None,512 **test_arguments):513 """514 Create test template515 :param klass: test class516 :param uid: test uid (by default used as id and name)517 :param description: Description appended to "uid" (for listing purpose)518 :param subtests_filter: optional filter of methods for avocado tests519 :param test_arguments: arguments to be passed to the klass(test_arguments)520 """521 if subtests_filter and not subtests_filter.search(uid):522 return []523 if description:524 uid = "%s: %s" % (uid, description)525 test_arguments["name"] = uid526 return [(klass, test_arguments)]527 def _make_tests(self, test_path, list_non_tests, subtests_filter=None):528 """529 Create test templates from given path530 :param test_path: File system path531 :param list_non_tests: include bad tests (NotATest, BrokenSymlink,...)532 :param subtests_filter: optional filter of methods for avocado tests533 """534 def ignore_broken(klass, uid, description=None): # pylint: disable=W0613535 """ Always return empty list """536 return []537 if list_non_tests: # return broken test with params538 make_broken = self._make_test539 else: # return empty set instead540 make_broken = ignore_broken541 test_name = test_path542 if os.path.exists(test_path):543 if os.access(test_path, os.R_OK) is False:544 return make_broken(AccessDeniedPath, test_path, "Is not "545 "readable")546 return self._make_existing_file_tests(test_path, make_broken,547 subtests_filter)548 else:549 if os.path.islink(test_path):550 try:551 if not os.path.isfile(os.readlink(test_path)):552 return make_broken(BrokenSymlink, test_path, "Is a "553 "broken symlink")554 except OSError:555 return make_broken(AccessDeniedPath, test_path, "Is not "556 "accessible.")557 return self._make_nonexisting_file_tests(test_path, make_broken,558 subtests_filter,559 test_name)560class FileLoader(SimpleFileLoader):561 """562 Test loader class.563 """564 name = 'file'565 NOT_TEST_STR = ("Not an INSTRUMENTED (avocado.Test based), PyUNITTEST ("566 "unittest.TestCase based) or SIMPLE (executable) test")567 @staticmethod568 def get_type_label_mapping():569 mapping = SimpleFileLoader.get_type_label_mapping()570 mapping.update(571 {test.SimpleTest: 'SIMPLE',572 test.Test: 'INSTRUMENTED',573 test.PythonUnittest: 'PyUNITTEST'})574 return mapping575 @staticmethod576 def get_decorator_mapping():577 mapping = SimpleFileLoader.get_decorator_mapping()578 mapping.update(579 {test.SimpleTest: output.TERM_SUPPORT.healthy_str,580 test.Test: output.TERM_SUPPORT.healthy_str,581 test.PythonUnittest: output.TERM_SUPPORT.healthy_str})582 return mapping583 @staticmethod584 def _is_matching_test_class(tst, test_class):585 if test_class is test.Test:586 # Instrumented tests are defined as string and loaded at the587 # execution time.588 return isinstance(tst, str)589 else:590 return not isinstance(tst, str) and issubclass(tst, test_class)591 def _find_python_unittests(self, test_path, disabled, subtests_filter):592 result = []593 class_methods = safeloader.find_python_unittests(test_path)594 for klass, methods in class_methods.items():595 if klass in disabled:596 continue597 if test_path.endswith(".py"):598 test_path = test_path[:-3]...
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!!