目录
自动化测试框架根据思想理念和深度不同,渐进式的分为以下几种:
步骤1、新建项目名API_TEST_FRAME,在项目的下面新建不同层级;
步骤2、根据接口文档的层级,在项目中的testcase层下新建层级;
步骤3、在begin_dev下新建test_get_access_token_api.py文件,并编写代码
步骤4、按照开发文档中的用户标签管理,新建test_create_user_tag_api.py文件
步骤5、把用例整合一起执行,在runner文件下的run_api_tests.py中编写代码:
步骤6、生成测试报告,把HTMLTestReportCN.py文件放到common文件夹中并在run_api_tests.py文件中调整代码;
接口自动化测试框架目的
测试工程师应用自动化测试框架的目的: 增强测试脚本的可维护性、易用性(降低公司自动化培训成本,让公司的测试工程师都可以开展自动化测试)。
以下框架以微信公众平台开放文档实战
地址:https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html
自动化测试框架根据思想理念和深度不同,渐进式的分为以下几种 :
线性脚本框架
模块化思想 ==>模块化测试脚本框架
库思想 ==>测试库框架。
数据驱动思想 ==>数据 驱动 测试框架
关键字驱动思想 ==>关键字驱动或表驱动的测试框架
上述思想融合完成企业实际自动化 ==>混合测试自动化框架
编写线性测试脚本实战
接口用例excel;

步骤1、新建项目名A PI_TEST_FRAME ,在项目的下面新建不同层级;如下图

步骤 2 、根据接口文档的层级,在项目中的test case 层下新建层级;如下图



步骤 3 、在begin_dev下新建 test_get_access_token_api.py 文件,并编写代码

编写代码:
python
# encoding: utf-8
# @author: Jeffrey
# @file: test_get_access_token_api.py
# @time: 2022/7/24 18:08
# @desc: 导入模块顺序:内置模块、第三方模块、自定义模块
import unittest
import requests
import jsonpath
class TestGetAccessTokenApi(unittest.TestCase):
def setUp(self) -> None:
self.session = requests.session()
def tearDown(self) -> None:
self.session.close()
def test_case_01(self):
'''[api_case_01] 测试获取access_token能否正常调用'''
url_params = {"grant_type":"client_credential",
"appid":"wxf14419077f707856",
"secret":"92a113bd4b5ffdc72144740dc7123c99"}
response = self.session.get(url="https://api.weixin.qq.com/cgi-bin/token",
params = url_params)
# 获取响应json中的access_token的值
actual_result = jsonpath.jsonpath(response.json(), "$.access_token")
print(actual_result)
self.assertTrue(actual_result, "api_case_01 执行失败") #非空,非0 都返回True为真
def test_case_02(self):
'''[api_case_02] 测试获取access_token接口在appid错误时,能否正常处理错误'''
url_params = {"grant_type":"client_credential",
"appid":"wxf14419077f707",
"secret":"92a113bd4b5ffdc72144740dc7123c99"}
response = self.session.get(url="https://api.weixin.qq.com/cgi-bin/token",
params = url_params)
# 获取响应json中的errcode的值,因为jsonpath返回的是列表,故加上下标0
actual_result = jsonpath.jsonpath(response.json(), "$.errcode")[0]
print(actual_result)
self.assertEqual(actual_result,40013, "api_case_02 执行失败")
if __name__ == '__main__':
unittest.main(verbosity=2)
执行查看结果:

步骤4、按照开发文档中的用户标签管理,新建test_create_user_tag_api.py文件

