一、案例介绍
作者的git地址:https://github.com/debugtalk
项目地址:https://github.com/debugtalk/v3-demo-mubu
https://github.com/debugtalk/hogwarts-20201017.git
网址:https://mubu.com
覆盖功能场景
登录账号
创建文档
设置文档标题
正文第一行输入
换行到第二行
正文第二行输入
删除文档
校验删除结果
校验文档是否还在列表中
接口上下级关联
.extract() #通过extract方式来确定
.with_jmepath(“body.data.id”,“docId”) #通过jmepath来提取参数
jmepath的用法参考文档:jmepath.org
然后把下面的接口用到的参数都替换为docid
baseurl的替换,为了迎合多环境测试,只需要更改一次url即可
config = Config(“testcase description”).verify(False).base_url(“https://mubu.com”)
变量的替换
config = (
Config(“testcase description”)
.verify(False)
.base_url(“https://mubu.com”)
.variables({“host”: “mubu.com”,})**
)
手机号和登录密码也引用全局变量
```
// A code block
var foo = ‘bar’;
.variables(
**{
"host": "mubu.com",
"phone": "18613143458",
"password": "moFrwx$!kz3DTRm@@*aV",
}
**user-agent替换,get_httprunner_version()是ddebugtalk.pyde的方法**
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210616140617615.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80OTg1NTI1MQ==,size_16,color_FFFFFF,t_70)
**局部变量的设置**
nodeId()方法是在debugtalk.py函数中编写的
```javascript
def nodeId():
return str(uuid.uuid4())
局部变量的编写:
.with_variables(nodeId="$nodeId()")
把登录单独写然后其他函数调用,实现层级调用
RunTestCase(“login mubu”).call(MubuLogin)
export 导出,就是对一个方法的提取然后提取之后进行导出,导出后就能在其他方法中使用
参数化驱动
如果对title进行参数化 用paramters
在yaml中编写的规则是:
config:
name: testcase description
variables:
user_phone: '18613143458'
password: moFrwx$!kz3DTRm@@*aV
parameters:
title:["demo1","demo2","demo3"]
user:["name1","name2"]
把yaml文件转化为py文件的命令:
hmake har/mubu_create_doc.yml
生成py文件的格式是:
多个参数化的写法
生成pytest的allure报告
默认的报告比较简陋 pytest-html
需要有一个allure插件
pip install “httprunner[allure]”
运行产生报告的命令
hrun testcases --alluredir=allurereports/
allure serve allurereports 查看报告
性能测试
需要安装插件:locust
pip install “httprunner[locust]”
运行压测
locusts -f testcases/mubu_login_test.py
localhost:8089,查看运行情况