Python 接口自动化框架:Pytest+Allure+Yaml+Requests
import requests
import yaml
import allure
class APITestCase:
def __init__(self, url, method, headers=None, params=None, data=None, json=None):
self.url = url
self.method = method
self.headers = headers
self.params = params
self.data = data
self.json = json
def send_request(self):
'''
发送请求,并返回响应结果
'''
response = requests.request(
method=self.method,
url=self.url,
headers=self.headers,
params=self.params,
data=self.data,
json=self.json
)
return response
def assert_status_code(self, response, expected_status_code):
'''
断言响应状态码是否符合预期
'''
assert response.status_code == expected_status_code, f'响应状态码错误,预期为{expected_status_code},实际为{response.status_code}'
def assert_response_text(self, response, expected_text):
'''
断言响应文本是否符合预期
'''
assert expected_text in response.text, f'响应文本错误,预期包含{expected_text},实际为{response.text}'
def assert_response_json(self, response, expected_json):
'''
断言响应json是否符合预期
'''
assert response.json() == expected_json, f'响应json错误,预期为{expected_json},实际为{response.json()}'
class TestAPI:
@allure.title('测试接口A')
@allure.description('测试接口A的功能')
def test_api_a(self):
'''
测试接口A
'''
# 读取yaml文件中的测试数据
with open('testdata.yaml', 'r') as f:
test_data = yaml.safe_load(f)['test_api_a']
# 遍历测试数据
for data in test_data:
# 构造请求参数
url = data['url']
method = data['method']
headers = data['headers']
params = data['params']
data = data['data']
json = data['json']
expected_status_code = data['expected_status_code']
expected_text = data['expected_text']
expected_json = data['expected_json']
# 发送请求
api = APITestCase(url=url, method=method, headers=headers, params=params, data=data, json=json)
response = api.send_request()
# 断言响应结果
api.assert_status_code(response, expected_status_code)
api.assert_response_text(response, expected_text)
api.assert_response_json(response, expected_json)
@allure.title('测试接口B')
@allure.description('测试接口B的功能')
def test_api_b(self):
'''
测试接口B
'''
# 读取yaml文件中的测试数据
with open('testdata.yaml', 'r') as f:
test_data = yaml.safe_load(f)['test_api_b']
# 遍历测试数据
for data in test_data:
# 构造请求参数
url = data['url']
method = data['method']
headers = data['headers']
params = data['params']
data = data['data']
json = data['json']
expected_status_code = data['expected_status_code']
expected_text = data['expected_text']
expected_json = data['expected_json']
# 发送请求
api = APITestCase(url=url, method=method, headers=headers, params=params, data=data, json=json)
response = api.send_request()
# 断言响应结果
api.assert_status_code(response, expected_status_code)
api.assert_response_text(response, expected_text)
api.assert_response_json(response, expected_json)
if __name__ == '__main__':
# 执行测试用例,并生成allure报告
import pytest
pytest.main(['-s', '-v', '--alluredir', './allure_report'])
allure_cmd = 'allure generate ./allure_report -o ./allure_html --clean'
os.system(allure_cmd)
说明:
- 代码使用了 pytest 作为测试框架,Allure 生成测试报告。
- 使用 Yaml 文件管理测试数据,方便维护和扩展。
- 代码包含详细的注释,方便理解和学习。
- 可以根据自己的需求修改代码,添加更多功能和断言。
使用方法:
- 确保已安装 pytest、allure 和 yaml 库。
- 创建一个 testdata.yaml 文件,存放测试数据。
- 运行代码,生成 allure 报告。
- 打开 allure_html 文件夹,查看测试报告。
测试数据示例:
test_api_a:
- url: 'http://example.com/api/a'
method: 'GET'
headers: {}
params: {}
data: {}
json: {}
expected_status_code: 200
expected_text: 'success'
expected_json: {}
test_api_b:
- url: 'http://example.com/api/b'
method: 'POST'
headers: {}
params: {}
data: {}
json: {}
expected_status_code: 201
expected_text: 'created'
expected_json: {}
注意:
- 将代码中的 testdata.yaml 替换为您的测试数据文件路径。
- 将代码中的 allure_report 和 allure_html 替换为您的报告目录路径。
- 将代码中的测试数据替换为您的实际测试数据。
- 根据您的需求调整代码和测试数据。
希望这套框架代码对您有所帮助!如果您有任何问题,请随时提出。
原文地址: https://www.cveoy.top/t/topic/lfkG 著作权归作者所有。请勿转载和采集!