(2023-03-29)1. 完成api自动化测试框架的搭建 2. 增加示例的测试用例,包括自动 生成以及手动编写

This commit is contained in:
floraachy
2023-05-17 17:00:38 +08:00
parent f80f054c53
commit 1f06223170
29 changed files with 1433 additions and 1 deletions
+7
View File
@@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
# @Version: Python 3.9
# @Time : 2023/1/12 15:22
# @Author : chenyinhua
# @File : __init__.py
# @Software: PyCharm
# @Desc: 这是文件的描述信息
+36
View File
@@ -0,0 +1,36 @@
import pytest
from case_utils.assert_handle import assert_result
from case_utils.requests_handle import BaseRequest
from loguru import logger
from case_utils.request_data_handle import RequestPreDataHandle
from config.global_vars import GLOBAL_VARS
@pytest.fixture(params=${case_data})
def case(request):
"""
收集用例数据
"""
return request.param
@pytest.mark.${func_title}
@pytest.mark.auto
class ${class_title}Auto:
def ${func_title}_auto(self, case):
logger.info("-----------------------------START-开始执行用例-----------------------------")
logger.debug(f"当前执行的用例数据:{case}")
# 将用例标题title作为全局变量,方便后续写入到测试报告report.description中
GLOBAL_VARS["title"] = case.get("title", "")
if case.get("run", None):
# 获取处理完成的用例数据
case_data = RequestPreDataHandle(case).request_data_handle()
# 发送请求
response = BaseRequest.send_request(case_data)
# 进行断言
assert_result(response, case_data["expected"])
else:
reason = f"标记了该用例为false,不执行\\n"
logger.warning(f"{reason}")
pytest.skip(reason)
logger.info("------------------------------------------用例执行结束------------------------------------------")
+21
View File
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# @Version: Python 3.9
# @Time : 2023/1/31 14:31
# @Author : chenyinhua
# @File : global_vars.py
# @Software: PyCharm
# @Desc: 全局变量
from enum import Enum
# 定义一个全局变量,作用于接口关联数据存储
GLOBAL_VARS = {}
class CaseFileType(Enum):
"""
用例数据可存储文件的类型枚举
"""
YAML = 1
EXCEL = 2
ALL = 0
+48
View File
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
# @Version: Python 3.9
# @Time : 2023/1/31 17:27
# @Author : chenyinhua
# @File : project_path.py
# @Software: PyCharm
# @Desc: 项目相关路径
import os
# ------------------------------------ 项目路径 ----------------------------------------------------#
# 项目根目录
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# 通用模块目录
COMMON_DIR = os.path.join(BASE_DIR, "common_utils")
# 配置模块目录
CONF_DIR = os.path.join(BASE_DIR, "config")
# 数据模块目录
DATA_DIR = os.path.join(BASE_DIR, "data")
# 日志/报告保存目录
OUT_DIR = os.path.join(BASE_DIR, "outputs")
if not os.path.exists(OUT_DIR):
os.mkdir(OUT_DIR)
# 报告保存目录
REPORT_DIR = os.path.join(OUT_DIR, "report")
if not os.path.exists(REPORT_DIR):
os.mkdir(REPORT_DIR)
# 报日志保存目录
LOG_DIR = os.path.join(OUT_DIR, "log")
if not os.path.exists(LOG_DIR):
os.mkdir(LOG_DIR)
# 测试用例模块
CASE_DIR = os.path.join(BASE_DIR, "test_case")
# 手动生成测试用例模块
MANUAL_CASE_DIR = os.path.join(CASE_DIR, "test_manual_case")
if not os.path.exists(MANUAL_CASE_DIR):
os.mkdir(MANUAL_CASE_DIR)
# 自动生成测试用例模块
AUTO_CASE_DIR = os.path.join(CASE_DIR, "test_auto_case")
+54
View File
@@ -0,0 +1,54 @@
/* change pytest-html test report style */
@charset "UTF-8";
body {
font-size: 16px;
}
h1 {
text-align: center;
color: #2084D9;
font-size: 30px;
}
h2 {
font-size: 24px;
color: #2084D9;
}
a {
color: #466AFF;
}
span {
font-size: 20px;
}
#environment td {
padding: 10px;
}
#results-table {
font-size: 16px;
table-layout:fixed;
}
#results-table td{
word-break:break-all;
word-wrap:break-word;
}
#results-table th{
font-size: 20px;
background-color: #2084D9;
color: #FFFFFF;
}
td {
color: #000000;
}
+43
View File
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
# @Version: Python 3.9
# @Time : 2023/1/9 17:08
# @Author : chenyinhua
# @File : settings.py
# @Software: PyCharm
# @Desc: 项目配置文件
# 0代表执行Excel和yaml两种格式的用例, 1 代表 yaml文件,2 用例代表Excel用例
CASE_FILE_TYPE = 0
# 测试报告的定制化信息展示
REPORT_TITLE = "自动化测试报告"
REPORT_NAME = f"apiautotest-report-"
PROJECT_NAME = "GitLink 确实开源"
TESTER = "测试人员:陈银花"
DEPARTMENT = "所属部门: 开源中心"
# 测试数据
test = [
{
# 示例测试环境及示例测试账号
"host": "https://testforgeplus.trustie.net/",
"login": "auotest",
"password": "12345678",
"nickname": "AutoTest",
"user_id": "84954",
"project_id": "",
"project": ""
}
]
live = [
{
"host": "https://www.gitlink.org.cn",
"login": "******",
"password": "******",
"nickname": "******",
"user_id": "******",
"project_id": "",
"project": ""
}
]