• pytest + yaml 框架 -5.调用内置方法和自定义函数


    前言

    在yaml用例文件中,有些数据不是固定的,比如注册账号,我需要每次生成不一样的,那么我们可以调用自己定义的函数
    pip 安装插件

    pip install pytest-yaml-yoyo
    
    • 1

    yaml 中调用内置方法

    pytest-yaml-yoyo 插件使用了强大的jinja2 模板引擎,所以我们在yaml文件中可以写很多python内置的语法了。
    举个例子:
    我定义了一个变量username的值是test123,但是我引用变量的时候只想取出前面四个字符串,于是可以用到引用变量语法

    $(username[:4])
    
    • 1

    可以直接对变量用python的切片语法

    test_fun1.yml

    # 作者-上海悠悠 微信/QQ交流:283340479
    # blog地址 https://www.cnblogs.com/yoyoketang/
    config:
      name: 引用内置函数
      variables:
        username: test123
    
    teststeps:
    -
      name: post
      request:
        method: POST
        url: http://httpbin.org/post
        json:
          username: ${username[:4]}
          password: "123456"
      validate:
        - eq: [status_code, 200]
        - eq: [$..username, test]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    命令行执行用例

    pytest test_fun1.yml
    
    • 1

    运行结果

    POST http://httpbin.org/post HTTP/1.1
    Host: httpbin.org
    User-Agent: python-requests/2.28.1
    Accept-Encoding: gzip, deflate
    Accept: */*
    Connection: keep-alive
    Content-Length: 42
    Content-Type: application/json
    
    {"username": "test", "password": "123456"}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    字典对象取值

    如果定义一个字典类型的变量,我们在取值的时候也可以根据key取值
    如定义变量

      variables:
        username: test123
        body:
          user: yoyo
          email: 123@qq.com
    
    • 1
    • 2
    • 3
    • 4
    • 5

    user和email的取值用2种方式,通过点属性或者用字典取值方法[key]

          username: ${body.user}
          email: ${body["user"]}
    
    • 1
    • 2

    test_fun2.yml完整示例

    # 作者-上海悠悠 微信/QQ交流:283340479
    # blog地址 https://www.cnblogs.com/yoyoketang/
    config:
      name: 引用内置函数
      variables:
        username: test123
        body:
          user: yoyo
          email: 123@qq.com
    
    teststeps:
    -
      name: post
      request:
        method: POST
        url: http://httpbin.org/post
        json:
          username: ${body.user}
          password: "123456"
          email: ${body["user"]}
      validate:
        - eq: [status_code, 200]
        - eq: [$..username, '${body.user}']
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    自定义函数功能

    自定义函数的实现,需在conftest.py (pytest 框架内置的插件文件)文件中实现

    # conftest.py
    # 作者-上海悠悠 微信/QQ交流:283340479
    # blog地址 https://www.cnblogs.com/yoyoketang/
    
    from pytest_yaml_yoyo import my_builtins
    import uuid
    import random
    
    
    def random_user():
        """生成随机账号 4-16位数字+字符a-z"""
        return str(uuid.uuid4()).replace('-', '')[:random.randint(4, 16)]
    
    
    # 注册到插件内置模块上
    my_builtins.random_user = random_user
    
    
    if __name__ == '__main__':
        print(random_user())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    实现基本原理是自己定义一个函数,然后注册到插件内置模块 my_builtins。这样我们在用例中就能找到该函数方法了

    test_fun3.yml 用例中引用内置函数示例

    config:
      name: 引用内置函数
      variables:
        username: ${random_user()}
    teststeps:
    -
      name: post
      request:
        method: POST
        url: http://httpbin.org/post
        json:
          username: ${username}
          password: "123456"
      validate:
        - eq: [status_code, 200]
        - eq: [$..username, '${username}']
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    函数传参数

    在引用自定义函数的时候,也可以传变量

    # conftest.py
    # 作者-上海悠悠 微信/QQ交流:283340479
    # blog地址 https://www.cnblogs.com/yoyoketang/
    
    from pytest_yaml_yoyo import my_builtins
    
    def func(x):
        return f"hello{x}"
    
    
    my_builtins.func = func
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    test_fun4.yml示例

    config:
      name: 引用内置函数
    teststeps:
    -
      name: post
      request:
        method: POST
        url: http://httpbin.org/post
        json:
          username: ${func("xxx")}
          password: "123456"
      validate:
        - eq: [status_code, 200]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    函数还能引用自己在config 中定义的变量

    config:
      name: 引用内置函数
      variables:
        var: test123
    teststeps:
    -
      name: post
      request:
        method: POST
        url: http://httpbin.org/post
        json:
          username: ${func(var)}
          password: "123456"
      validate:
        - eq: [status_code, 200]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    函数返回的结果也能二次取值

    如果一个函数返回list类型,我们在用例中也能取出其中的一个值

    # conftest.py
    # 作者-上海悠悠 微信/QQ交流:283340479
    # blog地址 https://www.cnblogs.com/yoyoketang/
    
    from pytest_yaml_yoyo import my_builtins
    
    def func_list():
        return ['test1', 'test2', 'test3']
    
    
    my_builtins.func_list = func_list
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    test_fun5.yml示例

    config:
      name: 引用内置函数
    
    teststeps:
    -
      name: post
      request:
        method: POST
        url: http://httpbin.org/post
        json:
          username: ${func_list().1}
          password: "123456"
      validate:
        - eq: [status_code, 200]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    list类型支持2种取值方式${func_list().1} 或者 ${func_list()[1]}

  • 相关阅读:
    0036力扣507题---完美数
    Ant Design Charts 行政区地图(ChoroplethMap)地图不显示的奇怪问题
    Shiro认证和鉴权的流程
    创建型设计模式之工厂方法模式
    DXF笔记:文字对齐的研究
    stream流参数总结
    常用脚本学习手册——Bat脚本
    matlab数据处理
    oracle 查询分隔符分隔开的所有数据
    离开大促的电商生意,应该怎么玩?
  • 原文地址:https://blog.csdn.net/qq_27371025/article/details/128125929