HttpRunner3.x 源码解析(4)-工具类loader.py

这些方法可以灵活的引用在其他项目中。

加载yaml文件

def _load_yaml_file(yaml_file: Text):
    """ load yaml file and check file content format
    """
    with open(yaml_file, mode="rb") as stream:
        try:
            yaml_content = yaml.load(stream, Loader=yaml.FullLoader)
        except yaml.YAMLError as ex:
            err_msg = f"YAMLError:\nfile: {yaml_file}\nerror: {ex}"
            logger.error(err_msg)
            raise exceptions.FileFormatError

        return yaml_content

加载json文件

def _load_json_file(json_file: Text):
    """ load json file and check file content format
    """
    with open(json_file, mode="rb") as data_file:
        try:
            json_content = json.load(data_file)
        except json.JSONDecodeError as ex:
            err_msg = f"JSONDecodeError:\nfile: {json_file}\nerror: {ex}"
            raise exceptions.FileFormatError(err_msg)

        return json_content

加载测试用例文件

def load_test_file(test_file: Text):
    #根据后缀判断是否为json/yaml文件,其他格式不支持
    """load testcase/testsuite file content"""
    if not os.path.isfile(test_file):
        raise exceptions.FileNotFound(f"test file not exists: {test_file}")

    file_suffix = os.path.splitext(test_file)[1].lower()
    if file_suffix == ".json":
        test_file_content = _load_json_file(test_file)
    elif file_suffix in [".yaml", ".yml"]:
        test_file_content = _load_yaml_file(test_file)
    else:
        # '' or other suffix
        raise exceptions.FileFormatError(
            f"testcase/testsuite file should be YAML/JSON format, invalid format file: {test_file}"
        )

    return test_file_content

load testcase

def load_testcase(testcase: Dict):
    try:
        # validate with pydantic TestCase model
        testcase_obj = TestCase.parse_obj(testcase)
    except ValidationError as ex:
        err_msg = f"TestCase ValidationError:\nerror: {ex}\ncontent: {testcase}"
        raise exceptions.TestCaseFormatError(err_msg)

    return testcase_obj
parse_obj用来根据判断传入的testcase是否符合TestCase模型。

Python - pydantic 入门介绍与 Models 的简单使用 (bbsmax.com)

class TestCase(BaseModel):
    config: TConfig
    teststeps: List[TStep]

class TConfig(BaseModel):
    name: Name
    verify: Verify = False
    base_url: BaseUrl = ""
    # Text: prepare variables in debugtalk.py, ${gen_variables()}
    variables: Union[VariablesMapping, Text] = {}
    parameters: Union[VariablesMapping, Text] = {}
    setup_hooks: Hooks = []
    teardown_hooks: Hooks = []
    export: Export = []
    path: Text = None
    weight: int = 1

Union  Union[int, str] 表示既可以是 int,也可以是 str

变量字典

VariablesMapping = Dict[Text, Any]
Hooks = List[Union[Text, Dict[Text, Text]]]
class TStep(BaseModel):
    name: Name
    request: Union[TRequest, None] = None
    testcase: Union[Text, Callable, None] = None
    variables: VariablesMapping = {}
    setup_hooks: Hooks = []
    teardown_hooks: Hooks = []
    # used to extract request's response field
    extract: VariablesMapping = {}
    # used to export session variables from referenced testcase
    export: Export = []
    validators: Validators = Field([], alias="validate")
    validate_script: List[Text] = []

Python - typing 模块 —— Union - 小菠萝测试笔记 - 博客园 (cnblogs.com)

load_dot_env_file


