1.导包import requests
import unitest
2. 创建测试类# 类名每一个第一个字母为大写,继承unitest.unitestcase
3.def setup 方法
3.1实例化session对象
3.2 定义验证接口url地址
3.3定义正如接口url地址
4.def teardown
4.1 关闭session对象
5.登录成功test_login_success(self)方法名以test开头
5.1发送验证码请求并断言
5.2发登录请求并断言
6.账号不存在
6.1发送验证码请求并断言
6.2发登录请求并断言
7.密码错误
7.1发送验证码请求并断言
7.2发登录请求并断言
# 获取验证码: http://localhost/index.php?m=Home&c=User&a=verify
# 登录 : http://localhost/index.php?m=Home&c=User&a=do_login
# 导包
import requests
import unittest
# 创建测试类
class TPShopLogin(unittest.TestCase):
def setUp(self):
# 实例化session对象
self.session = requests.Session()
# 定义验证接口url地址
self.url_verify = "http://localhost/index.php? m=Home&c=User&a=verify"
# 定义正如接口url地址
self.url_login = "http://localhost/index.php? m=Home&c=User&a=do_login"
# teardown
def tearDown(self):
# 关闭session对象
self.session.close()
# 登录成功
def test01_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_is_not_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": "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_error(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(-2, response.json().get("status"))
self.assertIn("密码错误", response.json().get("msg"))
用例报告
import unittest
from test10_unittest_tpshop import TPShopLogin
from test12_unittest_params import TPShopLogin2
from tools.HTMLTestRunner import HTMLTestRunner
# 封装测试套件
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TPShopLogin))
suite.addTest(unittest.makeSuite(TPShopLogin2))
# 指定报告路径
report = "./report/report-{}.html".format(time.strftime("%Y%m%d-%H%M%S"))
# 打开文件流
with open(report, "wb") as f:
# 创建HTMLTestRunner运行器
runner = HTMLTestRunner(f, title="tpshop接口测试报告")
#执行测试套件
runner.run(suite)
参数化构造数据from parameterized import parameterized
gdata/login.json数据
[
{
"desc": "登录成功",
"username": "13488888888",
"password": "123456",
"verify_code": "8888",
"status_code": 200,
"status": 1,
"msg": "登陆成功"
},
{
"desc": "账号不存在",
"username": "13488888899",
"password": "123456",
"verify_code": "8888",
"status_code": 200,
"status": -1,
"msg": "账号不存在"
},
{
"desc": "密码错误",
"username": "13488888888",
"password": "error",
"verify_code": "8888",
"status_code": 200,
"status": -2,
"msg": "密码错误"
}
]
# 获取验证码: http://localhost/index.php?m=Home&c=User&a=verify
# 登录 : http://localhost/index.php?m=Home&c=User&a=do_login
# 导包
import json
import requests
import unittest
from parameterized import parameterized
# 构造测试数据
def build_data():
test_data = []
file = "./data/login.json"
with open(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")
status = case_data.get("status")
msg = case_data.get("msg")
test_data.append((username, password, verify_code, status_code, status, msg))
print("test_data=".format(username, password, verify_code, status_code, status, msg))
return test_data
# 创建测试类
class TPShopLogin2(unittest.TestCase):
def setUp(self):
# 实例化session对象
self.session = requests.Session()
# 定义验证接口url地址
self.url_verify = "http://localhost/index.php?m=Home&c=User&a=verify"
# 定义正如接口url地址
self.url_login = "http://localhost/index.php?m=Home&c=User&a=do_login"
# teardown
def tearDown(self):
# 关闭session对象
self.session.close()
# 登录成功
@parameterized.expand(build_data())
def test01_login(self, username, password, verify_code, status_code, status, msg):
# 发送验证码请求并断言
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": username,
"password": password,
"verify_code": verify_code
}
response = self.session.post(url=self.url_login, data=login_data)
print(response.json())
self.assertEqual(status_code, response.status_code)
self.assertEqual(status, response.json().get("status"))
self.assertIn(msg, response.json().get("msg"))