1.pytest
用例发现,用例筛选,用例执行,用例报告,第三方插件。
创建 test_api.py
@pytest.mark.skip 可以跳过当前用例
def test_abc();
assert 1 == 2断言
执行:pytest 启动框架执行用例
pytest -vs 关闭io输出可以看到print代码 和执行过程
参数
mark fixture
2.requests
import requests
def test_baidu()
// 基本用法,统一用法requests.request()
resp = requests.request(
method='get', 必填
url='', 必填
data={} 选填
json={},选填
)
print(resp.content.json())
断言 自动对结果进行判断
assert resp.status == 200
assert resp.page == 1
1.表单参数
web端,参数只能是字符串,参数 有form
requests技巧:
如果data参数是一个字典,则自动将其识别为表单,自动添加请求头
user_info = {
"":"",
"":""
}
def test_api:
resp = requests.request(
method = 'post',
url='',
data=user_info
)
2.JSON参数
def test_api:
resp = requests.request(
method = 'post',
url='',
json=user_info
)
json传参也可以自动添加请求头
3.文件上传
上传方式: body直传,表单上传
请求头
如果上传是files 参数,自动识别为表单文件上传,
def upload:
path = r"E:\images\image"
f = open(path,'r')
resp = requests.request(
method='post',
url='',
files:{"file":f}
)
4.接口关联
流程 从第一个请求中通过jsonpath提取token给到第二个接口。
import jsonpath
def test_token():
token = ''
resp = requests.request(
method = 'post',
url='',
data=user_info
)
token = jsonpath.jsonpath(resp.json(),expr:'$.token')[0]
resp = requests.request(
method = 'post',
url='',
headers={"token":token}
)
5. pytest是一个专注用例执行的框架。如果需要增加新的功能,需要借助第三方插件。比如日志报告,失败重试,发送邮件
I-P-O 输入简单:用例不是代码,只是数据,输出简单:不是文本是网页。处理灵活:记录日志。
yaml文件,
test_name:'',
steps:
-request:
提取变量
-extract
断言
-validate
sanmu run 会执行py文件和yaml文件。执行完成后会生成log文件,取到log日志
sanmu report -s 生成allure测试报告
pytest的插件
创建 requirements.txt文件把插件名字放入文本,输入命令
pip install -r requirements.txt -r代表递归
安装后根据 pip list
6. oytest默认的测试用例规则
python test001
包名和模块名必须以test开头或test结尾,测试用例类必须以Text开头
函数形式
def test_01:
print('')
方法形式
class TestLogin:
def test_02(sekf):
print()
创建pyset.ini文件
[pytest]
testpaths = ./testcases