def load_dot_env_file(dot_env_path: Text):
    """ load .env file.

    Args:
        dot_env_path (str): .env file path

    Returns:
        dict: environment variables mapping

            {
                "UserName": "debugtalk",
                "Password": "123456",
                "PROJECT_KEY": "ABCDEFGH"
            }

    Raises:
        exceptions.FileFormatError: If .env file format is invalid.

    """
    if not os.path.isfile(dot_env_path):#如果不存在路径,返回空字典
        return {}

    logger.info(f"Loading environment variables from {dot_env_path}")
    env_variables_mapping = {}

    with open(dot_env_path, mode="rb") as fp:
        for line in fp:
            # maxsplit=1
            line = line.strip()
            if not len(line) or line.startswith(b"#"): #注释
                continue
            if b"=" in line:
                variable, value = line.split(b"=", 1)#支持=
            elif b":" in line:
                variable, value = line.split(b":", 1) #致辞:
            else:
                raise exceptions.FileFormatError(".env format error")

            env_variables_mapping[
                variable.strip().decode("utf-8")
            ] = value.strip().decode("utf-8")

    utils.set_os_environ(env_variables_mapping)
    return env_variables_mapping

 load_csv_file


def load_csv_file(csv_file: Text):
    """ load csv file and check file content format

    Args:
        csv_file (str): csv file path, csv file content is like below:

    Returns:
        list: list of parameters, each parameter is in dict format

    Examples:
        >>> cat csv_file
        username,password
        test1,111111
        test2,222222
        test3,333333

        >>> load_csv_file(csv_file)
        [
            {'username': 'test1', 'password': '111111'},
            {'username': 'test2', 'password': '222222'},
            {'username': 'test3', 'password': '333333'}
        ]

    """
    if not os.path.isabs(csv_file):
        global project_meta
        if project_meta is None:
            raise exceptions.MyBaseFailure("load_project_meta() has not been called!")

        # make compatible with Windows/Linux
        csv_file = os.path.join(project_meta.RootDir, *csv_file.split("/"))

    if not os.path.isfile(csv_file):
        # file path not exist
        raise exceptions.CSVNotFound(csv_file)

    csv_content_list = []

    with open(csv_file, encoding="utf-8") as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            csv_content_list.append(row)

    return csv_content_list

 

load_folder_files

递归返回目录下yaml/json/_test.py结尾的文件


def load_folder_files(folder_path: Text, recursive: bool = True):
    """ load folder path, return all files endswith .yml/.yaml/.json/_test.py in list.

    Args:
        folder_path (str): specified folder path to load
        recursive (bool): load files recursively if True

    Returns:
        list: files endswith yml/yaml/json
    """
    if isinstance(folder_path, (list, set)):
        files = []
        for path in set(folder_path):
            files.extend(load_folder_files(path, recursive))

        return files

    if not os.path.exists(folder_path):#如果目录不存在返回[]
        return []

    file_list = []

    for dirpath, dirnames, filenames in os.walk(folder_path):
        filenames_list = []

        for filename in filenames:
            if not filename.lower().endswith((".yml", ".yaml", ".json", "_test.py")):#不属于这几种的不支持
                continue

            filenames_list.append(filename)

        for filename in filenames_list:
            file_path = os.path.join(dirpath, filename)
            file_list.append(file_path)

        if not recursive:#不递归目录
            break

    return file_list

os.walk() 方法用于通过在目录树中游走输出在目录中的文件名,向上或者向下。

用法:os.path.dirname(path) 参数: path:代表文件系统路径的path-like对象。 返回类型:此方法返回一个字符串值,该字符串值表示指定路径中的目录名称。

 

load_file

定位文件,返回文件绝对路径


def locate_file(start_path: Text, file_name: Text):
    """ locate filename and return absolute file path.
        searching will be recursive upward until system root dir.

    Args:
        file_name (str): target locate file name
        start_path (str): start locating path, maybe file path or directory path

    Returns:
        str: located file path. None if file not found.

    Raises:
        exceptions.FileNotFound: If failed to locate file.

    """
    if os.path.isfile(start_path):
        start_dir_path = os.path.dirname(start_path)
    elif os.path.isdir(start_path):
        start_dir_path = start_path
    else:
        raise exceptions.FileNotFound(f"invalid path: {start_path}")

    file_path = os.path.join(start_dir_path, file_name)
    if os.path.isfile(file_path):
        # ensure absolute
        return os.path.abspath(file_path)

    # system root dir
    # Windows, e.g. 'E:\\'
    # Linux/Darwin, '/'
    parent_dir = os.path.dirname(start_dir_path)
    if parent_dir == start_dir_path:
        raise exceptions.FileNotFound(f"{file_name} not found in {start_path}")

    # locate recursive upward
    return locate_file(parent_dir, file_name)

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.kler.cn/a/8250.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

