美文网首页
使用python实现简单接口测试实例

使用python实现简单接口测试实例

作者: test小星星 | 来源:发表于2018-10-25 18:43 被阅读41次

说明:
此次接口测试实例是基于天气API
所需相关

  • python3
  • unittest框架
  • requests模块

测试API

{
    "status": 200,
    "data": {
        "wendu": "29",
        "ganmao": "各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。",
        "forecast": [
            {
                "fengxiang": "南风",
                "fengli": "3-4级",
                "high": "高温 32℃",
                "type": "多云",
                "low": "低温 17℃",
                "date": "16日星期二"
            },
            {
                "fengxiang": "南风",
                "fengli": "微风级",
                "high": "高温 34℃",
                "type": "晴",
                "low": "低温 19℃",
                "date": "17日星期三"
            },
            {
                "fengxiang": "南风",
                "fengli": "微风级",
                "high": "高温 35℃",
                "type": "晴",
                "low": "低温 22℃",
                "date": "18日星期四"
            },
            {
                "fengxiang": "南风",
                "fengli": "微风级",
                "high": "高温 35℃",
                "type": "多云",
                "low": "低温 22℃",
                "date": "19日星期五"
            },
            {
                "fengxiang": "南风",
                "fengli": "3-4级",
                "high": "高温 34℃",
                "type": "晴",
                "low": "低温 21℃",
                "date": "20日星期六"
            }
        ],
        "yesterday": {
            "fl": "微风",
            "fx": "南风",
            "high": "高温 28℃",
            "type": "晴",
            "low": "低温 15℃",
            "date": "15日星期一"
        },
        "aqi": "72",
        "city": "北京"
    },
    "message": "OK"
}

实例

用例设计

测试场景 用例描述 期望结果
正常参数 传入正常的参数 返回:Success提示信息。
异常参数 传入数字,特殊字符等 返回:Check the parameters提示信息。
参数为空 不传参数 返回:Check the parameters提示信息。

代码实现

import unittest
import requests
from urllib import parse
from time import sleep

class WeatherTest(unittest.TestCase):
    def setUp(self):
        # 接口地址
        self.url = 'https://www.sojson.com/open/api/weather/json.shtml'

        # 代理设置,避免ip被封
        # 根据协议类型,选择不同的代理
        proxies = {
            "http": "http://123.134.156.279:8888",
            "https": "http://123.134.156.279:8888",
        }

    def test_weather_sh(self):
        '''测试上海天气'''

        data = {'city': '上海'}
        # 中文需要把传入的参数对转换为url标准格式
        city = parse.urlencode(data).encode('utf-8')
        # 发送请求
        r = requests.get(self.url, params=city) # 如果需要代理再加个参数proxies
        result = r.json()
        #断言
        self.assertEqual(result['status'], 200)
        self.assertEqual(result['message'], 'Success !')
        self.assertEqual(result['city'], '北京')
        # 设置间隔时间,避免ip被封
        sleep(3)

    def test_weather_param_error(self):
        '''异常参数'''

        data = {'city': '343sad'}
        r = requests.get(self.url, params=city)
        result = r.json()
        # 断言
        self.assertEqual(result['message'], 'Check the parameters.')
        sleep(3)

    def test_weather_param_null(self):
        '''参数为空'''

        r = requests.get(self.url)
        result = r.json()
        # 断言
        self.assertEqual(result['status'], 400)
        self.assertEqual(result['message'], 'Check the parameters.')
        sleep(3)

if __name__ == '__main__':
    unittest.main()

好了,一个简单的接口测试实例就先到这了。通过代码可以发现很多代码都是重复的,其实还可以继续优化,实现数据驱动。等下次再分享吧。

相关文章

网友评论

      本文标题:使用python实现简单接口测试实例

      本文链接:https://www.haomeiwen.com/subject/fvjctqtx.html