接口自动化测试
APITEST: 接口自动化测试
目录结构介绍:
conf目录:用来存放项目运行环境、执行环境相关的配置参数
testsuite目录:测试用例在此目录编写,pytest默认约定test开头的文件和方法为测试用例,不满足条件的不会被执行,可按照功能模块建立文件夹对测试用例进行分类
utils目录:把与业务无关的实用程序放到此目录,比如自己写的辅助方法
.gitignore文件:git提交忽略文件配置文件
conftest.py文件:pytest的fixture方法可以写在这里,测试用例使用其中的fixture不需要使用import显示引入
pytest.ini文件:可以针对pytest进行一些配置,如日志格式和级别等
requirements.txt文件:把需要安装的python第三方库写入此文件,需要使用该工程时执行pip install -r requirements.txt,可一次性安装全部依赖
runall.py文件:执行全部用例入口,用来给jenkins等CI/CD工具拉起自动化任务使用
data目录:存放测试数据
results: 存放测试结果数据和测试报告
API:存放接口文档
log: 存放日志 '''
详情介绍如何新增接口测试
1、baseapi新增class Yuncheng(BaseApi)
class Yuncheng(BaseApi):
host = get_env_info("yuncheng.host")
def set_url(self):
self.url = "http://"+self.host + self.url+ self.suffix
return self
2、API 文件夹下新建yuncheng.py, 设置请求method,url
from API.base_api import Yuncheng
class YunchengTest(Yuncheng):
method = "POST"
url = "/posts"
suffix = ""
3、conf 配置不同环境请求的header,host
yuncheng:
host: jsonplaceholder.typicode.com
headers:
Content-Type: application/json
4、data 配置请求body,多种请求写list,无body写{}
[
{
"userId": 1,
"title": "云程低代码平台",
"body": "私有化部署、定制化开发、源代码交付、欢迎在线体验。http://www.yunchegnxc.com"
},
{
"userId": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
},
{
"userId": 1,
"title": "qui est esse",
"body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
}
]
5、testsuit 文件夹下testbase 增加header的获取
class TestYuncheng():
headers = get_env_info("yuncheng.headers")
6、测试testsuit 文件夹下新增test_yuncheng.py写接口测试
from API.yuncheng import * # 引进接口
import allure
from utils.common import get_json_data
from loguru import logger
import pytest
import datetime
from testsuite.test_base import TestYuncheng # 引进参数
@allure.feature("测试云程")
class TestCui(TestYuncheng,YunchengTest): # 继承了基础类有了header相关
@allure.story("测试一次性请求")
def test_cuilll(self):
data = get_json_data(r"./data/yuncheng/set_yuncheng.json") # 变化的是body请求体
# QuerySchedule继承baseapi实现了接口请求,run就是发送,res是返回数据
# print("===data=====", data)
# print("请求的header为=========", self.headers)
logger.add("./log/{}.log".format(datetime.date.today()), rotation="00:00")
# logger.info("headers " + str(self.headers))
for body in data:
# logger.info("body " + str(data))
res = YunchengTest().set_url().set_headers(self.headers).set_json(body).run()
# print("===res=====", res.response.json())
logger.info('\n'+"url: " + self.url+'\n' +"headers: " + str(self.headers)+'\n'+"body: " + str(data)+'\n'+"返回数据: " + str(res.response.json()))
pytest.assume(res.response.json()['userId'], "1")
@allure.story("测试多次请求")
# 与上面区别是每份数据是一次用例,上面整体是一次用例
@pytest.mark.parametrize("data", get_json_data(r"./data/yuncheng/set_yuncheng.json"))
def test_cui(self, data):
with allure.step("一步步测试"):
# print("请求的header为=========", self.headers)
# print("===data=====", data)
logger.add("./log/{}.log".format(datetime.date.today()), rotation="00:00")
# logger.info("headers "+str(self.headers))
# logger.info("body "+str(data))
res = YunchengTest().set_url().set_headers(self.headers).set_json(data).run()
# print("===res=====", res.response.json())
logger.info('\n'+"url: " + self.url+'\n' +"headers: " + str(self.headers)+'\n'+"body: " + str(data)+"返回数据: " + str(res.response.json()))
pytest.assume(res.response.json()['userId'], "1")
注:接口不涉及登录,如涉及登录则在fixture,fixture_login 设置登录接口相关
7、runall.py 设置运行用例
import os
import pytest
if __name__ == "__main__":
# pytest.main(["./testsuite/test", "-s", "-v", "--alluredir=./results/data", "--clean-alluredir"])
# os.system('allure generate ./results/data/ -o ./results/report --clean')
# os.system('allure open -h 127.0.0.1 -p 8883 ./results/report')
pytest.main(["-vs", "./testsuite/test/test_yuncheng.py"])