2023-02-27
接口自动化测试框架介绍
1、测试框架基本能力
项目管理:pip、virtualenv
用例编写:pytest
领域能力:app、web、http
执行调度:pytest、pycharm、shell、Jenkins
测试报告:allure2
2、http测试能力
请求方法构造:get、post、put、delete、head。。
请求体构造:form、json、xml、binary
响应结果分析:status code、response body、json path、xpath
3、Requests框架特点
功能全面:http/https支持全面
使用简单:简单易用,不用关心底层细节
定制性高:借助于hook机制完成通用处理
4、创造第一个接口自动化测试用例
class Test_Demo:
def test_get(self):
r = requests.get(‘https://httpbin.testing-studio.com/get’)
print(r.status_code)
print(r.json())
print(r.text)
assert r.status_code == 200
5、接口请求构造
(1)请求目标构造
r = requests.get(‘https://httpbin.testing-studio.com/get’)#通过requests.请求方法来构造
(2)请求参数构造
get query:path、query
post body:form、结构化请求(json、xml、json rpc)、binary
Get Query请求参数构造
payload = {‘key1’:‘value1’,‘key2’:‘value2’}
r = requests.get(‘https://httpbin.testing-studio.com/get’,params=payload)
Form 请求参数构造
payload = {‘key1’:‘value1’,‘key2’:‘value2’}
r = requests.post(‘https://httpbin.testing-studio.com/post’,data=payload)
文件上传
files = {‘file’:open(‘report.xls’,‘rb’)}
r = requests.post(url,files=files)
header构造
普通的header:
headers = {‘user-agent’:‘my-app/0.0.1’}
r = requests.get(url,headers=headers)
cookie
cookies = dict(cookies_are=‘working’)
r = requests.get(url,cookies=cookies)
6、接口测试断言
(1)响应结果
基本信息:r.rul、r.status_code(响应状态码)、r.headers(响应头)、r.cookies
响应结果:
r.text = r.encoding + r.content
r.json() = r.encoding + r.content + content type json
r.raw.read(10)
对应的请求内容:r.request
7、结构化请求体构造json、xml
json请求体构造:
payload = {‘some’:‘data’}
r = requests.post(url,json=payload)
xml请求体构造:
xml = “”"<?xml version='1.0' encoding='utf-8'?>
headers = {‘Content-Type’:‘application/xml’}
r = requests.post(url,data=xml,headers=headers).text
复杂数据解析:
数据保存:将复杂的xml或者json请求体保存到文件模板中
数据处理:使用mustache、freemaker等工具解析;简单的字符串替换;使用json、xml、api进行结构化解析
数据生成:输出最终结果
8、结构化响应断言json/xml
json断言:
def test_json(self):
url = “https://home.testing-studio.com/categories.json”
r = requests.get(url)
assert r.json()[‘category_list’][‘categories’][1][‘name’] == ‘学习笔记’
json path断言:
def test_json_path(self):
url = “https://home.testing-studio.com/categories.json”
r = requests.get(url)
assert r.json()[‘category_list’][‘categories’][1][‘name’] == ‘学习笔记’
assert jsonpath(r.json(),‘$…name’)[1] == ‘学习笔记’
xml断言:
from requests_xml import XMLSession
session = XMLSession()
r = session.get(url)
r.xml.links
harmcrest断言体系:
框架自带assert体系:assert、assertEqual
Harmcrest体系:assertThat
9、cookie处理
Cookie使用场景:
在接口测试过程中,很多情况下,需要发送的请求附带cookies,才会得到正常的响应的结果。
所以使用Python+requests进行接口自动化测试也是同理,需要在构造接口测试用例时加入cookie。
传递cookie的两种方式:
通过请求头信息传递;通过请求的关键字参数cookie传递
自定义header:
def test_cookie_headers(self):
url = “https://httpbin.testing-studio.com/cookies”
headers = {‘Cookie’:‘working=1’,‘User-Agent’:‘python-requests/2.23.0’}
r = requests.get(url,headers= headers)
print(r.requests.headers)
使用cookies参数:
def test_cookie(self):
url = “https://httpbin.testing-studio.com/cookies”
headers = {‘Cookie’:‘working=1’,‘User-Agent’:‘python-requests/2.23.0’}
cookies = dict(“hogwarts”:“school”)
r = requests.get(url,headers= headers,cookies=cookies)
print(r.requests.headers)
9、form请求
什么是form请求?
数据量不大;数据层级不深的情况;通常以键值对传递
使用方法:requests.post(‘https://httpbin.testing-studio.com/post’,data=data)
练习代码
import requests
from jsonpath import jsonpath
from hamcrest import *
class Test_Demo:
def test_get(self):
r = requests.get('https://httpbin.testing-studio.com/get')
print(r.status_code)
print(r.json())
print(r.text)
assert r.status_code == 200
def test_query(self):
payload = {
'level':'1',
'name':'hogwarts'
}
r = requests.get('https://httpbin.testing-studio.com/get',params=payload)
print(r.text)
assert r.status_code == 200
def test_post_form(self):
payload = {
'level':'1',
'name':'hogwarts'
}
r = requests.post('https://httpbin.testing-studio.com/post',data=payload)
print(r.text)
assert r.status_code == 200
def test_headers(self):
r = requests.get('https://httpbin.testing-studio.com/get',headers={"h":"header demo"})
print(r.text)
print(r.status_code)
print(r.json())
assert r.status_code == 200
assert r.json()['headers']["H"] == "header demo"
def test_post_json(self):
payload = {
'level': 1,
'name': 'hogwarts'
}
r = requests.post('https://httpbin.testing-studio.com/post', json=payload)
print(r.text)
assert r.status_code == 200
assert r.json()['json']['level'] == 1
def test_hogwarts_json(self):
r = requests.get('https://home.testing-studio.com/categories.json')
print(r.text)
assert r.status_code == 200
assert r.json()['category_list']['categories'][1]['name'] == '学习笔记'
print(jsonpath(r.json(),'$..name'))
assert jsonpath(r.json(),'$..name')[1] == '学习笔记'
def test_harmcrest(self):
r = requests.get('https://home.testing-studio.com/categories.json')
print(r.text)
assert r.status_code == 200
assert_that(r.json()['category_list']['categories'][1]['name'],equal_to('学习笔记'))
print(jsonpath(r.json(),'$..name'))
assert jsonpath(r.json(),'$..name')[1] == '学习笔记'
def test_cookie_headers(self):
url = "https://httpbin.testing-studio.com/cookies"
headers = {'Cookie': 'hogwarts=school'}
r = requests.get(url, headers=headers)
print(r.request.headers)
def test_cookie(self):
url = "https://httpbin.testing-studio.com/cookies"
headers = {'User-Agent':'hogwarts'}
cookie_data = {"hogwarts":"school","teacher":"AD"}
r = requests.get(url, headers=headers,cookies=cookie_data)
print(r.request.headers)
def setup_class(self):
#设置代理
self.proxy = {"http":"http://127.0.0.1:8888","https":"http://127.0.0.1:8888"}
def test_form(self):
data = {'hogwarts':'school'}
#通过data参数传入请求体信息
r = requests.post('https://httpbin.testing-studio.com/post',data=data,proxies = self.proxy,verify=False)
print(r.json())
def test_json2(self):
data = {'hogwarts':'school'}
#通过json参数传入请求体信息
r = requests.post('https://httpbin.testing-studio.com/post',json=data,proxies = self.proxy,verify=False)
print(r.json())