功能测试做长了,老感觉很慌,最近学习了接口测试,书上的例子比较简单,在实际工作了实现了两遍,都是登录功能,流程基本通了,慢慢扩展到jenkins 集成,自动 发送邮件。接口测试需要有接口文档,根据接口文件来创建测试用例,如果没有接口文件,就抓包工具来看一个需要填写那些字段名称。
第一步创建测试用例,代码如下:
import requests,unittest,HTMLTestRunner_PY3
class login(unittest.TestCase):
def setUp(self):
self.url= "替换成你自己的测试地址"
def tearDown(self):
pass
# 正确登录
def test1(self):
form = {"uid": "HTSSAdmin000000", "pwd": "HTSSAdmin000000"}
r = requests.post(self.url, data=form)
self.assertEqual(r.status_code, 200)
# 用户名为空
def test2(self):
form = {"uid": " ", "pwd": "HTSSAdmin000000"}
r = requests.post(self.url, data=form)
self.assertEqual(r.status_code, 200)
# 密码为空
def test3(self):
form = {"uid": "HTSSAdmin000000", "pwd": " "}
r = requests.post(self.url, data=form)
# 用户名错误
def test4(self):
form = {"uid": "HTSSAdmi", "pwd": "HTSSAdmin000000"}
r = requests.post(self.url, data=form)
self.assertEqual(r.status_code, 200)
# 密码错误
def test5(self):
form = {"uid": "HTSSAdmin000000", "pwd": "HTSSA"}
r = requests.post(self.url, data=form)
self.assertEqual(r.status_code, 200)
# 用户名和密码都错误
def test6(self):
form = {"uid": "HTSSAdmin", "pwd": "HTSSAdmin"}
r = requests.post(self.url, data=form)
self.assertEqual(r.status_code, 200)
#用户名输入特殊字符
def test7(self):
form = {"uid": "HTSSAdmin000000", "pwd": "@#@#@##¥#%¥%¥"}
r = requests.post(self.url, data=form)
self.assertEqual(r.status_code, 200)
#密码输入特殊字符
def test8(self):
form = {"uid": "@#@#@##¥#%¥%¥", "pwd": "HTSSAdmin"}
r = requests.post(self.url, data=form)
self.assertEqual(r.status_code, 200)
#用户名输入长文字
def test9(self):
form = {"uid": "HTSSAdmin000000HTSSAdmin000000HTSSAdmin000000HTSSAdmin000000HTSSAdmin000000HTSSAdmin000000HTSSAdmin000000HTSSAdmin000000", "pwd": "HTSSAdmin"}
r = requests.post(self.url, data=form)
self.assertIn("登录错误",r.text)
第二步:创建执行文件,代码如下:
#导入测试用例文件
from yingxinhuitong.hight_new_part.loginPage import login
import unittest, HTMLTestRunner_PY3
# 创建用例套件
def suite():
loginPageCase = unittest.TestSuite()
loginPageCase.addTest(login("test1"))
loginPageCase.addTest(login("test2"))
loginPageCase.addTest(login("test3"))
loginPageCase.addTest(login("test4"))
loginPageCase.addTest(login("test5"))
loginPageCase.addTest(login("test6"))
loginPageCase.addTest(login("test7"))
loginPageCase.addTest(login("test8"))
loginPageCase.addTest(login("test9"))
return loginPageCase
#执行程序
if __name__ == '__main__':
#创建html文件,用于可视化测试报告
with open("高新区登录页面测试.html", "wb") as fi:
runner = HTMLTestRunner_PY3.HTMLTestRunner(stream=fi, title="测试报告", description="详情")
runner.run(suite())