# 封装被测试系统接口
# 定义接口类
class LoginAPI:
# 初始化
def __init__(self):
self.url_verify = "http://localhost/index.php?m=Home&c=User&a=verify"
self.url_login = "http://localhost/index.php?m=Home&c=User&a=do_login"
# 获取验证码接口
def get_verify_code(self, session):
return session.get(self.url_verify)
# 登录接口
def login(self, session, username, password, verify_code):
login_data = {
"username": username,
"password": password,
"verify_code": verify_code
}
return session.post(url=self.url_login, data=login_data)
# 导包
import requests
import unittest from api.login
import LoginAPI
# 定义测试类
class TestLoginAPI(unittest.TestCase):
# 前置处理
def setUp(self):
self.login_api = LoginAPI()
self.session = requests.Session()
# 后置处理
def tearDown(self):
if self.session:
self.session.close()
# 定义测试方法
# 登录成功
# 登录成功
def test01_login_success(self):
# 发送验证码请求并断言
response = self.session.get(url=self.url_verify)
self.assertEqual(200, response.status_code)
self.assertIn("image", response.headers.get("Content-Type"))
# 发登录请求并断言
login_data = {
"username": "13488888888",
"password": "123456",
"verify_code": "8888"
}
response = self.session.post(url=self.url_login, data=login_data)
print(response.json())
self.assertEqual(200, response.status_code)
self.assertEqual(1, response.json().get("status"))
self.assertIn("登陆成功", response.json().get("msg"))
# 账号不存在
def test02_user_isnot_exist(self):
# 发送验证码请求并断言
response = self.session.get(url=self.url_verify)
self.assertEqual(200, response.status_code)
self.assertIn("image", response.headers.get("Content-Type"))
# 发登录请求并断言
login_data = {
"username": "13488888899",
"password": "123456",
"verify_code": "8888"
}
response = self.session.post(url=self.url_login, data=login_data)
print(response.json())
self.assertEqual(200, response.status_code)
self.assertEqual(-1, response.json().get("status"))
self.assertIn("账号不存在", response.json().get("msg"))
# 密码错误
def test03_password_exist(self):
# 发送验证码请求并断言
response = self.session.get(url=self.url_verify)
self.assertEqual(200, response.status_code)
self.assertIn("image", response.headers.get("Content-Type"))
# 发登录请求并断言
login_data = {
"username": "13488888888",
"password": "error",
"verify_code": "8888"
}
response = self.session.post(url=self.url_login, data=login_data)
print(response.json())
self.assertEqual(200, response.status_code)
self.assertEqual(-2, response.json().get("status"))
self.assertIn("密码错误", response.json().get("msg"))
# 导包
import time
import unittest from scripts.test01_login
import TestLoginAPI from tools.HTMLTestRunner
import HTMLTestRunner
# 封装测试套件
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestLoginAPI))
# 指定报告路径
report = "./report/report-{}.html".format(time.strftime("%Y%m%d-%H%M%S"))
# 打开文件流
with open(report, "wb") as f:
# 创建HTMLTestRunner执行器
runner = HTMLTestRunner(f, title="接口测试报告")
# 执行测试套件
runner.run(suite)
# 导包
import json
import requests
import unittest from api.login
import LoginAPI from parameterized
import parameterized
# 构造测试数据
def build_data():
json_file = "../data/login.json"
test_data = []
with open(json_file, encoding="utf-8") as f:
json_data = json.load(f)
for case_data in json_data:
username = case_data.get("username")
password = case_data.get("password")
verify_code = case_data.get("verify_code")
status_code = case_data.get("status_code")
content_type = case_data.get("content_type")
status = case_data.get("status")
msg = case_data.get("msg")
test_data.append((username, password, verify_code, status_code, content_type, status, msg))
print("test_data = {}".format((username, password, verify_code, status_code, content_type, status, msg)))
return test_data
# 定义测试类
class TestLoginAPI(unittest.TestCase):
# 前置处理
def setUp(self):
self.login_api = LoginAPI()
self.session = requests.Session()
# 后置处理
def tearDown(self):
if self.session:
self.session.close()
# 定义测试方法
@parameterized.expand(build_data)
def test01_login(self, username, password, verify_code, status_code, content_type, status, msg):
# 调用验证码接口
response = self.login_api.get_verify_code(self.session)
# 断言
self.assertEqual(status_code, response.status_code)
self.assertIn(content_type, response.headers.get("Content-Type"))
# 调用登录接口
response = self.login_api.login(self.session, username, password, verify_code)
print(response.json())
self.assertEqual(status_code, response.status_code)
self.assertEqual(status, response.json().get("status"))
self.assertIn(msg, response.json().get("msg"))