init the project
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
# 用例导出服务:将用例集转换为 YAML / CSV 格式
|
||||
|
||||
import csv
|
||||
import io
|
||||
import logging
|
||||
|
||||
import yaml
|
||||
|
||||
from server.services.testcase_engine.generator import tc_generator
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TestCaseExporter:
|
||||
"""Export test case sets to YAML, CSV, or structured data."""
|
||||
|
||||
async def export(self, tc_set_id: str, fmt: str) -> tuple[bytes | str, str]:
|
||||
"""Export testcases in specified format. Returns (content, mime_type, filename)."""
|
||||
tc_set = await tc_generator.get_tc_set(tc_set_id)
|
||||
if not tc_set:
|
||||
raise ValueError(f"Test case set not found: {tc_set_id}")
|
||||
|
||||
cases = tc_set.get("cases", [])
|
||||
|
||||
if fmt == "yaml":
|
||||
return self._to_yaml(cases), "application/x-yaml", f"testcases_{tc_set_id}.yaml"
|
||||
elif fmt == "csv":
|
||||
return self._to_csv(cases), "text/csv", f"testcases_{tc_set_id}.csv"
|
||||
elif fmt == "json":
|
||||
import json
|
||||
return json.dumps(tc_set, ensure_ascii=False, indent=2), "application/json", f"testcases_{tc_set_id}.json"
|
||||
else:
|
||||
raise ValueError(f"Unsupported export format: {fmt}")
|
||||
|
||||
def _to_yaml(self, cases: list[dict]) -> str:
|
||||
"""Convert cases to YAML string."""
|
||||
output = {
|
||||
"testcases": cases,
|
||||
"metadata": {
|
||||
"total": len(cases),
|
||||
"exported_format": "yaml",
|
||||
},
|
||||
}
|
||||
return yaml.dump(output, allow_unicode=True, sort_keys=False, default_flow_style=False)
|
||||
|
||||
def _to_csv(self, cases: list[dict]) -> str:
|
||||
"""Convert cases to CSV string."""
|
||||
if not cases:
|
||||
return ""
|
||||
|
||||
# Flatten tags for CSV
|
||||
flattened = []
|
||||
for c in cases:
|
||||
row = {k: v for k, v in c.items() if k != "tags"}
|
||||
row["tags"] = ";".join(c.get("tags", []))
|
||||
flattened.append(row)
|
||||
|
||||
output = io.StringIO()
|
||||
fieldnames = ["id", "module", "feature", "case_title", "preconditions", "steps", "expected_result", "priority", "tags"]
|
||||
writer = csv.DictWriter(output, fieldnames=fieldnames, extrasaction="ignore")
|
||||
writer.writeheader()
|
||||
writer.writerows(flattened)
|
||||
return output.getvalue()
|
||||
|
||||
|
||||
tc_exporter = TestCaseExporter()
|
||||
Reference in New Issue
Block a user