架构重构的技巧

1 代码重构 定义 对软件代码做任何改动以增加可读性或者简化结构而不影响输出结果。 目的 增加可读性、增加可维护性、可扩展性 3 关键点 不影响输出不修正错误不增加新的功能性 代码重构时,发现有个功能实现逻辑不合理,可直接修改吗?…

十年程序老狗手写分布式服务架构:原理、设计与实战

IT 技术日新月异地发展,我们自然不能躺在历史的温床上停歇,必须不断地学习。分布式、微服务几乎是现在的技术人员必须要了解的架构方向,从理论上来讲确实解耦了很多结构,但另一方面,又会带来更多衍生的复杂度及难点 如…

光度立体法检测原理讲解

光度立体法检测 图像辐照度 决定场景表面片辐射的因素有两个: 1.在场景表面片的照明 投在某一特定表面片上的照明量取决于该表面片在场景中相对于光源的分布位置 2.表面片反射的入射照明部分 在某一特定方向上被表面片反射的入射照明部分取决于表面材料的光学特性 反射类…

前端实现html转pdf

优秀文章: 前端实现 HTML 转 PDF 并导出 - 掘金 (juejin.cn)https://juejin.cn/post/7012049739482923039 安装 npm install html2canvas jspdf --save main.js导入: import htmlToPdf from ./utils/htmlToPdf Vue.use(htmlToPdf) html2Canvas.js文…

html+css实现的登录界面

