# 练习02:
# 假设有以下四个测试用例:
# 1,新增一个学院信息,ID字段值为:python01,其他三个字段值任选
# 2,以id(python01)为条件,查询新增的这个学院信息
# 3,更新新增的这个学院信息
# 4,删除新增的这个学院信息
# 应用unnitest框架,编写python代码来实现这四个测试用例的自动化执行,如下:
# 1,编写testcase类并添加断言(判断状态码是否符合预期)
# 2,批量执行这四个测试用例(写在runtest.py中)
# 3,生成HTML格式的测试报告
#导包
import unittest
import requests
class Case01(unittest.TestCase):
def test01(self): #新增
try:
json01= \
{
"data": [
{
"dep_id":"python01",
"dep_name":"python01",
"master_name":"python01",
"slogan":"python01"
}
]
}
url01="http://127.0.0.1:8000/api/departments/"
res01=requests.post(url01,json=json01)
code01=res01.status_code
self.assertEqual(201,code01)
except:
raise
def test02(self): #查看
try:
url02="http://127.0.0.1:8000/api/departments/python01/"
res02=requests.get(url02)
code2=res02.status_code
self.assertEqual(200,code2)
except:
raise
def test03(self): #修改
try:
url03="http://127.0.0.1:8000/api/departments/python01/"
json03= \
{
"data": [
{
"dep_id":"python01",
"dep_name":"hd832y39",
"master_name":"do2ijids",
"slogan":"dhhqdshd"
}
]
}
res03=requests.put(url03,json=json03)
code03=res03.status_code
self.assertEqual(200,code03)
except:
raise
def test04(self): #删除
try:
url04="http://127.0.0.1:8000/api/departments/python01/"
res04=requests.delete(url04)
code04=res04.status_code
self.assertEqual(204,code04)
except:
raise
if __name__ == '__main__':
unittest.main()