python request 基于unittest 请求deom

# coding:utf-8
"""
Compatible for python2.x and python3.x
requirement: pip install requests
"""
import requests
# 请求示例 url 默认请求参数已经做URL编码
from httprunner import response
import unittest
class Case(unittest.TestCase):
    def tearDown(self):
        pass
    def test_01(self):
        url = 'http://120.76.205.241:8000/tools/phone_number_ascription'
        body={
            'phoneNumber':'18301994686',
            'apikey':'k416xoWT4zQTVm2N641LcpUEiZpgRHxEwUhQA5hGHtzSPHI13UHiXkBvdOOFueeN'
        }
        headers = {
        'Accept-Encoding': 'gzip',
        'Connection': 'close'
        }
        r = requests.get(url, params=body,headers=headers)
        resp_obj = response.ResponseObject(r)
        jsons=resp_obj.json
        data=resp_obj.json['data']
        for h in data:
            x='18301994686'
            b = len(h)
            # if x in h:
            #     print('ID手机号返回成功',': ',h[x])
            if x == h['id']:
                print('手机号返回正确',':',h['id'])
            else:
                print('手机号码返回不匹配')
            if  b==5:
                print('测试长度通过')
            else:
                print('字段返回不全')
        self.assertEqual('18301994686',h['id']) # 对比列表ID 是否登陆 这个字符串
        self.assertEqual('上海移动183卡',h['cardType'])
        self.assertIn('location', h) #对比字典里面是否存在这个字段
        self.assertEqual('phone_number_ascription',jsons['appCode'])
    def tearDown(self):
        pass
if __name__ == '__main__':
    unittest.main(verbosity=2)
    # 断言相关实例
    # self.assertTrue(hasattr(resp_obj, 'status _code'))
    # self.assertTrue(hasattr(resp_obj, 'headers'))
    # self.assertTrue(hasattr(resp_obj, 'content'))
    # self.assertIn('Content-Type', resp_obj.headers)
    # self.assertIn('Content-Length', resp_obj.headers)
    # self.assertIn('success', resp_obj.json)

 

posted @ 2018-08-06 23:25  mahaining  阅读(302)  评论(0编辑  收藏  举报