Commit da5c5b95 by gitlab

test-api_V1.1

parent aee36a5b
......@@ -9,19 +9,20 @@
# ---------------------------------------------------------------------------
import pymysql
from apiAutoTest_v3 import log
from apiAutoTest_v3.common.basic_read_ini import ReadIni
from apiAutoTest_v3.common.read_project_ini import ReadProjectIni
class DB:
def __init__(self):
"""链接数据库,获取链接对象和游标对象"""
ini = ReadIni()
# 链接数据库,数据库的连续配置信息存放在项目的配置文件中的,需要使用ReadProjectIni类对象调用get_sql_connect_msg方法
pro_ini = ReadProjectIni()
self.conn = pymysql.connect(
host=ini.get_sql_connect_msg("host"),
port=int(ini.get_sql_connect_msg("port")),
user=ini.get_sql_connect_msg("user"),
password=ini.get_sql_connect_msg("password"),
database=ini.get_sql_connect_msg("database"),
host=pro_ini.get_sql_connect_msg("host"),
port=int(pro_ini.get_sql_connect_msg("port")),
user=pro_ini.get_sql_connect_msg("user"),
password=pro_ini.get_sql_connect_msg("password"),
database=pro_ini.get_sql_connect_msg("database"),
charset="utf8"
)
self.cursor = self.conn.cursor()
......
......@@ -8,24 +8,30 @@
#
# ---------------------------------------------------------------------------
import openpyxl
from apiAutoTest_v3 import log
from apiAutoTest_v3.common.read_json import read_json
from apiAutoTest_v3.common.user_read_ini import ReadIni
from apiAutoTest_v3.common.basic_read_ini import ReadIni as BasicIni
from apiAutoTest_v3.common.read_project_ini import ReadProjectIni
from apiAutoTest_v3.common.read_user_ini import ReadUserIni
from apiAutoTest_v3.data_config.settings import *
# read_excel.py文件的功能:就是获取每个用户的测试数据,并将测试数据存放在一个二维列表中
class ReadExcel:
def __init__(self, username="demo"):
def __init__(self, user_dir_name):
"""获取所有json文件的路径,并读取json文件,再获取excel文件的路径,加载excel的工作簿,并获取工作表的名称,再获取工作表"""
self.ini = ReadIni(username)
case_data_path = self.ini.get_file_path(FILE_CASE)
expect_data_path = self.ini.get_file_path(FILE_EXPECT)
sql_data_path = self.ini.get_file_path(FILE_SQL)
excel_path = self.ini.get_file_path(FILE_EXCEL)
table_name = self.ini.get_table_name(TABLE_kEY)
# 被测系统的域名存放在项目的ini配置文件中,所有需要ReadProjectIni对象,调用get_host方法获取被测系统的域名
self.pro_ini = ReadProjectIni()
# 先读取每个用户的配置文件的对象。ReadUserIni对象。因为每个用户的数据存放的文件信息放在每个用户的ini配置文件中。
user_ini = ReadUserIni(user_dir_name)
# 根据ReadUserIni对象获取每个用户的数据文件路径
case_data_path = user_ini.get_file_path(FILE_CASE)
expect_data_path = user_ini.get_file_path(FILE_EXPECT)
sql_data_path = user_ini.get_file_path(FILE_SQL)
excel_path = user_ini.get_file_path(FILE_EXCEL)
table_name = user_ini.get_table_name(TABLE_kEY)
self.case_data_dict = read_json(case_data_path)
self.expect_data_dict = read_json(expect_data_path)
......@@ -63,7 +69,8 @@ class ReadExcel:
"""根据行号,获取用例的url"""
path = self.__get_cell_value(URL, row)
if path:
return BasicIni().get_host(HOST_KEY) + path
# 被测系统的域名存放在项目的ini配置文件中,所有需要ReadProjectIni对象,调用get_host方法获取被测系统的域名
return self.pro_ini.get_host(HOST_KEY) + path
def case_req_method(self, row):
"""根据行号,获取用例的请求方法"""
......
# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName: test62
# FileName: read_project_ini.py
# Author: lao_zhao
# Datetime: 2024/9/6 17:03
# Description:
#
# ---------------------------------------------------------------------------
import configparser
import os
from apiAutoTest_v3 import log
class ReadProjectIni:
def __init__(self):
"""获取ini文件的路径,并读取ini"""
self.data_config = os.path.join(os.path.dirname(os.path.dirname(__file__)), "data_config")
# 获取项目配置ini文件的路径
ini_path = os.path.join(self.data_config, "project_config.ini")
self.conf = configparser.ConfigParser()
self.conf.read(ini_path, encoding="utf-8")
def get_host(self, key):
"""根据key获取host节点下key对应被测系统的域名"""
try:
return self.conf.get("host", key)
except Exception as e:
log.error(f"方法get_host执行失败,形参key传参为:{key},错误为:{e}")
raise e
def get_sql_connect_msg(self, key):
"""根据key获取sql节点下key对应的数据库链接信息"""
try:
return self.conf.get("sql", key)
except Exception as e:
log.error(f"方法get_sql_connect_msg执行失败,形参key传参为:{key},错误为:{e}")
raise e
# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName: test62
# FileName: read_ini.py
# Author: lao_zhao
# Datetime: 2024/9/4 14:17
# Description:
#
# ---------------------------------------------------------------------------
import configparser
import os
from apiAutoTest_v3 import log
class ReadUserIni:
def __init__(self, user_dir_name):
"""获取ini文件的路径,并读取ini"""
# 获取数据配置层的目录路径
data_config = os.path.join(os.path.dirname(os.path.dirname(__file__)), "data_config")
# 获取存放用户数据的目录路径
self.user_dir = os.path.join(data_config, user_dir_name)
# 拼接用户的ini文件路径
ini_path = os.path.join(self.user_dir, "config.ini")
self.conf = configparser.ConfigParser()
self.conf.read(ini_path, encoding="utf-8")
def get_file_path(self, key):
"""根据key获取file节点下key对应文件的路径"""
try:
log.info(f"执行方法get_file_path为:根据key获取file节点下key对应文件的路径,形参key的传参为{key},")
file_name = self.conf.get("file", key)
except Exception as e:
log.error(f"方法get_file_path执行失败,形参key传参为:{key},错误为:{e}")
raise e
else:
return os.path.join(self.user_dir, file_name)
def get_table_name(self, key):
"""根据key获取table节点下key对应的工作表名"""
try:
return self.conf.get("table", key)
except Exception as e:
log.error(f"方法get_table_name执行失败,形参key传参为:{key},错误为:{e}")
raise e
if __name__ == '__main__':
ini = ReadUserIni("demo")
# D:\Project\PythonDoc\test62\test62\apiAutoTest_v3\data_config\demo\apiAutoTest.xlsx
# D:\Project\PythonDoc\test62\test62\apiAutoTest_v3\data_config\demo\apiAutoTest.xlsx
print(ini.get_file_path("excel"))
\ No newline at end of file
[file]
# 配置数据配置层中文件的名称
excel=apiAutoTest.xlsx
......@@ -10,3 +9,4 @@ sql=sql_data.json
[table]
# 配置工作表名称
table_name=BPM
......@@ -2,11 +2,14 @@
[file]
# 配置数据配置层中文件的名称
excel=张三.xlsx
case=用例数据.json
expect=期望数据.json
sql=sql_data.json
excel=老张的用例管理文件.xlsx
case=老张的用例数据文件.json
expect=老张的期望数据文件.json
sql=老张的sql语句文件.json
[table]
# 配置工作表名称
table_name=张三-认证接口
table_name=登录和添加维度的用例
{
{
"维度管理": {
"添加维度": {
"AddDemSuccess": "dELETE FROM uc_demension WHERE `CODE_`=\"requestsAddDem\";"
},
"根据维度编码删除维度": {
"DeleteDemSuccess": "select ID_ from uc_demension where CODE_=\"requestsAddDem\";"
}
}
}
{
{
"认证接口":{
"登录系统":{
"LoginSuccess": {"username":"超级管理员","account":"admin","userId":"1","expiration":86400,"loginStatus":true},
"LoginErrorUsernameIsNone": {"message":"账号或密码错误"},
"LoginErrorUsernameIsSpecialChar": {"message":"账号或密码错误"},
"LoginErrorUsernameIsLong": {"message":"账号或密码错误"},
"LoginErrorUsernameIsShort": {"message":"账号或密码错误"},
"LoginErrorUsernameIsError": {"message":"账号或密码错误"},
"LoginErrorPasswordIsNone":{"message":"账号或密码错误"},
"LoginErrorPasswordIsSpecialChar": {"message":"账号或密码错误"},
"LoginErrorPasswordIsLong": {"message":"账号或密码错误"},
"LoginErrorPasswordIsShort": {"message":"账号或密码错误"},
"LoginErrorPasswordIsError":{"message":"账号或密码错误"}
},
"刷新token": {
"RefreshSuccess": {"message": "刷新token成功"}
}
},
"维度管理": {
"添加维度": {
"AddDemSuccess": {"message": "添加维度成功"}
},
"根据维度编码获取维度信息": {
"GetDemMessageSuccess": {"isDelete": "0"}
},
"根据维度编码删除维度": {
"DeleteDemSuccess": {"message": "删除维度成功!"}
}
}
}
{
{
"认证接口":{
"登录系统":{
"LoginSuccess": {"username": "admin","password": "bF6N3L93cX8pV6x2yEqxNjwIIPaEOYw9bNI5GuIY4g9MeoFyPFPL5WteHaV0LcxQqmDJWlhuCRMXzAPvrFcxJrA8BgGjJpmB1WMrQrazIJPbWbCfmDit2s2jzn+DRerVlYIFojDM96y24drEniwWzHtaJKiWoc7LGL1csNmokvQ="},
"LoginErrorUsernameIsNone": {"username": "","password": "bF6N3L93cX8pV6x2yEqxNjwIIPaEOYw9bNI5GuIY4g9MeoFyPFPL5WteHaV0LcxQqmDJWlhuCRMXzAPvrFcxJrA8BgGjJpmB1WMrQrazIJPbWbCfmDit2s2jzn+DRerVlYIFojDM96y24drEniwWzHtaJKiWoc7LGL1csNmokvQ="},
"LoginErrorUsernameIsSpecialChar": {"username": "#!$!@#!@#","password": "bF6N3L93cX8pV6x2yEqxNjwIIPaEOYw9bNI5GuIY4g9MeoFyPFPL5WteHaV0LcxQqmDJWlhuCRMXzAPvrFcxJrA8BgGjJpmB1WMrQrazIJPbWbCfmDit2s2jzn+DRerVlYIFojDM96y24drEniwWzHtaJKiWoc7LGL1csNmokvQ="},
"LoginErrorUsernameIsLong": {"username": "adminadminadminadminadminadminadminadminadmin","password": "bF6N3L93cX8pV6x2yEqxNjwIIPaEOYw9bNI5GuIY4g9MeoFyPFPL5WteHaV0LcxQqmDJWlhuCRMXzAPvrFcxJrA8BgGjJpmB1WMrQrazIJPbWbCfmDit2s2jzn+DRerVlYIFojDM96y24drEniwWzHtaJKiWoc7LGL1csNmokvQ="},
"LoginErrorUsernameIsShort": {"username": "a","password": "bF6N3L93cX8pV6x2yEqxNjwIIPaEOYw9bNI5GuIY4g9MeoFyPFPL5WteHaV0LcxQqmDJWlhuCRMXzAPvrFcxJrA8BgGjJpmB1WMrQrazIJPbWbCfmDit2s2jzn+DRerVlYIFojDM96y24drEniwWzHtaJKiWoc7LGL1csNmokvQ="},
"LoginErrorUsernameIsError": {"username": "adminxyz","password": "bF6N3L93cX8pV6x2yEqxNjwIIPaEOYw9bNI5GuIY4g9MeoFyPFPL5WteHaV0LcxQqmDJWlhuCRMXzAPvrFcxJrA8BgGjJpmB1WMrQrazIJPbWbCfmDit2s2jzn+DRerVlYIFojDM96y24drEniwWzHtaJKiWoc7LGL1csNmokvQ="},
"LoginErrorPasswordIsNone": {"username": "admin","password": ""},
"LoginErrorPasswordIsSpecialChar": {"username": "admin","password": "#!$!@#!@#"},
"LoginErrorPasswordIsLong": {"username": "admin","password": "bF6N3L93cX8pV6x2yEqxNjwIIPaEOYw9bNI5GuIY4g9MeoFyPFPL5WteHaV0LcxQqmDJWlhuCRMXzAPvrFcxJrA8BgGjJpmB1WMrQrazIJPbWbCfmDit2s2jznbF6N3L93cX8pV6x2yEqxNjwIIPaEOYw9bNI5GuIY4g9MeoFyPFPL5WteHaV0LcxQqmDJWlhuCRMXzAPvrFcxJrA8BgGjJpmB1WMrQrazIJPbWbCfmDit2s2jznbF6N3L93cX8pV6x2yEqxNjwIIPaEOYw9bNI5GuIY4g9MeoFyPFPL5WteHaV0LcxQqmDJWlhuCRMXzAPvrFcxJrA8BgGjJpmB1WMrQrazIJPbWbCfmDit2s2jznbF6N3L93cX8pV6x2yEqxNjwIIPaEOYw9bNI5GuIY4g9MeoFyPFPL5WteHaV0LcxQqmDJWlhuCRMXzAPvrFcxJrA8BgGjJpmB1WMrQrazIJPbWbCfmDit2s2jznbF6N3L93cX8pV6x2yEqxNjwIIPaEOYw9bNI5GuIY4g9MeoFyPFPL5WteHaV0LcxQqmDJWlhuCRMXzAPvrFcxJrA8BgGjJpmB1WMrQrazIJPbWbCfmDit2s2jzn"},
"LoginErrorPasswordIsShort": {"username": "admin","password": "123456"},
"LoginErrorPasswordIsError": {"username": "admin","password": "bF6N3L93cX8pV6x2yEqxNjwIIPaEOYw9bNI5GuIY4g9MeoFyPFPL5WteHaV0LcxQqmDJWlhuCRMXzAPvrFcxJrA8BgGjJpmB1WMrQrazIJPbWbCfmDit2s2jzn+DRerVlYIFojDM96y24drEniwWzHtaJKiWoc7LGL1csNmokvQ"}
}
},
"维度管理": {
"添加维度": {
"AddDemSuccess": {"code": "requestsAddDem","description": "requestsAddDem","isDefault": 1,"name": "requests添加的维度"}
},
"根据维度编码获取维度信息": {
"GetDemMessageSuccess": {"code": "requestsAddDem"}
},
"根据维度编码删除维度": {
"DeleteDemSuccess": {"ids": "需要更新"}
}
}
}
[host]
# 配置被测系统的域名
bpm_host=http://36.139.193.99:8088
[sql]
host=36.139.193.99
port=3306
user=root
password=Rhrc@2024
database=eip8
[report]
log=log
\ No newline at end of file
......@@ -9,13 +9,14 @@
# ---------------------------------------------------------------------------
import requests
from apiAutoTest_v3.common.basic_read_ini import ReadIni
from apiAutoTest_v3.common.read_project_ini import ReadProjectIni
class RequestMethod:
def __init__(self):
"""关联登录成功的token"""
login_url = ReadIni().get_host("bpm_host") + "/auth"
# 被测系统的域名存放在项目的ini配置文件中,所有需要ReadProjectIni对象,调用get_host方法获取被测系统的域名
login_url = ReadProjectIni().get_host("bpm_host") + "/auth"
login_data = {"username": "admin", "password": "bF6N3L93cX8pV6x2yEqxNjwIIPaEOYw9bNI5GuIY4g9MeoFyPFPL5WteHaV0LcxQ"
"qmDJWlhuCRMXzAPvrFcxJrA8BgGjJpmB1WMrQrazIJPbWbCfmDit2s2jzn+DRerV"
"lYIFojDM96y24drEniwWzHtaJKiWoc7LGL1csNmokvQ="}
......
......@@ -3,7 +3,7 @@
# ProjectName: test62
# FileName: __init__.py
# Author: lao_zhao
# Datetime: 2024/9/4 14:01
# Datetime: 2024/9/6 17:25
# Description:
#
# ---------------------------------------------------------------------------
......@@ -3,9 +3,12 @@
# ProjectName: test62
# FileName: __init__.py
# Author: lao_zhao
# Datetime: 2024/9/4 17:32
# Datetime: 2024/9/4 14:01
# Description:
#
# ---------------------------------------------------------------------------
# user_data_dir_name就是用户存放数据的目录名称
# user_data_dir_name = "demo"
# namespace也是用户存放数据的目录名称
namespace = "demo"
\ No newline at end of file
......@@ -13,6 +13,8 @@ from apiAutoTest_v3 import log
from apiAutoTest_v3.common.read_excel import ReadExcel
from apiAutoTest_v3.test_case.test_demo import namespace
# 创建ReadExcel对象,便于获取用户的测试数据,所有创建ReadExcel对象时需要传入用户存放数据的目录名称
excel = ReadExcel(namespace)
......
# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName: test62
# FileName: __init__.py
# Author: lao_zhao
# Datetime: 2024/9/6 17:30
# Description:
#
# ---------------------------------------------------------------------------
namespace = "lao_zhang"
\ No newline at end of file
# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName: test62
# FileName: conftest.py
# Author: lao_zhao
# Datetime: 2024/9/4 15:43
# Description:
#
# ---------------------------------------------------------------------------
import pytest
from apiAutoTest_v3.common.db import DB
from apiAutoTest_v3.request_method.request_method import RequestMethod
# DB自定义固件
@pytest.fixture(scope="session")
def fix_db():
db = DB()
yield db
db.close()
# RequestMethod自定义固件
@pytest.fixture(scope="session")
def fix_req():
req = RequestMethod()
yield req
[pytest]
; ;开启日志
; log_cli=true
; ;设置日志的级别,如果不设置级别的话,可以设置为NOTSET,如果要设置级别,级别可以有debug,info,warning,error,致命
; log_level=NOTSET
; ;设置日志显示的信息格式
; log_format=%(levelname)s--%(asctime)s--%(message)s
; ;设置日志中时间显示的格式
; log_date_format=%Y-%m-%d %H:%M:%S
; ;每个py文件运行的时候追加的命令
; ;addopts=-vs
; ;设置日志保存的文件
; log_file=../report/log/bpm_test.log
;设置日志保存在文件中的级别
log_file_level=error
;设置日志在文件中的信息格式
log_file_format=%(levelname)s--%(asctime)s--%(message)s
;设置文件日志中时间显示的格式
log_file_date_format=%Y-%m-%d %H:%M:%S
\ No newline at end of file
# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName: test62
# FileName: test_bpm.py
# Author: lao_zhao
# Datetime: 2024/9/4 15:46
# Description:
#
# ---------------------------------------------------------------------------
import pytest
import allure
from apiAutoTest_v3 import log
from apiAutoTest_v3.common.read_excel import ReadExcel
from apiAutoTest_v3.test_case.test_zhang import namespace
# 创建ReadExcel对象,便于获取用户的测试数据,所有创建ReadExcel对象时需要传入用户存放数据的目录名称
excel = ReadExcel(namespace)
class TestBPM:
@allure.epic("BPM项目-Demo")
# @allure.feature("模块名称")
# @allure.story("接口名称")
# @allure.title("用例标题")
# @allure.severity("用例等级")
@pytest.mark.parametrize("module_name, api_name, level, title, url, method, mime, case_data, expect_data, sql_type, sql_data, update_key", excel.get_data())
def test_bpm(self, fix_db, fix_req, module_name, api_name, level, title, url, method, mime, case_data, expect_data, sql_type, sql_data, update_key):
allure.dynamic.feature(module_name)
allure.dynamic.story(api_name)
allure.dynamic.title(title)
allure.dynamic.severity(level)
# 判断sql语句的类型是否为delete
if sql_type == "delete":
# 使用DB类对象调用delete方法执行删除的sql语句,DB类对象===fix_db自定义固件
fix_db.delete(sql_data['delete'])
# 判断sql语句类型是否为select
elif sql_type == "select":
# 使用DB类对象调用select方法执行查询的sql语句, 并获取查询结果,DB类对象===fix_db自定义固件
select_result = fix_db.select(sql_data['select'])
# 将查询结果更新到用例数据中
case_data[update_key] = select_result
# 判断sql语句的类型是否为select|delete 或者为 delete|select
elif sql_type == "select|delete" or sql_type == "delete|select":
# 使用DB类对象调用delete方法执行删除的sql语句,DB类对象===fix_db自定义固件
fix_db.delete(sql_data['delete'])
# 使用DB类对象调用select方法执行查询的sql语句, 并获取查询结果,DB类对象===fix_db自定义固件
select_result = fix_db.select(sql_data['select'])
# 将查询结果更新到用例数据中
case_data[update_key] = select_result
# 使用RequestMethod类对象发送请求
res = fix_req.request_all(req_method=method, req_url=url, req_mime=mime, case_data=case_data)
# 断言
try:
for key in expect_data.keys():
assert expect_data[key] == res.json().get(key)
except AssertionError:
log.error(f"断言失败,接口url为:{url}, 用例数据:{case_data}, 期望数据:{expect_data}, 服务器返回数据:{res.text}")
raise AssertionError("断言失败")
else:
log.info(f"断言成功,接口url为:{url}, 用例数据:{case_data}, 期望数据:{expect_data}, 服务器返回数据:{res.text}")
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment