一、API测试项目简介
- 项目名称:天气API
- 项目来源:https://www.sojson.com/blog/305.html
- 接口URL:http://t.weather.sojson.com/api/weather/city/+city_code
- 请求方式:GET
- 参数说明:city_code表示城市的Id码,由9为纯数字组成,拼接在以上接口URL的末尾,测试时不需要单独作为参数进行请求
各个城市的city_code码获取地址:http://cdn.sojson.com/_city.json
二、项目API调试
测试案例描述:本次以广州作为被测城市(“city_code”: “101280101”),请求对应城市的天气数据,请求正常情况下,预期将会返回:前一天(yesterday)、当天及未来4天(forecast),一共6天的广州天气数据
拼接成完整的接口URL:http://t.weather.sojson.com/api/weather/city/101280101
1.浏览器调试API
对以上拼接完成的接口URL,在浏览器调试发送请求,响应数据如下:
JSON解析工具①:浏览器自带F12–>Preview
JSON解析工具②:在线JSON校验格式化工具
http://www.bejson.com/
JSON解析工具③:Json在线解析
2.Python IDE调试API
import requests
# 构造接口测试数据
base_url = 'http://t.weather.sojson.com/api/weather/city'
data = {'city_code':'101280101'} # 广州的城市Id码
# 发送请求
r = requests.get(base_url+'/'+data['city_code']) # 拼接接口URL
print(r.url)
print('------------------')
# 将返回结果转换为json类型
response_data = r.json()
# 从返回结果中获取(日期、信息、状态、城市)
print(response_data['date']) # 请求的当天日期
print(response_data['message']) # message信息
print(response_data['status']) # 响应状态码
print(response_data['cityInfo']['city']) # 请求的城市名称
print('------------------')
# 获取当天的天气
print(response_data['data']['forecast'][0]['date']) # 当天日期
print(response_data['data']['forecast'][0]['type']) # 天气状态
print(response_data['data']['forecast'][0]['high']) # 当天最高温
print(response_data['data']['forecast'][0]['low']) # 当天最低温
执行结果
http://t.weather.sojson.com/api/weather/city/101280101
------------------
20181022
Success !
200
广州市
------------------
22日星期一
阴
高温 28.0℃
低温 21.0℃
三、unittest用例封装(集成到unittest)
1.应用背景
以上案例中的调试API,仅仅针对单个场景进了接口调用,而实际的接口测试工作中,需要对不同的参数场景进行测试,同时还需要设置断言、生成测试报告。
2.用例设计
3.代码实现
import unittest
import requests
from time import sleep
# 构造WeatherTest类,继承unittest.TestCase
class WeatherTest(unittest.TestCase):
# 用例执行前的准备工作
def setUp(self):
self.url = 'http://t.weather.sojson.com/api/weather/city'
# 定义测试guangzhou天气的方法
def test_weather_guangzhou(self): # 用例方法需要以test开头,便于执行顺利
'''
Case01-正常存在的city_code值
'''
data = {'city_code':'101280101'}
r = requests.get(self.url+'/'+data['city_code']) # 拼接接口URL
result = r.json() # 将返回结果转换为json类型
# 设置断言
self.assertEqual(result['status'],200) # 状态码的值是数字,非字符串
self.assertEqual(result['message'],'Success !')
self.assertEqual(result['cityInfo']['city'],'广州市')
self.assertEqual(result['cityInfo']['cityId'],'101280101')
sleep(3) # 控制请求的间隔时间,防止过快请求而IP受限制
def test_weather_param_error(self):
'''
Case02-错误的city_code值
'''
data = {'city_code':'666abc'}
r = requests.get(self.url+'/'+data['city_code'])
result = r.json()
self.assertEqual(result['message'],'Request resource not found.')
self.assertEqual(result['status'],404)
sleep(3)
def test_weather_param_non_existent(self):
'''
Case03-不存在的city_code值
'''
data = {'city_code':'123456789'}
r = requests.get(self.url+'/'+data['city_code'])
result = r.json()
self.assertEqual(result['message'],'no_city_id')
self.assertEqual(result['status'],403)
sleep(3)
def test_weather_no_param(self):
'''
Case04-不传入任何city_code值(空)
'''
data = {'city_code':''}
r = requests.get(self.url+'/'+data['city_code'])
result = r.json()
self.assertEqual(result['message'],'Request resource not found.')
self.assertEqual(result['status'],404)
sleep(3)
# 调试WeatherTest类
if __name__ == '__main__':
unittest.main()