目录
一、代码片段
get请求
def test_get(self):
r = requests.get('http://httpbin.testing-studio.com/get')
print(r)
print(r.status_code)
print(r.text)
print(r.json())
assert r.status_code == 200
def test_query(self):
payload = {
"level": 1,
"name": "sevenirbuy"
}
r = requests.get('http://httpbin.testing-studio.com/get', params=payload)
print(r)
print(r.text)
assert r.status_code == 200
post表单请求
def test_post_form(self):
payload = {
"level": 1,
"name": "sevenirbuy"
}
r = requests.post('http://httpbin.testing-studio.com/post', data=payload)
print(r)
print(r.text)
assert r.status_code == 200
请求中添加headers
def test_header(self):
r = requests.get('http://httpbin.testing-studio.com/get', headers={"h": "hi_header"})
print(r.status_code)
print(r.text)
print(r.json())
assert r.status_code == 200
assert r.json()["headers"]["H"] == "hi_header"
json请求
def test_post_json(self):
payload = {
"level": 1,
"name": "sevenirbuy"
}
r = requests.post('http://httpbin.testing-studio.com/post', json=payload)
print(r)
print(r.text)
assert r.status_code == 200
assert r.json()['json']['level'] == 1
xml请求
def test_post_xml(self):
headers = {
"Content-Type": "application/xml"
}
xml = """
<?xml version='1.0' endcoding='utf-8'?>
<a>%</a>
"""
r = requests.post('http://httpbin.testing-studio.com/post', data=xml, headers=headers)
print(r)
print(r.text)
assert r.status_code == 200
jsonpath断言
hamcrest断言
def test_jsonpath_assert(self):
r = requests.get('https://ceshiren.com/categories.json')
print(r)
print(r.text)
print(r.json())
assert r.status_code == 200
assert r.json()["category_list"]["can_create_category"] == False
print(jsonpath(r.json(), '$..name')) # 打印出..name路径下所有的元素
# jsonpath断言
assert jsonpath(r.json(), '$..name')[0] == '提问区'
# hamcrest断言
print(r.json()["category_list"]["categories"][0]["topic_count"])
# 断言目标位置是否包含关键词
assert_that((r.json()["category_list"]["categories"][0]["topic_count"]), equal_to(893))
Hamcrest断言:自动化测试的利器_测试界的飘柔的博客-CSDN博客
headers中添加cookie
def test_headers_cookie(self):
url = "http://httpbin.testing-studio.com/cookies"
header = {"Cookie": "this_cookie=halo"}
r = requests.get(url, headers=header)
print(r.request.headers)
请求中添加cookies参数
def test_cookies(self):
url = "http://httpbin.testing-studio.com/cookies"
header = {'Accept': '*/*'}
cookies_data = {
"this_cookie": "this_cookie=halo",
"this_element": "AD"
}
r = requests.get(url, headers=header, cookies=cookies_data)
print(r.request.headers)
Auth认证
def test_auth(self):
url = "http://httpbin.testing-studio.com/basic-auth/root/12"
# auth =
r = requests.get(url, auth=HTTPBasicAuth("root", "12"))
print(r)
print(r.text)
二、完整代码
import requests
from hamcrest import assert_that, equal_to
from jsonpath import jsonpath
from requests.auth import HTTPBasicAuth
class TestDemo:
"""
get请求
"""
def test_get(self):
r = requests.get('http://httpbin.testing-studio.com/get')
print(r)
print(r.status_code)
print(r.text)
print(r.json())
assert r.status_code == 200
def test_query(self):
payload = {
"level": 1,
"name": "sevenirbuy"
}
r = requests.get('http://httpbin.testing-studio.com/get', params=payload)
print(r)
print(r.text)
assert r.status_code == 200
"""
post表单请求
"""
def test_post_form(self):
payload = {
"level": 1,
"name": "sevenirbuy"
}
r = requests.post('http://httpbin.testing-studio.com/post', data=payload)
print(r)
print(r.text)
assert r.status_code == 200
"""
get请求中添加headers
"""
def test_header(self):
r = requests.get('http://httpbin.testing-studio.com/get', headers={"h": "hi_header"})
print(r.status_code)
print(r.text)
print(r.json())
assert r.status_code == 200
assert r.json()["headers"]["H"] == "hi_header"
"""
json请求
"""
def test_post_json(self):
payload = {
"level": 1,
"name": "sevenirbuy"
}
r = requests.post('http://httpbin.testing-studio.com/post', json=payload)
print(r)
print(r.text)
assert r.status_code == 200
assert r.json()['json']['level'] == 1
"""
xml请求
"""
def test_post_xml(self):
headers = {
"Content-Type": "application/xml"
}
xml = """
<?xml version='1.0' endcoding='utf-8'?>
<a>%</a>
"""
r = requests.post('http://httpbin.testing-studio.com/post', data=xml, headers=headers)
print(r)
print(r.text)
assert r.status_code == 200
"""
jsonpath断言
hamcrest断言
"""
def test_jsonpath_assert(self):
r = requests.get('https://ceshiren.com/categories.json')
print(r)
print(r.text)
print(r.json())
assert r.status_code == 200
assert r.json()["category_list"]["can_create_category"] == False
print(jsonpath(r.json(), '$..name')) # 打印出..name路径下所有的元素
# jsonpath断言
assert jsonpath(r.json(), '$..name')[0] == '提问区'
# hamcrest断言
print(r.json()["category_list"]["categories"][0]["topic_count"])
# 断言目标位置是否包含关键词
assert_that((r.json()["category_list"]["categories"][0]["topic_count"]), equal_to(893))
"""
headers中添加cookie
"""
def test_headers_cookie(self):
url = "http://httpbin.testing-studio.com/cookies"
header = {"Cookie": "this_cookie=halo"}
r = requests.get(url, headers=header)
print(r.request.headers)
"""
请求中添加cookies参数
"""
def test_cookies(self):
url = "http://httpbin.testing-studio.com/cookies"
header = {'Accept': '*/*'}
cookies_data = {
"this_cookie": "this_cookie=halo",
"this_element": "AD"
}
r = requests.get(url, headers=header, cookies=cookies_data)
print(r.request.headers)
"""
Auth认证
"""
def test_auth(self):
url = "http://httpbin.testing-studio.com/basic-auth/root/12"
# auth =
r = requests.get(url, auth=HTTPBasicAuth("root", "12"))
print(r)
print(r.text)