接口返回:
“code”: 200, “msg”: “成功!”, “data”: { “yesterday”: { “date”: “14日星期五”, “high”: “高温 7℃”, “fx”: “无持续风向”, “low”: “低温 4℃”, “fl”: “”, “type”: “阴” }, “city”: “成都”, “aqi”: “89”, “forecast”: [ { “date”: “15日星期六”, “high”: “高温 10℃”, “fengli”: “”, “low”: “低温 2℃”, “fengxiang”: “无持续风向”, “type”: “多云” }, { “date”: “16日星期天”, “high”: “高温 15℃”, “fengli”: “”, “low”: “低温 3℃”, “fengxiang”: “无持续风向”, “type”: “晴” }, { “date”: “17日星期一”, “high”: “高温 14℃”, “fengli”: “”, “low”: “低温 5℃”, “fengxiang”: “无持续风向”, “type”: “晴” }, { “date”: “18日星期二”, “high”: “高温 11℃”, “fengli”: “”, “low”: “低温 6℃”, “fengxiang”: “无持续风向”, “type”: “多云” }, { “date”: “19日星期三”, “high”: “高温 12℃”, “fengli”: “”, “low”: “低温 6℃”, “fengxiang”: “无持续风向”, “type”: “多云” } ], “ganmao”: “昼夜温差较大,较易发生感冒,请适当增减衣服。体质较弱的朋友请注意防护。”, “wendu”: “9” } }
代码:
coding=utf-8
import requests
import unittest
#4.编写测试用例和断言
class TestWeather(unittest.TestCase): '''测试天气预报接口''' # 此注释将展示到测试报告的测试组类
def test_beijin_weather(self): '''查询北京天气预报''' # 此注释将展示到测试报告的用例标题
url = "https://www.apiopen.top/weatherApi"
par = {
"city": "北京", # 城市名
}
r = requests.post(url, params=par)
print(r.text) # 获取返回的结果
result = r.json()['code'] #获取状态码
print(result)
# 断言
self.assertEqual(200, result)
self.assertIn('msg', r.text)
self.assertTrue('北京'in r.text)
def test_nanjin_weather(self):
'''查询南京天气预报''' # 此注释将展示到测试报告的用例标题
url = "https://www.apiopen.top/weatherApi"
par = {
"city": "南京", # 城市名
}
r = requests.post(url, params=par)
print(r.text) # 获取返回的结果
result = r.json()['code']#获取状态码
print(result)
# 断言
self.assertEqual(200, result)
self.assertIn('msg', r.text)
self.assertTrue('南京' in r.text)
if __name__ == "__main__":
unittest.main()
注:本贴主要学习作用,如有侵权,请联系我删帖,并道歉谢谢!
|