使用unittest框架进行接口测试
直接上代码吧,就是为了能添加一个断言
import requests
import unittest
class Test(unittest.TestCase):
def setUp(self):
print("开始")
def tearDown(self):
print("结束")
def test01(self):
#定义变量
url = "http://web.juhe.cn:8080/environment/water/river"
para = {"river": "长江流域", "key": "5877d742cc7ebb0d1d9f65c88329cdf4"}
r=requests.get(url,para)
res=r.json()
print(res)
#断言
self.assertEqual(res["reason"],"SUCCESSED!")
if __name__ == '__main__':
unittest.main()
运行结果:
Process finished with exit code 0
开始
Ran 1 test in 0.302s
{‘resultcode’: ‘200’, ‘reason’: ‘SUCCESSED!’
OK
结束