1、接口测试框架:
结构说明:
-API用于封装被测系统的接口(用requests模块封装的请求方法)
-TestCase将一个或多个接口封装成测试用例,并使用UnitTest管理测试用例 TestCase可以调用数据库进行数据的校验
-为了方便维护测试数据,可以把测试脚本和测试数据分离开
-通过UnitTest断言接口返回的数据,并生成测试报告
2、框架目录结构
3、实例:
????????打开pycharm创建上述目录
?
在logintestcase(测试用例)文件中写入:
import unittest
from requests import Session
from api.login import LoginApi
class LoginTest(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.login_api = LoginApi()
def setUp(self) -> None:
self.session = Session()
def tearDown(self) -> None:
self.session.close()
def test_login_success(self):
# 请求验证码
response = self.login_api.get_login_verify_code(self.session)
self.assertIn("image",response.headers.get("Content-Type"))
# 验证码识别
# 调用登录方法,发送请求,获取响应数据
res = self.login_api.login(self.session,"13800000002","123456","8888")
res = res.json()
# 断言
self.assertEqual(200,res.status_code)
self.assertEqual(1, res.get("status"))
self.assertEqual("登录成功", res.get("msg"))
?????????在login文件中写入:
class LoginApi(object):
# 初始化
def __init__(self):
self.verify_code_url = "xxxx"
self.login_url = "yyyy"
# 登录过程:
# 1、获取验证码
# 2、填写必要数据:用户名、密码、验证码
# 3、点击登录按钮
def get_login_verify_code(self,session):
return session.get(self.verify_code_url)
def login(self,session,username,password,verify_code):
data = {
"username":username,
"password":password,
"verify_code":verify_code
}
return session.post(url=self.login_url,data=data)
?????????在run_suite文件写入:
import unittest
from scripts import logintestcase
from lib.HTMLTestRunner import HTMLTestRunner
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(logintestcase.LoginTest))
f = open('D:/p6课堂练习/apitest/lib/report/report.html',"wb")
runner = HTMLTestRunner(stream = f ,title="xxx",description="fffff")
runner.run(suite)
将测试报告模板文件复制到lib文件夹下
运行run_suite,结果为:
?
?
?
?
4、测试用例实现参数化
构建json文件存放数据
[
{
"username": "13012345678",
"password": "13579",
"verify_code": "8888",
"status": 0,
"msg": "账号或密码错误"
},
{
"username": "",
"password": "13579",
"verify_code": "8888",
"status": 0,
"msg": "账号或密码错误"
},
{
"username": "13800000005",
"password": "",
"verify_code": "8888",
"status": 0,
"msg": "账号或密码错误"
}
]
?????????在logintestcase(测试用例)文件中写入:
?
# 测试用例
import unittest
from requests import Session
from api.login import LoginApi
import json
from parameterized import parameterized
# 测试用例实现参数化
def build_data():
with open("D:/p6课堂练习/apitest/api/data/data.json","r",encoding='utf-8') as f:
content = f.read()
data = json.loads(content)
# [("username","password","verify_code","status","msg"),()]
d = []
for item in data:
d.append((item["username"],item["password"],item["verify_code"],item["status"],item["msg"]))
return d
class LoginTest(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.login_api = LoginApi()
def setUp(self) -> None:
self.session = Session()
def tearDown(self) -> None:
self.session.close()
@parameterized.expand(build_data)
def test_login_success(self,username,password,verify_code,stauts,msg):
# 请求验证码
response = self.login_api.get_login_verify_code(self.session)
self.assertIn("image",response.headers.get("Content-Type"))
# 验证码识别
# 调用登录方法,发送请求,获取响应数据
res = self.login_api.login(self.session,username,password,verify_code)
res = res.json()
# 断言
self.assertEqual(200,res.status_code)
self.assertEqual(1, res.get("status"))
self.assertEqual("登录成功", res.get("msg"))
5、db数据承载形式实现参数化
构建数据库数据,执行SQL脚本,将数据写入数据库中
修改构建数据函数
?
|