Interface automation testing framework purpose
The purpose of test engineers applying the automated testing framework: to enhance the maintainability and ease of use of test scripts (reduce the company's automation training costs, and allow the company's test engineers to carry out automated testing).
The following framework is based on the open documents of the WeChat public platform
Address: https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html
According to different ideas and depths, automated testing frameworks are gradually divided into the following categories :
Linear Script Framework
Modular thinking ==> modular test script framework
Library thinking ==> testing library framework.
Data-driven thinking ==> Data- driven testing framework
Keyword Driven Thought ==> Keyword Driven or Table Driven Test Framework
The integration of the above ideas completes the actual automation of the enterprise ==> hybrid test automation framework
Writing linear test scripts in practice
Interface use case excel;
Step 1. Create a new project named A PI_TEST_FRAME , and create different levels under the project; as shown below
Step 2. According to the level of the interface document, create a new level under the test case layer in the project ; as shown in the figure below
Step 3. Create a new test_get_access_token_api.py file under begin_dev and write the code
Write code:
# 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)
Execute to view the results:
Step 4. According to the user tag management in the development document, create a new test_create_user_tag_api.py file
Write code:
# 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)
When Requests simulates a post request, how to deal with the problem of transcoding when sending json data containing Chinese to the server?
Method 1: as shown below
Method 2: as shown below
Results of the
Continue to create new test_update_user_tag_api.py files and test_delete_user_tag_api.py files; expand by yourself
Step 5. Integrate and execute the use cases together, and write the code in run_api_tests.py under the runner file:
Write code:
# 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)
View execution results:
Step 6. Generate a test report, put the HTMLTestReportCN.py file in the common folder and adjust the code in the run_api_tests.py file;
Write code:
# 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)
View the report after execution:
Practical case
Optical theory is useless, you have to learn to follow along, and you have to do it yourself, so that you can apply what you have learned to practice. At this time, you can learn from some actual combat cases.
If it is helpful to you, please like and collect it to give the author an encouragement. It is also convenient for you to quickly find it next time.
If you don’t understand, please consult the small card below. The blogger also hopes to learn and progress with like-minded testers
At the right age, choose the right position, and try to give full play to your own advantages.
My road of automated test development is inseparable from the plan of each stage along the way, because I like planning and summarizing,
Test and develop video tutorials, study notes and receive portals! ! !