import unittest,requests
from AddStu.help_method import *
from AddStu.database import *
class TestPostStu(unittest.TestCase):
test_status = 'smoke_test1'
# 类执行前,设置类的初始化条件
@classmethod
def setUpClass(cls):
# 连接sqlite数据库,创建游标
cls.conn, cls.cur = create_conn_and_cur('C:\\Users\Administrator\Documents\\Navicat\MySQL\servers\mysite\mysite.sql')
# 类执行后,销毁类的初始化环境
@classmethod
def tearDownClass(cls):
# 关闭连接和游标
close_conn_and_cur(cls.conn, cls.cur)
# 钩子方法: 测试用例级别的初始化测试环境,用例执行前被自动调用
def setUp(self):
# 实例化一个session对象,来保存会话
self.se = requests.Session()
# 添加登录信息
self.data = login('hzdltest', 'hzdltest')
# 创建学生数据
self.data1 = create_random_stu()
# print(self.data1)
# 钩子方法: 测试用例级别的销毁测试环境,用例执行后被自动调用
def tearDown(self):
# # 删除数据库里,stu_id>18的数据
# execute_other(self.conn,self.cur,'delete from hello_student where stu_id > 18;')
# 关闭session对象
self.se.close()
# 测试用例: 查询学生成功
def test_get_event_success_4000(self):
self.se.post('http://127.0.0.1:8000/stu/loginsys/', data=self.data)
r1 = self.se.post('http://127.0.0.1:8000/stu/addstu/', data=self.data1)
# print(r1.json())
r2 = self.se.post('http://127.0.0.1:8000/stu/querystu/',data={'stu_id':r1.json()['stu_id']})
print(r2.json())
# 断言响应状态码和业务状态码
assert_code(r2, 200, 4000)
# 断言响应返回的数据
response_data = r2.json()['data'][0]
response_data.pop('stu_reg_time')
self.assertDictEqual(response_data,self.data1)
# # 断言数据库数据
name = r2.json()['data'][0]['stu_name']
assert_values(self.cur, self.data1, f'select * from hello_student where stu_name = {name}')
# 测试用例: 查询学生失败4001
@unittest.skipIf(test_status == 'smoke_test', '通过性验证不必测!')
def test_get_event_failed_4001(self):
self.se.post('http://127.0.0.1:8000/stu/loginsys/', data=self.data)
self.se.post('http://127.0.0.1:8000/stu/addstu/', data=self.data1)
self.phone = None
r2 = self.se.post('http://127.0.0.1:8000/stu/querystu/',data=self.phone)
# 断言响应状态码和业务状态码
assert_code(r2, 200, 4001)
# 测试用例: 查询学生失败4002
@unittest.skipIf(test_status == 'smoke_test', '通过性验证不必测!')
def test_get_event_failed_4002(self):
self.se.post('http://127.0.0.1:8000/stu/loginsys/', data=self.data)
self.se.post('http://127.0.0.1:8000/stu/addstu/', data=self.data1)
self.phone = int(self.data1['stu_phone'])-1
r2 = self.se.post('http://127.0.0.1:8000/stu/querystu/',data=self.phone)
# 断言响应状态码和业务状态码
assert_code(r2, 200, 4002)
if __name__ == '__main__':
unittest.main()
TestPostStu
最新推荐文章于 2024-11-02 20:25:41 发布