Best Python code snippet using autotest_python
testcase_manager.py
Source:testcase_manager.py  
1#!/usr/bin/env python32# coding=utf-834#5# Copyright (c) 2020 Huawei Device Co., Ltd.6# Licensed under the Apache License, Version 2.0 (the "License");7# you may not use this file except in compliance with the License.8# You may obtain a copy of the License at9#10#     http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing, software13# distributed under the License is distributed on an "AS IS" BASIS,14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15# See the License for the specific language governing permissions and16# limitations under the License.17#1819import os20import copy21from core.utils import get_file_list_by_postfix22from core.config.config_manager import FilterConfigManager23from xdevice import platform_logger2425TESTFILE_TYPE_DATA_DIC = {"DEX": [], "HAP": [], "PYT": [],26                          "CXX": [], "BIN": []}27FILTER_SUFFIX_NAME_LIST = [".TOC", ".info", ".pyc"]282930class TestCaseManager(object):31    case_log = platform_logger("TestCaseManager")3233    def get_test_files(self, test_case_path, options):34        self.case_log.info("test case path: " + test_case_path)35        self.case_log.info("test type list: " + str(options.testtype))36        suit_file_dictionary = copy.deepcopy(TESTFILE_TYPE_DATA_DIC)37        if os.path.exists(test_case_path):38            if len(options.testtype) != 0:39                test_type_list = options.testtype40                suit_file_dictionary = self.get_test_file_data(test_case_path,41                                                               test_type_list,42                                                               options)43        else:44            self.case_log.error("%s is not exist." % test_case_path)45        return suit_file_dictionary4647    def get_test_file_data(self, test_case_path, test_type_list, options):48        suit_file_dictionary = copy.deepcopy(TESTFILE_TYPE_DATA_DIC)49        for test_type in test_type_list:50            temp_dictionary = \51                self.get_test_file_data_by_test_type(test_case_path,52                                                     test_type,53                                                     options)54            for key, value in suit_file_dictionary.items():55                suit_file_dictionary[key] = value + temp_dictionary[key]56        return suit_file_dictionary5758    def get_test_file_data_by_test_type(self, test_case_path,59                                        test_type, options):60        suit_file_dictionary = copy.deepcopy(TESTFILE_TYPE_DATA_DIC)61        test_case_out_path = os.path.join(test_case_path, test_type)62        if os.path.exists(test_case_out_path):63            self.case_log.info("The test case directory: %s" %64                               test_case_out_path)65            return self.get_all_test_file(test_case_out_path, options)66        else:67            self.case_log.error("The test case directory does not exist. %s" %68                                test_case_out_path)69        return suit_file_dictionary7071    def get_all_test_file(self, test_case_out_path, options):72        suite_file_dictionary = copy.deepcopy(TESTFILE_TYPE_DATA_DIC)73        filter_list_subsystem = FilterConfigManager().get_filtering_list(74            "subsystem_name", options.productform)75        filter_list_test_file = FilterConfigManager().get_filtering_list(76            "testfile_name", options.productform)7778        for subsystem_name in os.listdir(test_case_out_path):79            subsystem_case_dir = os.path.join(test_case_out_path,80                                              subsystem_name)81            if not os.path.isdir(subsystem_case_dir):82                continue8384            if subsystem_name in filter_list_subsystem:85                continue8687            suit_file_list = get_file_list_by_postfix(subsystem_case_dir)88            for suite_file in suit_file_list:89                if -1 != suite_file.replace(test_case_out_path, "").find(90                        os.sep + "resource" + os.sep):91                    continue9293                file_name = os.path.basename(suite_file)94                if file_name in filter_list_test_file:95                    continue9697                _, suffix_name = os.path.splitext(file_name)98                if suffix_name in FILTER_SUFFIX_NAME_LIST:99                    continue100101                if not self._get_valid_suite_file(test_case_out_path,102                                                  suite_file,103                                                  options.subsystem,104                                                  options.testmodule,105                                                  options.testsuit):106                    continue107108                if suffix_name == ".dex":109                    suite_file_dictionary["DEX"].append(suite_file)110                elif suffix_name == ".hap":111                    suite_file_dictionary["HAP"].append(suite_file)112                elif suffix_name == ".py":113                    if not self._check_python_test_file(suite_file):114                        continue115                    suite_file_dictionary["PYT"].append(suite_file)116                elif suffix_name == "":117                    suite_file_dictionary["CXX"].append(suite_file)118                elif suffix_name == ".bin":119                    suite_file_dictionary["BIN"].append(suite_file)120121        return suite_file_dictionary122123    @classmethod124    def _get_valid_suite_file(cls,125                              test_case_out_path,126                              suit_file,127                              test_subsystem,128                              test_module,129                              test_suit):130        is_valid_status = False131        if suit_file.startswith(test_case_out_path):132            if test_suit == "":133                suite_file_sub_path = suit_file.replace(134                    test_case_out_path, "").strip(os.sep)135                if test_subsystem != "":136                    if test_module != "":137                        if suite_file_sub_path.startswith(test_subsystem +138                                                          os.sep +139                                                          test_module +140                                                          os.sep):141                            is_valid_status = True142                    else:143                        if suite_file_sub_path.startswith(144                                test_subsystem + os.sep):145                            is_valid_status = True146                else:147                    if test_module != "":148                        dir_item_list = suite_file_sub_path.split(os.sep)149                        if len(dir_item_list) > 2 \150                                and test_module == dir_item_list[1]:151                            is_valid_status = True152                    else:153                        is_valid_status = True154            else:155                short_name, _ = os.path.splitext(156                    os.path.basename(suit_file))157                if short_name == test_suit:158                    is_valid_status = True159        return is_valid_status160161    @classmethod162    def _check_python_test_file(cls, suite_file):163        if suite_file.endswith(".py"):164            filename = os.path.basename(suite_file)165            if filename.startswith("test_"):166                return True
...test_tasks.py
Source:test_tasks.py  
1from unittest.mock import Mock23import pytest45from tasks.utils.models.task import Task6from tasks.utils.exceptions import StatusError789@pytest.fixture10def task() -> Task:11    return Task(Mock(), Mock())121314@pytest.mark.parametrize(15    'is_valid_status, status',16    (17        (True, 'done'),18        (True, 'Done'),19        (True, 'DONE'),20        (True, 'failed'),21        (False, 'seeing'),22    )23)24def test_task_add_status(is_valid_status: bool, status: str, task: Task) -> None:25    if not is_valid_status:26        with pytest.raises(StatusError):27            task.add_status(status)28    else:29        task.add_status(status)
...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!!
