首页 > 其他分享 >pytest + yaml 框架 -5.调用内置方法和自定义函数

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

时间:2022-12-05 14:01:46浏览次数:44  
标签:username 内置 name 自定义 yaml pytest user post 函数

前言

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

pip install pytest-yaml-yoyo

yaml 中调用内置方法

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

$(username[:4])

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

test_fun1.yml

# 作者-上海悠悠 
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]

命令行执行用例

pytest test_fun1.yml

运行结果

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"}

字典对象取值

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

variables:
username: test123
body:
user: yoyo
email: [email protected]

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

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

test_fun2.yml完整示例

# 作者-上海悠悠
config:
name: 引用内置函数
variables:
username: test123
body:
user: yoyo
email: [email protected]

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}']

自定义函数功能

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

# conftest.py
# 作者-上海悠悠

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())

实现基本原理是自己定义一个函数,然后注册到插件内置模块 ​​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}']

函数传参数

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

# conftest.py
# 作者-上海悠悠

from pytest_yaml_yoyo import my_builtins

def func(x):
return f"hello{x}"


my_builtins.func = func

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]

函数还能引用自己在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]

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

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

# conftest.py
# 作者-上海悠悠

from pytest_yaml_yoyo import my_builtins

def func_list():
return ['test1', 'test2', 'test3']


my_builtins.func_list = func_list

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]

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



标签:username,内置,name,自定义,yaml,pytest,user,post,函数
From: https://blog.51cto.com/u_15249893/5912019

相关文章

  • el-from表单校验某一项(自定义校验规则)
    <el-form-itemlabel="适用税率"prop="rate":rules="[{validator:(field,value,cb)=>......
  • 小程序自定义扫码界面
    小程序扫一扫wx.scanCode  这个api扫描不能自定义这个时候就需要用camera自定义扫描界面;camera文档传送门   效果图:(需要获取camera授权权限)代码:<vi......
  • MeterSphere BeanShell 前后置脚本如何引用自定义 Jar 包
    需求接口测试中需要对post请求的body参数进行排序后,进行md5加密,生成sign字符串,放入请求体中提交。思路:在前置脚本中自动获取请求参数中的值,进行sign加密,生......
  • pytest + yaml 框架 -7.用例分层机制
    前言当我们测试流程类的接口,需反复去调用同一个接口,就会想到复用API,在代码里面可以写成函数去调用。那么在yaml文件中,我们可以把单个API写到一个yaml文件,测试用例去调......
  • RDLC后台自定义报表模板
    首先封装一个公共类,统一来操作RDLC报表usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Xml;usingSystem.Data;usingMi......
  • 小程序自定义底部导航 custom-tab-bar
    1.app.json     2.将custom-tab-bar放到根目录下(pages同级)3. custom-tab-bar 代码{"component":true}Component({data:{USERTYPE:'cu......
  • spring源码 自定义beanDefinition的添加逻辑
      首先我自定义了两个beandefinition,第一个会产生第二个,并且都是特殊的实现,能够避开前面的循环读取,所以,spring在最后用了一个while循环,一个flag就将,beandefinition新增......
  • 自定义RBAC(1)
    您好,我是湘王,这是我的51CTO博客,欢迎您来,欢迎您再来~在对Spring Security稍做了解之后,可以知道,SpringSecurity其实只是一个实现认证授权的框架,封装了很多实现细节。但也有一......
  • 自定义RBAC(1)
    您好,我是湘王,这是我的博客园,欢迎您来,欢迎您再来~ 在对Spring Security稍做了解之后,可以知道,SpringSecurity其实只是一个实现认证授权的框架,封装了很多实现细节。但也有......
  • 【C语言】自定义一个模仿strlen()函数功能的函数
    自定义一个模仿strlen()函数功能的函数//my_strlen()函数声明,返回字符数组中字符的个数//参数为字符数组的地址intmy_strlen(char*str);intmain(void){ charch[......