编写代码:
python
# encoding: utf-8
# @author: Jeffrey
# @file: test_create_user_tag_api.py
# @time: 2022/7/24 19:02
# @desc:
import unittest
import requests
import jsonpath
import json
class TestCreateUserTagApi(unittest.TestCase):
def setUp(self) -> None:
self.session = requests.session()
def tearDown(self) -> None:
self.session.close()
def test_case_01(self):
'''[api_case_03] 测试正常进行创建标签接口调用'''
url_params = {"grant_type":"client_credential",
"appid":"wxf14419077f707856",
"secret":"92a113bd4b5ffdc72144740dc7123c99"}
response = self.session.get(url="https://api.weixin.qq.com/cgi-bin/token",
params = url_params)
# 获取响应json中的access_token的值
token_value = jsonpath.jsonpath(response.json(), "$.access_token")[0]
tag_url_params = {"access_token":token_value}
tag_boby = { "tag": { "name":"深圳人2" } }
# 解决中文乱码问题;模拟post请求时,携带json 数据包含中文发送给服务器会转码
# 方式一:json.dumps()
tag_str = json.dumps(tag_boby, ensure_ascii=False)
response = self.session.post(url="https://api.weixin.qq.com/cgi-bin/tags/create",
params = tag_url_params,
data=tag_str.encode('utf-8'))
print(response.json())
# # 方式二:修改requests中的models.py中的源码,修改完后
# response = self.session.post(url="https://api.weixin.qq.com/cgi-bin/tags/create",
# params=tag_url_params,
# json=tag_boby)
# print(response.json())
# 获取响应json的tag的name值,因为jsonpath返回的是列表,故加上下标0
actual_result = jsonpath.jsonpath(response.json(), "$.tag.name")[0]
self.assertEqual(actual_result,"深圳人2", "api_case_03 执行失败")
if __name__ == '__main__':
unittest.main(verbosity=2)
Requests模拟post请求时,如何处理携带json 数据包含中文发送给服务器会转码的问题?
方式一:如下图

方式二:如下图

执行结果

继续新建test_update_user_tag_api.py文件和test_delete_user_tag_api.py文件;自己拓展

步骤5、把用例整合一起执行,在runner文件下的run_api_tests.py中编写代码:

编写代码:
python
# encoding: utf-8
# @author: Jeffrey
# @file: run_api_tests.py
# @time: 2022/7/24 17:52
# @desc:
import os
import unittest
# 获取当前路径
current_path = os.path.dirname(os.path.abspath(__file__))
# 测试用例路径
case_path = os.path.join(current_path, '../testcases')
discover_obj = unittest.defaultTestLoader.discover(start_dir=case_path,
pattern='test*.py')
all_case_suite = unittest.TestSuite()
# 把discover对象发现的用例加载到测试套件中
all_case_suite.addTest(discover_obj)
unittest.main(defaultTest="all_case_suite", verbosity=2)
查看执行结果:

步骤6、生成测试报告,把HTMLTestReportCN.py文件放到common文件夹中并在run_api_tests.py文件中调整代码;

编写代码:
python
# encoding: utf-8
# @author: Jeffrey
# @file: run_api_tests.py
# @time: 2022/7/24 17:52
# @desc:
import os
import unittest
from common import HTMLTestReportCN
# 获取当前路径
current_path = os.path.dirname(os.path.abspath(__file__))
# 测试用例路径
case_path = os.path.join(current_path, '../testcases')
discover_obj = unittest.defaultTestLoader.discover(start_dir=case_path,
pattern='test*.py')
all_case_suite = unittest.TestSuite()
# 把discover对象发现的用例加载到测试套件中
all_case_suite.addTest(discover_obj)
# unittest.main(defaultTest="all_case_suite", verbosity=2)
report_path = os.path.join(current_path, '../reports/result.html')
html_file_obj = open(report_path, 'wb')
html_runner = HTMLTestReportCN.HTMLTestRunner(stream=html_file_obj,
title='接口接口自动化测试',
tester='YOU',
description='学习接口框架')
html_runner.run(all_case_suite)
执行后查看报告:


以下是我收集到的比较好的学习教程资源,虽然不是什么很值钱的东西,如果你刚好需要,可以评论区,留言【777】直接拿走就好了


各位想获取资料的朋友请 点赞 + 评论 + 收藏 ,三连!
三连之后我会在评论区挨个私信发给你们~