Best Python code snippet using lemoncheesecake
loader.py
Source:loader.py  
...89def _get_suite_classes_from_module(mod):90    return _get_test_symbols(mod, _is_suite_class)91def _get_generated_tests(obj):92    return getattr(obj, "_lccgeneratedtests", [])93def _get_sub_dirs_from_dir(top_dir):94    paths = [os.path.join(top_dir, path) for path in os.listdir(top_dir)]95    return sorted(filter(osp.isdir, paths))96def _normalize_link(link):97    if type(link) is str:98        return link, None99    else:100        return link101def load_suite_from_class(class_):102    # type: (Any) -> Suite103    """104    Load a suite from a class.105    """106    try:107        md = class_._lccmetadata108    except AttributeError:109        raise SuiteLoadingError("Class is not declared as a suite")110    try:111        suite_obj = class_()112    except UserError as e:113        raise e  # propagate UserError114    except Exception:115        raise LemoncheesecakeException("Got an unexpected error while instantiating suite class '%s':%s" % (116            class_.__name__, serialize_current_exception()117        ))118    suite = Suite(suite_obj, md.name, md.description)119    suite.tags.extend(md.tags)120    suite.properties.update(md.properties)121    suite.links.extend(md.links)122    suite.rank = md.rank123    suite.disabled = md.disabled124    suite.hidden = md.condition and not md.condition(suite_obj)125    try:126        _check_test_tree_node_types(suite)127    except TypeError as excp:128        raise SuiteLoadingError("Invalid suite metadata type for '%s': %s" % (suite.name, excp))129    for hook_name in SUITE_HOOKS:130        if hasattr(suite_obj, hook_name):131            suite.add_hook(hook_name, getattr(suite_obj, hook_name))132    for test in _load_tests(_get_test_methods_from_class(suite_obj)):133        suite.add_test(test)134    for test in _get_generated_tests(suite_obj):135        suite.add_test(test)136    for sub_suite in load_suites_from_classes(_get_sub_suites_from_class(suite_obj)):137        suite.add_suite(sub_suite)138    return suite139def load_suites_from_classes(classes):140    # type: (Sequence[Any]) -> List[Suite]141    """142    Load a list of suites from a list of classes.143    """144    return list(145        filter(146            lambda suite: not suite.hidden, map(load_suite_from_class, classes)147        )148    )149def load_suite_from_module(mod):150    # type: (Any) -> Suite151    """152    Load a suite from a module instance.153    """154    suite_info = getattr(mod, "SUITE", {})155    suite_condition = suite_info.get("visible_if")156    suite_name = suite_info.get("name", inspect.getmodulename(inspect.getfile(mod)))157    suite_description = suite_info.get("description", build_description_from_name(suite_name))158    suite = Suite(mod, suite_name, suite_description)159    suite.tags.extend(suite_info.get("tags", []))160    suite.properties.update(suite_info.get("properties", {}))161    suite.links.extend(map(_normalize_link, suite_info.get("links", [])))162    suite.rank = suite_info.get("rank", _get_metadata_next_rank())163    suite.hidden = suite_condition and not suite_condition(mod)164    try:165        _check_test_tree_node_types(suite)166    except TypeError as excp:167        raise SuiteLoadingError("Invalid suite metadata type for '%s': %s" % (suite.name, excp))168    for hook_name in SUITE_HOOKS:169        if hasattr(mod, hook_name):170            suite.add_hook(hook_name, getattr(mod, hook_name))171    for test in _load_tests(_get_test_functions_from_module(mod)):172        suite.add_test(test)173    for test in _get_generated_tests(mod):174        suite.add_test(test)175    for sub_suite in load_suites_from_classes(_get_suite_classes_from_module(mod)):176        suite.add_suite(sub_suite)177    return suite178def load_suite_from_file(filename):179    # type: (str) -> Suite180    """181    Load a suite from a Python module indicated by a filename.182    Raise SuiteLoadingError if the file cannot be loaded as a suite.183    """184    try:185        mod = import_module(filename)186    except ModuleImportError as e:187        raise SuiteLoadingError(str(e))188    mod_name = strip_py_ext(osp.basename(filename))189    suite = load_suite_from_module(mod)190    # in case of module containing no tests, a single sub-suite whose name is the same a as module191    # then this sub-suite will be actually considered as the suite itself192    if not hasattr(mod, "SUITE") and \193        len(suite.get_tests()) == 0 and len(suite.get_suites()) == 1 and suite.get_suites()[0].name == mod_name:194        suite = suite.get_suites()[0]195        suite.parent_suite = None196    return suite197def load_suites_from_files(patterns, excluding=()):198    # type: (Sequence[str], Sequence[str]) -> List[Suite]199    """200    Load a list of suites from a list of files.201    :param patterns: a mandatory list (a simple string can also be used instead of a single element list)202      of files to import; the wildcard '*' character can be used203    :param excluding: an optional list (a simple string can also be used instead of a single element list)204      of elements to exclude from the expanded list of files to import205    Example::206        load_suites_from_files("test_*.py")207    """208    return list(209        filter(210            lambda suite: not suite.hidden and not suite.is_empty(),211            map(load_suite_from_file, get_matching_files(patterns, excluding))212        )213    )214def load_suites_from_directory(dir, recursive=True):215    # type: (str, bool) -> List[Suite]216    """217    Load a list of suites from a directory.218    If the recursive argument is set to True, sub suites will be searched in a directory named219    from the suite module: if the suite module is "foo.py" then the sub suites directory must be "foo".220    Raise SuiteLoadingError if one or more suite could not be loaded.221    """222    if not osp.exists(dir):223        raise SuiteLoadingError("Directory '%s' does not exist" % dir)224    suites = {}225    for filename in get_py_files_from_dir(dir):226        suite = load_suite_from_file(filename)227        if not suite.hidden:228            suites[filename] = suite229    if recursive:230        for dirname in _get_sub_dirs_from_dir(dir):231            suite = suites.get(dirname + ".py")232            if not suite:233                suite_name = osp.basename(dirname)234                suite = Suite(None, suite_name, build_description_from_name(suite_name))235                suites[suite.name] = suite236            for sub_suite in load_suites_from_directory(dirname, recursive=True):237                suite.add_suite(sub_suite)...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!!
