解决读取用例数据自动生成测试方法,只能读取一级目录的问题。目前支持data目录存在多级,只要以test_开头的用例数据(excel/yaml)都能被读取到。如test_demo.yaml文件处于data/projects/project目录下,也可以读取到
This commit is contained in:
+32
-30
@@ -14,28 +14,27 @@ from config.global_vars import CaseFileType
|
|||||||
from config.settings import CASE_FILE_TYPE
|
from config.settings import CASE_FILE_TYPE
|
||||||
from string import Template
|
from string import Template
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
from common_utils.files_handle import get_files
|
||||||
# 定义生成的测试用例的模板
|
|
||||||
with open(file=os.path.join(CONF_DIR, "case_template.txt"), mode="r", encoding="utf-8") as f:
|
|
||||||
case_template = f.read()
|
|
||||||
|
|
||||||
|
|
||||||
def get_case_data():
|
def get_case_data():
|
||||||
"""
|
"""
|
||||||
根据配置文件,从指定类型文件中读取用例数据,并调用生成用例文件方法,生成用例文件
|
根据配置文件,从指定类型文件中读取用例数据,并调用生成用例文件方法,生成用例文件
|
||||||
"""
|
"""
|
||||||
|
cases = []
|
||||||
# 判断配置文件里面CASE_DATA_TYPE,判断用例数据是从excel还是yaml文件中读取
|
# 判断配置文件里面CASE_DATA_TYPE,判断用例数据是从excel还是yaml文件中读取
|
||||||
# 从excel中读取用例数据
|
# 从excel中读取用例数据
|
||||||
if CASE_FILE_TYPE == CaseFileType.EXCEL.value:
|
if CASE_FILE_TYPE == CaseFileType.EXCEL.value:
|
||||||
cases = []
|
# 在用例数据"DATA_DIR"目录中寻找后缀是xlsx, xls的文件
|
||||||
# 在用例数据data_path目录中寻找后缀是xlsx的文件
|
files = get_files(target=DATA_DIR, start="test_", end=".xlsx") + get_files(target=DATA_DIR, start="test_",
|
||||||
for file in [excel for excel in os.listdir(DATA_DIR) if os.path.splitext(excel)[1] in [".xlsx", "xls"]]:
|
end=".xls")
|
||||||
# 判断只有以test_开头的excel才生成测试用例py文件
|
for file in files:
|
||||||
if file.startswith("test_"):
|
# 这里file文件绝对路径,filename才是文件名称
|
||||||
|
filename = os.path.basename(file)
|
||||||
# 读取excel文件中的用例数据,存储到data中
|
# 读取excel文件中的用例数据,存储到data中
|
||||||
data = ReadExcel(os.path.join(DATA_DIR, file)).read()
|
data = ReadExcel(file).read()
|
||||||
# 将用例数据的名称作为测试用例文件名称
|
# 将用例数据的名称作为测试用例文件名称
|
||||||
func_name = os.path.splitext(file)[0]
|
func_name = os.path.splitext(filename)[0]
|
||||||
# 测试用例test_demo.py的类名是TestDemo
|
# 测试用例test_demo.py的类名是TestDemo
|
||||||
class_name = func_name.split("_")[0].title() + func_name.split("_")[1].title()
|
class_name = func_name.split("_")[0].title() + func_name.split("_")[1].title()
|
||||||
# 调用gen_case方法生成测试用例test_demo.py
|
# 调用gen_case方法生成测试用例test_demo.py
|
||||||
@@ -46,16 +45,15 @@ def get_case_data():
|
|||||||
return cases
|
return cases
|
||||||
# 从yaml中读取用例数据
|
# 从yaml中读取用例数据
|
||||||
elif CASE_FILE_TYPE == CaseFileType.YAML.value:
|
elif CASE_FILE_TYPE == CaseFileType.YAML.value:
|
||||||
cases = []
|
# 在用例数据"DATA_DIR"目录中寻找后缀是yaml, yml的文件
|
||||||
# 在用例数据data_path目录中寻找后缀是yaml/yml的文件
|
files = get_files(target=DATA_DIR, start="test_", end=".yaml") + get_files(target=DATA_DIR, start="test_",
|
||||||
for file in [yaml for yaml in os.listdir(DATA_DIR) if
|
end=".yml")
|
||||||
os.path.splitext(yaml)[1] in [".yaml", ".yml"]]:
|
for file in files:
|
||||||
# 判断只有以test_开头的yaml才生成测试用例py文件
|
filename = os.path.basename(file)
|
||||||
if file.startswith("test_"):
|
|
||||||
# 读取yaml/yml文件中的用例数据,存储到data中
|
# 读取yaml/yml文件中的用例数据,存储到data中
|
||||||
data = HandleYaml(os.path.join(DATA_DIR, file)).read_yaml
|
data = HandleYaml(file).read_yaml
|
||||||
# 将用例数据的名称作为测试用例文件名称
|
# 将用例数据的名称作为测试用例文件名称
|
||||||
func_name = os.path.splitext(file)[0]
|
func_name = os.path.splitext(filename)[0]
|
||||||
# 测试用例test_demo.py的类名是TestDemo
|
# 测试用例test_demo.py的类名是TestDemo
|
||||||
class_name = func_name.split("_")[0].title() + func_name.split("_")[1].title()
|
class_name = func_name.split("_")[0].title() + func_name.split("_")[1].title()
|
||||||
# 调用gen_case方法生成测试用例test_demo.py
|
# 调用gen_case方法生成测试用例test_demo.py
|
||||||
@@ -65,19 +63,20 @@ def get_case_data():
|
|||||||
logger.debug(f"从yaml中读取到的用例数据是:{cases}")
|
logger.debug(f"从yaml中读取到的用例数据是:{cases}")
|
||||||
return cases
|
return cases
|
||||||
else:
|
else:
|
||||||
# 从excel以及yaml/yml文件中读取用例数据
|
# 在用例数据"DATA_DIR"目录中寻找后缀是xlsx,xls, yaml, yml的文件
|
||||||
cases = []
|
files = get_files(target=DATA_DIR, start="test_", end=".xlsx") + get_files(target=DATA_DIR, start="test_",
|
||||||
for file in [excel for excel in os.listdir(DATA_DIR) if
|
end=".xls") + get_files(
|
||||||
os.path.splitext(excel)[1] in [".yaml", ".yml", ".xlsx", "xls"]]:
|
target=DATA_DIR, start="test_", end=".yaml") + get_files(target=DATA_DIR, start="test_",
|
||||||
# 判断只有以test_开头的yaml才生成测试用例py文件
|
end=".yml")
|
||||||
if file.startswith("test_"):
|
for file in files:
|
||||||
if os.path.splitext(file)[1] == ".xlsx":
|
filename = os.path.basename(file)
|
||||||
data = ReadExcel(os.path.join(DATA_DIR, file)).read()
|
if os.path.splitext(file)[1] == ".xlsx" or os.path.splitext(file)[1] == ".xls":
|
||||||
func_name = os.path.splitext(file)[0]
|
data = ReadExcel(file).read()
|
||||||
|
func_name = os.path.splitext(filename)[0]
|
||||||
cases.extend(data)
|
cases.extend(data)
|
||||||
else:
|
else:
|
||||||
data = HandleYaml(os.path.join(DATA_DIR, file)).read_yaml
|
data = HandleYaml(file).read_yaml
|
||||||
func_name = os.path.splitext(file)[0]
|
func_name = os.path.splitext(filename)[0]
|
||||||
cases.extend(data)
|
cases.extend(data)
|
||||||
|
|
||||||
class_name = func_name.split("_")[0].title() + func_name.split("_")[1].title()
|
class_name = func_name.split("_")[0].title() + func_name.split("_")[1].title()
|
||||||
@@ -96,6 +95,9 @@ def gen_case_file(func_name, case_data, class_name):
|
|||||||
"""
|
"""
|
||||||
string.Template是将一个string设置为模板,通过替换变量的方法,最终得到想要的string。
|
string.Template是将一个string设置为模板,通过替换变量的方法,最终得到想要的string。
|
||||||
"""
|
"""
|
||||||
|
# 定义生成的测试用例的模板
|
||||||
|
with open(file=os.path.join(CONF_DIR, "case_template.txt"), mode="r", encoding="utf-8") as f:
|
||||||
|
case_template = f.read()
|
||||||
my_case = Template(case_template).safe_substitute({"case_data": case_data,
|
my_case = Template(case_template).safe_substitute({"case_data": case_data,
|
||||||
"func_title": func_name,
|
"func_title": func_name,
|
||||||
"class_title": class_name})
|
"class_title": class_name})
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# @Time : 2023/3/30 22:34
|
||||||
|
# @Author : Flora.Chen
|
||||||
|
# @File : files_handle.py
|
||||||
|
# @Software: PyCharm
|
||||||
|
# @Desc: 处理文件相关操作
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def get_files(target, start=None, end=None):
|
||||||
|
"""
|
||||||
|
@param: target: 目标文件绝对路径
|
||||||
|
@param: start: 以什么开头,默认为空
|
||||||
|
@param: end: 以什么结尾,默认为空
|
||||||
|
获取目录下所有的文件,以列表的形式返回
|
||||||
|
"""
|
||||||
|
if os.path.isfile(target):
|
||||||
|
return []
|
||||||
|
# files返回j经过处理的文件列表
|
||||||
|
files = []
|
||||||
|
# dirpath:表示获取的目录的路径,以string形式返回值。
|
||||||
|
# dirnames: 包含了当前dirpath路径下所有的子目录名字(不包含目录路径),以列表形式返回值。
|
||||||
|
# filenames:包含了当前dirpath路径下所有的非目录子文件的名字(不包含目录路径)。
|
||||||
|
for dirpath, dirnames, filenames in os.walk(target):
|
||||||
|
for filename in filenames:
|
||||||
|
# 如果"start"和"end"都有值
|
||||||
|
if start and end:
|
||||||
|
# filename是以"start"且filename是以"end"结尾,则追加到files
|
||||||
|
if filename.startswith(start) and filename.endswith(end):
|
||||||
|
files.append(os.path.abspath(os.path.join(dirpath, filename)))
|
||||||
|
# 或者如果"start"有值,filename是以"start"开头,则追加到files
|
||||||
|
elif start and (not end):
|
||||||
|
if filename.startswith(start):
|
||||||
|
files.append(os.path.abspath(os.path.join(dirpath, filename)))
|
||||||
|
# 或者如果"end"有值,且filename是以"end"结尾,则追加到files
|
||||||
|
elif end and (not start):
|
||||||
|
if filename.endswith(end):
|
||||||
|
files.append(os.path.abspath(os.path.join(dirpath, filename)))
|
||||||
|
else:
|
||||||
|
files.append(os.path.abspath(os.path.join(dirpath, filename)))
|
||||||
|
# 判断files列表是否为空,不为空则返回files,为空则返回all_files
|
||||||
|
return files
|
||||||
|
|
||||||
|
|
||||||
|
def get_newest_file(dir_path):
|
||||||
|
"""
|
||||||
|
获取目录下最新的文件
|
||||||
|
"""
|
||||||
|
if os.path.isfile(dir_path):
|
||||||
|
return None
|
||||||
|
|
||||||
|
# 获取目录下所有文件
|
||||||
|
files = os.listdir(dir_path)
|
||||||
|
|
||||||
|
# 按文件修改时间排序
|
||||||
|
sorted_files = sorted(
|
||||||
|
[(os.path.join(dir_path, file), os.path.getmtime(os.path.join(dir_path, file))) for file in files],
|
||||||
|
key=lambda x: x[1],
|
||||||
|
reverse=True
|
||||||
|
)
|
||||||
|
|
||||||
|
# 返回最新文件路径
|
||||||
|
return sorted_files[0][0]
|
||||||
Reference in New Issue
Block a user