Skip to content

Commit

Permalink
优化测试用例自动生成 (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-clan authored Aug 29, 2024
1 parent 648f171 commit 16283ce
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
16 changes: 6 additions & 10 deletions httpfpt/utils/case_auto_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,21 @@ def auto_generate_testcases(rewrite: bool = False) -> None:
testcase_func_name = 'test_' + testcase_func_name
case_code = f'''#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import allure
import pytest
from httpfpt.common.send_request import send_request
from httpfpt.utils.request.case_data_parse import get_testcase_data
from common.send_request import send_request
from utils.request.case_data_parse import get_testcase_data
allure_data, ddt_data, ids = get_testcase_data(filename='{file_property[0]}')
ddt_data, ids = get_testcase_data(filename='{file_property[0]}')
@allure.epic(allure_data['epic'])
@allure.feature(allure_data['feature'])
class {testcase_class_name}:
"""{testcase_class_name.replace('Test', '')}"""
@allure.story(allure_data['story'])
@pytest.mark.parametrize('data', ddt_data, ids=ids)
def {testcase_func_name}(self, data):
@pytest.mark.parametrize('case_data', ddt_data, ids=ids)
def {testcase_func_name}(self, case_data):
"""{create_file_root_name}"""
send_request.send_request(data)
send_request.send_request(case_data)
'''
# 创建测试用例文件
tag = case_filename.split(httpfpt_config.PROJECT_NAME)[1].split(os.path.sep)[1:-1]
Expand Down
7 changes: 3 additions & 4 deletions httpfpt/utils/request/case_data_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def case_id_unique_verify() -> None:
redis_client.rset(f'{redis_client.prefix}:case_id_list', str(all_case_id))


def get_testcase_data(*, filename: str) -> tuple[dict, list, list]:
def get_testcase_data(*, filename: str) -> tuple[list, list]:
"""
获取测试用例数据
Expand All @@ -138,15 +138,14 @@ def get_testcase_data(*, filename: str) -> tuple[dict, list, list]:
if steps is None:
raise RequestDataParseError(test_steps_error)

allure_data = case_data['config']['allure']
if isinstance(steps, dict):
ids = get_ids(case_data)
mark = get_testcase_mark(case_data)
if mark is not None:
ddt_data = pytest.param(case_data, marks=[getattr(pytest.mark, m) for m in mark])
else:
ddt_data = case_data
return allure_data, [ddt_data], ids
return [ddt_data], ids
elif isinstance(steps, list):
_ddt_data_list = []
marked_ddt_data_list = []
Expand All @@ -162,7 +161,7 @@ def get_testcase_data(*, filename: str) -> tuple[dict, list, list]:
else:
raise RequestDataParseError(test_steps_error)
ids = get_ids(_ddt_data_list)
return allure_data, marked_ddt_data_list, ids
return marked_ddt_data_list, ids
else:
raise RequestDataParseError(f'请求测试用例数据文件 {filename} 格式错误, 请检查用例数据文件内容')

Expand Down
6 changes: 4 additions & 2 deletions httpfpt/utils/request/ids_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ def get_ids(request_data: dict | list) -> list:
try:
if isinstance(request_data, dict):
module = request_data['config']['module']
name = request_data['test_steps']['name']
case_id = request_data['test_steps']['case_id']
ids.append(f'module: {module}, case_id: {case_id}')
ids.append(f'module: {module}, name: {name}, case_id: {case_id}')
else:
for data in request_data:
module = data['config']['module']
name = data['test_steps']['name']
case_id = data['test_steps']['case_id']
ids.append(f'module: {module}, case_id: {case_id}')
ids.append(f'module: {module}, name: {name}, case_id: {case_id}')
except KeyError as e:
raise RequestDataParseError('测试用例 ids 获取失败, 请检查测试用例数据是否符合规范: {}'.format(e))
return ids

0 comments on commit 16283ce

Please sign in to comment.