Get请求参数化例子
import unittest
import requests
import ddt
@ddt.ddt
class MyTestCase(unittest.TestCase):
@ddt.data(('qq_37616069', '80376776'), ('zhangchangbin123', '89310491'))
@ddt.unpack
def testGet(self, uid, pid):
# Headers配置
header = {
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-CN,zh;q=0.9",
"cache-control": "max-age=0",
}
res = requests.get('https://blog.csdn.net/'+uid+'/article/details/'+pid, headers=header)
status = res.status_code
print(res.url)
print(status)
self.assertEqual(200, status)
if __name__ == '__main__':
unittest.main()
@ddt.data({'key1': 'value1', 'key2': 'value2'},
{'key1': 'value3', 'key2': 'value4'})
# @ddt.unpack
def test_Get2(self, payload):
# Headers配置
header = {
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-CN,zh;q=0.9",
"cache-control": "max-age=0",
}
r = requests.get("http://httpbin.org/get", headers=header, params=payload)
print(r.url)
status = r.status_code
print(status)
Post 请求
@ddt.data({'key1': 'value1', 'key2': 'value2'},
{'key1': 'value3', 'key2': 'value4'})
def testPost(self, payload):
header = {
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-CN,zh;q=0.9",
"cache-control": "max-age=0",
}
url = 'http://httpbin.org/post'
r = requests.post(url, headers=header, data=payload)
print(r.text)
status = r.status_code
print(status)
self.assertEqual(200, status)