码迷,mamicode.com
首页 > 编程语言 > 详细

python request 基于unittest 请求deom

时间:2018-08-07 00:25:15      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:上海   stc   nec   key   port   实例   url   tool   6.2   


# 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)

python request 基于unittest 请求deom

标签:上海   stc   nec   key   port   实例   url   tool   6.2   

原文地址:https://www.cnblogs.com/mahaining/p/9434207.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!