前言
我们上一次分享了FastAPI(八十八)实战开发《在线课程学习系统》-- 查看留言列表接口测试和回复留言接口测试,这次我们看下删除留言接口测试,这也是FastAPI接口测试最后一篇分享。
正文
我们看下FastAPI(七十三)实战开发《在线课程学习系统》接口开发-- 删除留言开发的文章,以及前面的接口设计的文章,我们根据需求,进行用例设计。大概的思路如下
代码语言:javascript复制1.未登陆删除,提示未登陆
2.登陆删除不存在的,提示不存在
3.删除不是自己的,删除失败
4.删除自己的留言,可以正常删除
我们看下如何实现的相关的case
代码语言:javascript复制import unittest
from test.userlogin import student_parame,get_students_token
import requests
from test.pyopearmysql import OpearMysql
from config import testplan
class TestViesMessageCase(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.url = testplan '/user/deletemessage'
cls.token = get_students_token(student_parame)
cls.connct=OpearMysql()
@classmethod
def tearDownClass(cls) -> None:
cls.url=''
def setUp(self) -> None:
pass
def tearDown(self) -> None:
self.text=''
def testuser_not_login(self):
reponse=requests.get(self.url)
status=reponse.status_code
reslut=reponse.json()
self.assertEqual(status, 200)
self.assertEqual(reslut['code'], 421)
def testuser_login_id_not(self):
#删除用户不存在
headers = {
"token": self.token
}
selectid = self.connct.run(
"SELECT messages.id FROM messages WHERE senduser in (SELECT id FROM users WHERE username !='liwanle1i' )")
data = {'id': int(selectid[-1][0]) 1}
reponse=requests.get(self.url,params=data,headers=headers)
status=reponse.status_code
reslut=reponse.json()
self.assertEqual(status, 200)
self.assertEqual(reslut['code'], 100901)
self.assertEqual(reslut['message'], '删除留言不存在')
def testuser_login_id_per(self):
#删除失败
headers = {
"token": self.token
}
selectid=self.connct.run("SELECT messages.id FROM messages WHERE senduser in (SELECT id FROM users WHERE username !='liwanle1i' )")
data={'id':selectid[0][0]}
reponse=requests.get(self.url,params=data,headers=headers)
status=reponse.status_code
reslut=reponse.json()
self.assertEqual(status, 200)
self.assertEqual(reslut['code'], 100902)
self.assertEqual(reslut['message'], '权限不足')
def testuser_login_id_success(self):
#删除成功
headers = {
"token": self.token
}
selectid = self.connct.run(
"SELECT messages.id FROM messages WHERE senduser in (SELECT id FROM users WHERE username ='liwanle1i' and status=0 )")
id=(selectid[0][0])
data={'id':id}
reponse=requests.get(self.url,params=data,headers=headers)
status=reponse.status_code
reslut=reponse.json()
self.assertEqual(status, 200)
self.assertEqual(reslut['code'], 200)
self.assertEqual(reslut['message'], '成功')
if __name__ == '__main__':
unittest.main()
这样接口测试就可以完成了。到这里,后续的课程相关的,留给大家做一个动手操作的。
这里我们针对注册的接口进行优化
代码语言:javascript复制import unittest, uuid
import requests
from config import testplan
class FastApiTestUserCreate(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
'''
初始化测试环境
:return:
'''
cls.client = requests
cls.testurl = testplan "/user/create"
@classmethod
def tearDownClass(cls) -> None:
'''
还原测试环境,测试url
:return:
'''
cls.client = None
cls.testurl = ''
def setUp(self) -> None:
'''
初始化参数
:return:
'''
self.parame = {"username": "liwanle1i",
"password": "123456",
"role": "学生",
"age": 19,
'studentnum': 20332}
def tearDown(self) -> None:
'''最后清理参数'''
self.parame.clear()
def test_create_user(self):
'''重复的添加一个数据'''
response = self.client.post(self.testurl, json=self.parame)
code = response.status_code
reslut = response.json()
self.assertEqual(code, 200)
self.assertEqual(reslut['message'], '用户名重复')
self.assertEqual(reslut['code'], 100104)
def test_create_user_new(self):
'''添加一个新的学生'''
self.parame['username'] = str(uuid.uuid1())[:9]
response = self.client.post(self.testurl, json=self.parame)
code = response.status_code
reslut = response.json()
self.assertEqual(code, 200)
self.assertEqual(reslut['code'], 200)
self.assertEqual(reslut['message'], 'success')
def test_create_user_new_tearcher(self):
'''添加一个新的教师'''
self.parame['username'] = str(uuid.uuid1())[:9]
self.parame['role'] = "教师"
self.parame['jobnum'] = 93928
self.parame['sex'] = "女"
response = self.client.post(self.testurl, json=self.parame)
code = response.status_code
reslut = response.json()
self.assertEqual(code, 200)
self.assertEqual(reslut['code'], 200)
self.assertEqual(reslut['message'], 'success')
def test_create_user_new_one(self):
'''测试学生没有填写学籍号'''
self.parame['username'] = str(uuid.uuid1())[:9]
self.parame.pop("studentnum")
response = self.client.post(self.testurl, json=self.parame)
code = response.status_code
reslut = response.json()
self.assertEqual(code, 200)
self.assertEqual(reslut['code'], 100102)
self.assertEqual(reslut['message'], '身份和对应号不匹配')
def test_create_user_new_tearcher_one(self):
'''
测试老师的工作号不填写
'''
self.parame['username'] = str(uuid.uuid1())[:9]
self.parame['role'] = "教师"
self.parame['sex'] = "女"
response = self.client.post(self.testurl, json=self.parame)
code = response.status_code
reslut = response.json()
self.assertEqual(code, 200)
self.assertEqual(reslut['code'], 100102)
self.assertEqual(reslut['message'], '身份和对应号不匹配')
if __name__ == "__main__":
unittest.main()
那么我们把所有的接口测试用例都汇总下。
我们创建一个run.py
然后我们执行下所有的代码
代码语言:javascript复制import unittest
def suite():
import os
path=os.getcwd()
suite=unittest.TestLoader().discover(path, pattern='test*.py', top_level_dir=None)
return suite
if __name__=="__main__":
runner = unittest.TextTestRunner()
runner.run(suite())
这样我们也可以修改测试用例执行。
后面我们可以把BSTestRunner增加历史执行记录展示和重试功能里面的BSTestRunner给引入进来。
代码语言:javascript复制import unittest,os
from test.BSTestRunner import BSTestRunner
def suite():
import os
path=os.getcwd()
suite=unittest.TestLoader().discover(path, pattern='test*.py', top_level_dir=None)
return suite
if __name__=="__main__":
BASH_DIR = "history"
report_path = os.path.join(BASH_DIR, "test.html")
openone = open(report_path, 'w ')
besautiful = BSTestRunner(title="报告",
description="测试报告",
stream=openone,
trynum=2,
filepath=BASH_DIR,
is_show=True)
all=suite()
besautiful.run(all)
最后测试报告如下
接下来,我们需要整理下所有的case,然后对于失败的看下,是否有问题。排除已知问题。到这里,我们分享的接口测试,到这里就算完结了。