Best Python code snippet using tempest_python
check_uuid.py
Source:check_uuid.py  
...78            return '.'.join((self.package.__name__,) +79                            tuple(relative_path.split('/')))80        else:81            return self.package.__name__82    def _modules_search(self):83        """Recursive search for python modules in base package"""84        modules = []85        for root, dirs, files in os.walk(self.base_path):86            if not os.path.exists(os.path.join(root, '__init__.py')):87                continue88            root_package = self._path_to_package(root)89            for item in files:90                if item.endswith('.py'):91                    module_name = '.'.join((root_package,92                                           os.path.splitext(item)[0]))93                    if not module_name.startswith(UNIT_TESTS_EXCLUDE):94                        modules.append(module_name)95        return modules96    @staticmethod97    def _get_idempotent_id(test_node):98        """99        Return key-value dict with all metadata from @test.idempotent_id100        decorators for test method101        """102        idempotent_id = None103        for decorator in test_node.decorator_list:104            if (hasattr(decorator, 'func') and105                hasattr(decorator.func, 'attr') and106                decorator.func.attr == DECORATOR_NAME and107                hasattr(decorator.func, 'value') and108                decorator.func.value.id == DECORATOR_MODULE):109                for arg in decorator.args:110                    idempotent_id = ast.literal_eval(arg)111        return idempotent_id112    @staticmethod113    def _is_decorator(line):114        return line.strip().startswith('@')115    @staticmethod116    def _is_def(line):117        return line.strip().startswith('def ')118    def _add_uuid_to_test(self, patcher, test_node, source_path):119        with open(source_path) as src:120            src_lines = src.read().split('\n')121        lineno = test_node.lineno122        insert_position = lineno123        while True:124            if (self._is_def(src_lines[lineno - 1]) or125                    (self._is_decorator(src_lines[lineno - 1]) and126                        (DECORATOR_TEMPLATE.split('(')[0] <=127                            src_lines[lineno - 1].strip().split('(')[0]))):128                insert_position = lineno129                break130            lineno += 1131        patcher.add_patch(132            source_path,133            ' ' * test_node.col_offset + DECORATOR_TEMPLATE % uuid.uuid4(),134            insert_position135        )136    @staticmethod137    def _is_test_case(module, node):138        if (node.__class__ is ast.ClassDef and139                hasattr(module, node.name) and140                inspect.isclass(getattr(module, node.name))):141            return issubclass(getattr(module, node.name), unittest.TestCase)142    @staticmethod143    def _is_test_method(node):144        return (node.__class__ is ast.FunctionDef145                and node.name.startswith('test_'))146    @staticmethod147    def _next_node(body, node):148        if body.index(node) < len(body):149            return body[body.index(node) + 1]150    @staticmethod151    def _import_name(node):152        if type(node) == ast.Import:153            return node.names[0].name154        elif type(node) == ast.ImportFrom:155            return '%s.%s' % (node.module, node.names[0].name)156    def _add_import_for_test_uuid(self, patcher, src_parsed, source_path):157        with open(source_path) as f:158            src_lines = f.read().split('\n')159        line_no = 0160        tempest_imports = [node for node in src_parsed.body161                           if self._import_name(node) and162                           'tempest.' in self._import_name(node)]163        if not tempest_imports:164            import_snippet = '\n'.join(('', IMPORT_LINE, ''))165        else:166            for node in tempest_imports:167                if self._import_name(node) < DECORATOR_IMPORT:168                    continue169                else:170                    line_no = node.lineno171                    import_snippet = IMPORT_LINE172                    break173            else:174                line_no = tempest_imports[-1].lineno175                while True:176                    if (not src_lines[line_no - 1] or177                            getattr(self._next_node(src_parsed.body,178                                                    tempest_imports[-1]),179                                    'lineno') == line_no or180                            line_no == len(src_lines)):181                        break182                    line_no += 1183                import_snippet = '\n'.join((IMPORT_LINE, ''))184        patcher.add_patch(source_path, import_snippet, line_no)185    def get_tests(self):186        """Get test methods with sources from base package with metadata"""187        tests = {}188        for module_name in self._modules_search():189            tests[module_name] = {}190            module = importlib.import_module(module_name)191            source_path = '.'.join(192                (os.path.splitext(module.__file__)[0], 'py')193            )194            with open(source_path, 'r') as f:195                source = f.read()196            tests[module_name]['source_path'] = source_path197            tests[module_name]['tests'] = {}198            source_parsed = ast.parse(source)199            tests[module_name]['ast'] = source_parsed200            tests[module_name]['import_valid'] = (201                hasattr(module, DECORATOR_MODULE) and202                inspect.ismodule(getattr(module, DECORATOR_MODULE))...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!!