一、界面效果 二、代码 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>登录</title><style>body {background-color: #f1f1f1;}.login-box {width: 400px;margin: 50px auto;padding: 2…

【计算机视觉·OpenCV】使用Haar+Cascade实现人脸检测

前言 人脸检测的目标是找出图像中所有的人脸对应的位置&#xff0c;算法的输出是人脸的外接矩形在图像中的坐标。使用 haar 特征和 cascade 检测器进行人脸检测是一种传统的方式&#xff0c;下面将给出利用 OpenCV 中的 haarcascade 进行人脸检测的代码。 程序流程 代码 impo…

ESP32设备驱动-MLX90615红外测温仪驱动

MLX90615红外测温仪驱动 文章目录 MLX90615红外测温仪驱动1、MLX90615介绍2、硬件准备3、软件准备4、驱动实现1、MLX90615介绍 MLX90615 是一款用于非接触式温度测量的微型红外温度计。 IR 敏感热电堆探测器芯片和信号调节 ASIC 都集成在同一个微型 TO-46 罐中。 红外测温仪出…

快速尝鲜Oracle 23c免费开发者版,惊喜多多

&#x1f4e2;&#x1f4e2;&#x1f4e2;&#x1f4e3;&#x1f4e3;&#x1f4e3; 哈喽&#xff01;大家好&#xff0c;我是【IT邦德】&#xff0c;江湖人称jeames007&#xff0c;10余年DBA及大数据工作经验 一位上进心十足的【大数据领域博主】&#xff01;&#x1f61c;&am…

分布式一致性协议

1 两阶段提交协议(2PC) 1.1 两阶段提交协议 两阶段提交协议&#xff0c;简称 2PC(2 Prepare Commit)&#xff0c;是比较常用的解决分布式事务问题的方式&#xff0c;要么所 有参与进程都提交事务&#xff0c;要么都取消事务&#xff0c;即实现 ACID 中的原子性(A)的常用手段。…

ctfshow web入门 爆破 21-28

web21 刚进去就要求我们登录&#xff0c;这里题目给了我们一个字典&#xff0c;就是这个字典为什么他那么多数字中 就一个字母的密码还不明显吗。 这里我们使用burp拦包&#xff0c;这里没有发现登录的账号密码&#xff0c;但是有一串可疑的字符串&#xff0c;尝试base64解密 这…

Java设计模式 07-装饰者模式

装饰者模式 套娃模式&#xff0c;直接new放构造器里面套 把抽象类聚合到它的子类里 该子类(装饰者)构造抽象类的实现(被装饰者) 一、星巴克咖啡订单项目&#xff08;咖啡馆&#xff09; 1)咖啡种类/单品咖啡&#xff1a;Espresso(意大利浓咖啡)、ShortBlack、LongBlack(美式…

【Spring】2—IOC容器

⭐⭐⭐⭐⭐⭐ Github主页&#x1f449;https://github.com/A-BigTree 笔记链接&#x1f449;https://github.com/A-BigTree/Code_Learning ⭐⭐⭐⭐⭐⭐ 如果可以&#xff0c;麻烦各位看官顺手点个star~&#x1f60a; 如果文章对你有所帮助&#xff0c;可以点赞&#x1f44d;…

教你如何搭建物业-后勤管理系统,demo可分享

1、简介 1.1、案例简介 本文将介绍&#xff0c;如何搭建物业-后勤管理。 1.2、应用场景 该应用包含疫情上报、绿化、安保等管理功能。 2、设置方法 2.1、表单搭建 1&#xff09;新建表单【返区登记】&#xff0c;字段设置如下&#xff1a; 名称类型名称类型姓名单行文本…

静态路由的原理和配置(理论详细实验全面)

第五章&#xff1a;静态路由 目录 第五章&#xff1a;静态路由 5.1路由器的工作原理 5.1.1路由器根据路由表转发数据 5.1.2 路由信息获取的方式 5.2路由选路原则 5.2.1最长匹配原则 5.2.2路由优先级 5.2.3路由度量值 5.3静态路由 5.3.1静态路由实验 5.3.2缺省路由实…

初学对象存储OSS---学习笔记

文章目录前言一、OSS是什么&#xff1f;下面以一个小故事介绍OSS的作用&#xff1a;二、怎么使用OSS1.进入 -----> [阿里云官网](https://www.aliyun.com/)2.点击进入OSS控制台3.获取accessKeyId 和 accessKeySecret4.拿到了accessKeyId 和 accessKeySecret &#xff0c;就可…

CTP_将C++封装为Python可调用接口

目录 写在前面&#xff1a; 前置准备&#xff1a; step 1 与上期所原始代码对比分析源码 td源码 1 配置属性-》常规-》配置类型 要为 “动态库(.dll)” 2 VC目录 -》包含目录 3 VC目录 -》 库目录 4 链接器-》常规-》附加库目录 5 链接器-》输入-》附加依赖项 vnctp.h 的功…

CTF杂项提纲

CTF的杂项是涉及编码&#xff0c;图片隐写&#xff0c;音频隐写&#xff0c;压缩包分析的方向&#xff0c;本文对MISC的知识点做了一个简单列举 常见编码 ASCII 0-9,48-57 A-Z 65-90 a-z 97-122 URL url编码又叫百分号编码&#xff0c;是统一资源定位的编码方式 base16/…

leetcode每日一题:数组篇(1/2)

&#x1f61a;一个不甘平凡的普通人&#xff0c;日更算法学习和打卡&#xff0c;期待您的关注和认可&#xff0c;陪您一起学习打卡&#xff01;&#xff01;&#xff01;&#x1f618;&#x1f618;&#x1f618; &#x1f917;专栏&#xff1a;每日算法学习 &#x1f4ac;个人…

cmake 常用方法自我总结

目录 1. 编译一个库依赖另一个库 2. 把某一个CMakeLists.txt中变量设为整个工程中任意CMakeLists.txt都可以访问 3. 枚举目录下所有匹配文件 4. 拷贝文件 5. 解决方案下分Header Files&#xff0c;Source Files目录 6. 依赖头文件&#xff08;附加依赖项&#xff09; 7.…

通过阿里云函数计算解决ChatGPT API的调用问题

ChatGPT系列文章 与其被ChatGPT取代&#xff0c;不如征服ChatGPT&#xff0c;做它的主人&#xff01; 文章目录ChatGPT系列文章前言命令行部署准备工作两行命令实现部署应用中心部署使用代理访问API总结前言 自2022年11月30日 OpenAI 发布 ChatGPT 以来&#xff0c;虽然时有唱…
最新文章