httprunner框架

复制代码
'''
一、httprunner命名规范
类名以Test开头
teststeps为测试步骤,每一个测试步骤叫做Step
Step里的RunRequest,是待测的API名字;
.py文件,必须以_test结尾

二、httprunner的模块
Config:全局设置,可以设置全局变量,.variables(**{});可以忽略Request库的警告,.verify(False);可以设置全局化项目地址,.base_url()
Step:测试步骤,每个接口测试用例,对应一个step
RunRequest:每个测试步骤的接口名称命名,可以是中文
RunTestCase:每个测试步骤的接口名称命名,可以是中文,只可以用于调用其他hrun文件时使用

.with_headers(**{})  请求接口的headers入参放在{}这里
.variables(**{})   可以设置全局变量         在Config()后
.base_url()  可以设置全局化项目地址          在Config()后
.with_variables(**{ })定义局部变量,仅再当前step可用       在Step里RunRequest()或RunTestCase()后

引用设置的变量:${变量名}
.with_cookies(**{}):设置cookie值
.with_data({})     data参数传参
.with_json({})     json参数传参,在headers里声明json传参{'Content-Type':'application/json;charset=utf8'}


extract().with_jmespath()       拿到接口的response,用作后续使用
extract().with_jmespath('body.data','data')     第一个是取的值,第二个参数是命名取到的变量
extract().with_jmespath('headers.Sever','cookies')
extract().with_jmespath('cookies.key','edit-key')
怎么确保取的值正确:
运行一遍httprunner,pytest -s ERP_Project_test.py

response清单返回列表数据,可以通过数组下标的方式取值
extract().with_jmespath('body.data.list[].quantity','quantity')
取出来的参数,如何运用到其他step中:${变量名}

断言.validate()
.assert_equal()  断言文本信息是否相等
.assert_greater_than()   断言获取到的值,是否大于预期设定的值
.assert_endswith()      断言获取到的值,在字符串末尾是否与预期设定的值一致
.assert_startswith()    断言获取到的值,在字符串开始是否与预期设定的值一致


业务解耦
RunTestCase('功能名称').call(调用另一个.py文件里的类名)


debugtalk.py辅助函数    创建小方法,可用于step中
1、函数的封装
(1)uuid函数
def get_random_request_id():
    return uuid.uuid4()

(2)睡眠函数
def sleep(n_secs):
    time.sleep(n_secs)

2、引用函数
引用debugtalk.py文件方法,不需要导入debugtalk.py文件
例:.teardown_hook()   退出后调用,teardown方法
.teardown_hook('${sleep(5)}')


生成allure报告
allure generate ./allure/ -0 ./reports --clean       生成allure报告到reports文件夹里


数据驱动(3种)
import pytest
from httprunner import Parameters
1.单组数据
@pytest.mark.parametrize('param',
                         Parameters({'code':[1,2,3]}))
def test_start(self, param):
    super().test_start(param)

2.多组数据——形成笛卡尔积,3*3,共9组测试用例
@pytest.mark.parametrize('param',
                         Parameters({'code':[1,2,3],
                                     'name':['x1','x2']}))
def test_start(self, param):
    super().test_start(param)

3.引用debugtalk.py写好的方法做驱动
@pytest.mark.parametrize('param',
                         Parameters({'code':[1,2,3],
                                     'name':'${getname()}'}))   debugtalk.py里定义好的getname()函数
def test_start(self, param):
    super().test_start(param)

'''
复制代码

 

posted @   爱coding的果妈  阅读(162)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示