Files
test_item_gen/modules/test_standard_loader.py
2026-02-04 14:38:52 +08:00

162 lines
4.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# @line_count 150
"""测试规范加载器"""
import yaml
from pathlib import Path
from typing import List, Dict, Any, Optional
class TestStandardLoader:
"""测试规范加载器,负责从配置文件加载测试规范"""
def __init__(self, config_path: Optional[str] = None):
"""
初始化测试规范加载器
Args:
config_path: 配置文件路径默认为config/test_standards.yaml
"""
if config_path is None:
current_dir = Path(__file__).parent.parent
config_path = current_dir / "config" / "test_standards.yaml"
self.config_path = Path(config_path)
self.standards: List[Dict[str, Any]] = []
self.mapping_rules: Dict[str, Any] = {}
self.keyword_mapping: Dict[str, List[str]] = {}
self.load_config()
def load_config(self):
"""加载配置文件"""
if not self.config_path.exists():
raise FileNotFoundError(f"测试规范配置文件不存在: {self.config_path}")
with open(self.config_path, 'r', encoding='utf-8') as f:
config = yaml.safe_load(f)
self.standards = config.get('test_standards', [])
self.mapping_rules = config.get('requirement_mapping', {})
self.keyword_mapping = config.get('keyword_mapping', {})
def get_all_standards(self) -> List[Dict[str, Any]]:
"""
获取所有测试规范
Returns:
测试规范列表
"""
return self.standards
def get_standard_by_id(self, standard_id: str) -> Optional[Dict[str, Any]]:
"""
根据ID获取测试规范
Args:
standard_id: 测试规范ID
Returns:
测试规范字典如果不存在返回None
"""
for standard in self.standards:
if standard.get('id') == standard_id:
return standard
return None
def get_standards_by_ids(self, standard_ids: List[str]) -> List[Dict[str, Any]]:
"""
根据ID列表获取多个测试规范
Args:
standard_ids: 测试规范ID列表
Returns:
测试规范列表
"""
result = []
for standard_id in standard_ids:
standard = self.get_standard_by_id(standard_id)
if standard:
result.append(standard)
return result
def get_standard_summary(self, standard_id: str) -> str:
"""
获取测试规范的摘要
Args:
standard_id: 测试规范ID
Returns:
规范摘要文本
"""
standard = self.get_standard_by_id(standard_id)
if standard:
return standard.get('summary', '')
return ''
def get_standard_key_points(self, standard_id: str) -> List[str]:
"""
获取测试规范的关键测试点
Args:
standard_id: 测试规范ID
Returns:
关键测试点列表
"""
standard = self.get_standard_by_id(standard_id)
if standard:
return standard.get('key_points', [])
return []
def get_requirement_mapping(self) -> Dict[str, Any]:
"""
获取需求类型到测试规范的映射规则
Returns:
映射规则字典
"""
return self.mapping_rules
def get_keyword_mapping(self) -> Dict[str, List[str]]:
"""
获取关键词到测试规范的映射规则
Returns:
关键词映射字典
"""
return self.keyword_mapping
def format_standards_summary(self, standard_ids: List[str]) -> str:
"""
格式化多个测试规范的摘要用于Prompt
Args:
standard_ids: 测试规范ID列表
Returns:
格式化后的摘要文本
"""
if not standard_ids:
return ""
lines = []
for standard_id in standard_ids:
standard = self.get_standard_by_id(standard_id)
if not standard:
continue
name = standard.get('name', standard_id)
summary = standard.get('summary', '').strip()
key_points = standard.get('key_points', [])
lines.append(f"{name}")
if summary:
lines.append(summary)
if key_points:
lines.append("关键测试点:")
for i, point in enumerate(key_points, 1):
lines.append(f" {i}. {point}")
lines.append("") # 空行分隔
return "\n".join(lines)