在做接口自动化数据驱动时,发现传进去的data和用postman测试用的数据一样,
但是响应结果不一致。
@data(ADD_DATA['test_add_department_003'],ADD_DATA['test_add_department_004'], ADD_DATA['test_add_department_005'])
@unpack
@write_case_log()
def test_add_department_2(self, req_data, res_key, res_value, status_code, des):
print(self._testMethodName + des)
r = HttpReq.post(self, url=self.url,data=req_data)
# print("状态码为:"+r)
print("响应文本为:" + r.text)
response = json.loads(r.text)
print(response)
self.assertEqual(response[res_key], res_value)
self.assertEqual(r.status_code, status_code)
print("{}执行成功".format(self._testMethodName))
?运行代码得到的响应结果应该和postman的一致,但是不一致,问题在哪里?
原因:在传数据时,没有将data传为json格式。
?解决办法:传data时进行json.dumps(data)
@data(ADD_DATA['test_add_department_003'],ADD_DATA['test_add_department_004'], ADD_DATA['test_add_department_005'])
@unpack
@write_case_log()
def test_add_department_2(self, req_data, res_key, res_value, status_code, des):
print(self._testMethodName + des)
r = ETReq.post(url=self.url, data=json.dumps(req_data))
# print("状态码为:"+r)
print("响应文本为:" + r.text)
response = json.loads(r.text)
print(response)
self.assertEqual(response[res_key][0], res_value)
self.assertEqual(r.status_code, status_code)
print("{}执行成功".format(self._testMethodName))
|