往期编写的接口测试的代码,测试用例的数据都是分开编写的,这次是将用例集中在一起,详见以下代码:
#列表里嵌套了字典
test_data=[{
"data":{
"email": "******", "password": "******"},"exceped":"logout success..."},
{
"data":{
"email": "******", "password": " "},"exceped":"密码错误"},
{
"data":{
"email": " ", "password": "******"},"exceped":"该用户不存在"},
{
"data":{
"email": "******", "password": "******"},"exceped":"密码错误"}]
class api_login(unittest.TestCase):#登陆接口的用例
def setUp(self):
#登陆
self.login_url = '******'
#查看详情的网址
self.fenlei_url= '***'
def test_normal_login(self):#输入正确的用户
res=HttpRequest().http_request(self.login_url,'post',test_data[0]["data"])
if res.cookies:#如果cookie存在,则更新cookie
setattr(GetDate,"Cookie",res.cookies)
print(res.status_code)
print(res.text)
try:
self.assertIn(test_data[0]["exceped"], res.text)
except AssertionError as e:
print("出错啦,断言错误是{}".format(e)