mtkit 1.16.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mtkit/__init__.py +36 -0
- mtkit/__main__.py +6 -0
- mtkit/asset.py +101 -0
- mtkit/audit.py +220 -0
- mtkit/builder.py +100 -0
- mtkit/cli.py +885 -0
- mtkit/conf.py +63 -0
- mtkit/coverage.py +175 -0
- mtkit/db.py +676 -0
- mtkit/evolve.py +142 -0
- mtkit/extract/__init__.py +17 -0
- mtkit/extract/code_facts.py +631 -0
- mtkit/extract/docx_loader.py +115 -0
- mtkit/extract/xmind_loader.py +125 -0
- mtkit/generate/__init__.py +10 -0
- mtkit/generate/llm.py +360 -0
- mtkit/generate/prompts.py +178 -0
- mtkit/generate/skeleton.py +59 -0
- mtkit/normalize.py +480 -0
- mtkit/parsers.py +318 -0
- mtkit/review.py +274 -0
- mtkit/selfheal.py +110 -0
- mtkit/tb.py +191 -0
- mtkit/track.py +309 -0
- mtkit/validator.py +174 -0
- mtkit/writer.py +137 -0
- mtkit-1.16.1.dist-info/METADATA +150 -0
- mtkit-1.16.1.dist-info/RECORD +31 -0
- mtkit-1.16.1.dist-info/WHEEL +5 -0
- mtkit-1.16.1.dist-info/entry_points.txt +2 -0
- mtkit-1.16.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
Prompt 模板库:LLM 用例生成的系统提示与任务提示。
|
|
4
|
+
|
|
5
|
+
设计原则:
|
|
6
|
+
1. 数据契约(JSON 结构)在系统提示中声明,模型只输出 JSON
|
|
7
|
+
2. 历史样例(few-shot)与代码事实注入任务提示,保证 SQL/表名来自事实而非编造
|
|
8
|
+
3. 校验反馈由 llm.py 追加,模型按反馈修正
|
|
9
|
+
"""
|
|
10
|
+
import json
|
|
11
|
+
|
|
12
|
+
# system prompt: role + think-and-search flow + data contract + design methodology + self-review
|
|
13
|
+
SYSTEM_PROMPT = """你是一名资深测试架构师,负责根据技术方案文档生成集成测试用例。你具备检索与思考能力,按以下流程工作。
|
|
14
|
+
|
|
15
|
+
## 思考流程(按序执行)
|
|
16
|
+
|
|
17
|
+
1. **理解**:通读文档片段,梳理出全部功能点(接口/按钮/校验规则/后端逻辑/异常分支),不遗漏也不臆造
|
|
18
|
+
2. **检索**:对每个功能点,主动从「代码事实」(接口清单/SQL 字典/存储过程/表结构)中检索实现细节:
|
|
19
|
+
- 接口:检索对应 url/方法/权限点,交叉验证文档描述与代码实现
|
|
20
|
+
- SQL:优先从 SQL 字典中检索原语句;检索不到时基于表结构字段构造,库/表/列必须真实存在于检索源;两者都无依据时 sql 留空数组
|
|
21
|
+
- 报错文案/权限点/接口 url:从文档与代码事实中原样引用,不自行改写
|
|
22
|
+
- 文档与代码冲突时:以代码实际行为为准,并在用例中反映实际行为
|
|
23
|
+
3. **设计**:按功能点特性选择测试设计方法,确定优先级与粒度
|
|
24
|
+
4. **输出**:按契约生成 JSON(允许在 JSON 前附一段简短「设计说明」,JSON 后不得有其他文字)
|
|
25
|
+
5. **自审**:输出前逐项检查——功能点是否全覆盖、steps/expects 是否逐条对应、SQL 是否可追溯、文案是否原样
|
|
26
|
+
|
|
27
|
+
## 数据契约
|
|
28
|
+
|
|
29
|
+
{"chapters":[{"t":"章节标题","cases":[{"t":"tc-P0-功能测试:用例名","pc":["前置条件"],"steps":["操作步骤"],"expects":["预期结果"],"sql":["SQL语句"]}]}]}
|
|
30
|
+
|
|
31
|
+
- 用例标题:tc-P{级别}-功能测试:{用例名}
|
|
32
|
+
- pc 允许空数组;无数据库交互的用例 sql 为空数组
|
|
33
|
+
|
|
34
|
+
## 设计方法学(按功能点特性选择,可综合运用)
|
|
35
|
+
|
|
36
|
+
- 场景法:主流程/备选流程/异常流程各自成用例,异常流程必须有触发条件与失败预期
|
|
37
|
+
- 等价类划分:同一验证目标合并有效等价类,为无效等价类单独建用例
|
|
38
|
+
- 边界值:数值/长度/时间类校验,取边界值、边界±1、超界值
|
|
39
|
+
- 状态迁移:状态机功能覆盖全部合法迁移与非法迁移
|
|
40
|
+
- 判定表:多条件组合用判定表确保覆盖,冗余组合合并
|
|
41
|
+
|
|
42
|
+
## 智能决策原则(不机械执行)
|
|
43
|
+
|
|
44
|
+
- 优先级:按业务影响判断——P0 核心主流程(阻塞发布)/P1 主要功能/ P2 边界与异常
|
|
45
|
+
- 粒度:以「完整表达一个验证目标」为准(通常 3~8 步);复杂场景可拆多条,等价场景可合并并在标题注明
|
|
46
|
+
- 步骤须含动作对象与输入值,预期须含可观察结果;禁止「操作成功」类空泛预期
|
|
47
|
+
- 用例数量与功能点复杂度成正比,不堆砌无效用例
|
|
48
|
+
- 信息不足处优先合理推断并保证可执行;无法推断的用 pc 或预期注明「待确认」
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def build_chunk_prompt(chunk_title, chunk_text, hist_sample="", code_text="",
|
|
53
|
+
total_chunks=1, chunk_index=1):
|
|
54
|
+
"""构建单个分片的任务提示(含思考检索引导)。
|
|
55
|
+
|
|
56
|
+
:param chunk_title: 分片标题(所属章节)
|
|
57
|
+
:param chunk_text: 分片文本(章节+段落+表格)
|
|
58
|
+
:param hist_sample: 历史用例格式样例(few-shot)
|
|
59
|
+
:param code_text: 代码事实文本(接口清单/SQL 字典,可空)
|
|
60
|
+
:param total_chunks: 总分片数
|
|
61
|
+
:param chunk_index: 当前分片序号(从 1 开始)
|
|
62
|
+
:return: prompt 文本
|
|
63
|
+
"""
|
|
64
|
+
parts = [
|
|
65
|
+
SYSTEM_PROMPT,
|
|
66
|
+
"===== 任务(第 %d/%d 片,章节:%s)=====" % (chunk_index, total_chunks, chunk_title),
|
|
67
|
+
"按思考流程完成本片:先理解本片段功能点 → 从检索源交叉检索实现细节 → 设计用例 → 输出契约 JSON。",
|
|
68
|
+
"本片段覆盖全部功能点(接口/按钮/校验规则/后端逻辑/异常分支);异常分支必须有具体触发条件。",
|
|
69
|
+
]
|
|
70
|
+
if code_text:
|
|
71
|
+
parts.append(
|
|
72
|
+
"===== 非功能场景清单(检索源含 MQ/定时/缓存/事务证据时,须补充对应场景用例)====="
|
|
73
|
+
+ "\n"
|
|
74
|
+
+ "幂等性(重复提交/重发)、越权(无权限/跨用户)、数据一致性(并发/事务回滚)、"
|
|
75
|
+
+ "消息可靠性(消费失败/重复消费)、定时任务(触发时机/重叠执行)、"
|
|
76
|
+
+ "缓存一致性(缓存与库不一致/失效)。"
|
|
77
|
+
)
|
|
78
|
+
if hist_sample:
|
|
79
|
+
parts.append("===== 风格参照(检索其层级、详略与标题习惯,对齐风格而非照抄)=====")
|
|
80
|
+
parts.append(hist_sample)
|
|
81
|
+
if code_text:
|
|
82
|
+
parts.append("===== 检索源(接口/SQL/表结构事实;SQL 与表名、列名均从这里检索,无依据不得编造)=====")
|
|
83
|
+
parts.append(code_text)
|
|
84
|
+
else:
|
|
85
|
+
parts.append("===== 检索源 =====")
|
|
86
|
+
parts.append("(本片未注入代码事实:SQL 无依据时留空数组;接口按文档描述写入)")
|
|
87
|
+
parts.append("===== 技术方案文档片段 ====")
|
|
88
|
+
parts.append(chunk_text)
|
|
89
|
+
parts.append("===== 输出 ====")
|
|
90
|
+
parts.append("可选:JSON 前附一段简短设计说明(功能点清单与方法选择)。然后输出契约 JSON。")
|
|
91
|
+
return "\n\n".join(parts)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def facts_to_text(facts):
|
|
95
|
+
"""结构化事实(docx/代码扫描结果)转纯文本,用于 prompt 注入。
|
|
96
|
+
|
|
97
|
+
支持两种来源:
|
|
98
|
+
- docx 事实:{"root","sections","features"}
|
|
99
|
+
- 代码事实:{"interfaces","sqls","stored_procs"}
|
|
100
|
+
|
|
101
|
+
:param facts: 结构化事实 dict
|
|
102
|
+
:return: 文本
|
|
103
|
+
"""
|
|
104
|
+
if not facts:
|
|
105
|
+
return ""
|
|
106
|
+
lines = []
|
|
107
|
+
root = facts.get("root")
|
|
108
|
+
if root:
|
|
109
|
+
lines.append("文档: " + root)
|
|
110
|
+
|
|
111
|
+
sections = facts.get("sections") or []
|
|
112
|
+
for section in sections:
|
|
113
|
+
lines.append("# " + section.get("title", ""))
|
|
114
|
+
lines.extend(section.get("paragraphs") or [])
|
|
115
|
+
for table in section.get("tables") or []:
|
|
116
|
+
if not table:
|
|
117
|
+
continue
|
|
118
|
+
lines.append("| " + " | ".join(table[0]) + " |")
|
|
119
|
+
lines.append("|" + "---|" * len(table[0]))
|
|
120
|
+
for row in table[1:]:
|
|
121
|
+
lines.append("| " + " | ".join(row) + " |")
|
|
122
|
+
|
|
123
|
+
interfaces = facts.get("interfaces") or []
|
|
124
|
+
for item in interfaces:
|
|
125
|
+
item_lines = item.get("lines") or 0
|
|
126
|
+
risk = "(高风险,文件 %d 行)" % item_lines if item_lines > 500 else ""
|
|
127
|
+
lines.append(
|
|
128
|
+
"接口: %s %s -> %s (权限点: %s)%s" % (
|
|
129
|
+
item.get("method") or "?", item.get("url") or "?",
|
|
130
|
+
item.get("name") or "?", item.get("permission") or "-", risk,
|
|
131
|
+
)
|
|
132
|
+
)
|
|
133
|
+
sqls = facts.get("sqls") or []
|
|
134
|
+
for item in sqls[:50]:
|
|
135
|
+
lines.append("SQL[%s]: %s" % (item.get("id") or "?", item.get("sql") or ""))
|
|
136
|
+
procs = facts.get("stored_procs") or []
|
|
137
|
+
for item in procs[:20]:
|
|
138
|
+
lines.append("存储过程[%s]: %s" % (item.get("name") or "?", (item.get("sql") or "")[:2000]))
|
|
139
|
+
|
|
140
|
+
db_schema = facts.get("db_schema")
|
|
141
|
+
if db_schema:
|
|
142
|
+
lines.append("数据库: " + (db_schema.get("db") or "?"))
|
|
143
|
+
for table in (db_schema.get("tables") or [])[:80]:
|
|
144
|
+
cols = table.get("columns") or []
|
|
145
|
+
col_text = ", ".join(
|
|
146
|
+
(c.get("name") or "")
|
|
147
|
+
+ (":%s" % c.get("type") if c.get("type") else "")
|
|
148
|
+
+ (":%s" % c.get("key") if c.get("key") else "")
|
|
149
|
+
for c in cols
|
|
150
|
+
)
|
|
151
|
+
lines.append("表[%s]: %s" % (table.get("name") or "?", col_text or "(无列信息)"))
|
|
152
|
+
|
|
153
|
+
front = facts.get("frontend")
|
|
154
|
+
if front:
|
|
155
|
+
pages = front.get("pages") or []
|
|
156
|
+
for page in pages[:100]:
|
|
157
|
+
lines.append(
|
|
158
|
+
"前端页面: %s (接口调用: %s | 权限点: %s)" % (
|
|
159
|
+
page.get("file") or "?",
|
|
160
|
+
"、".join(page.get("api_calls") or []) or "-",
|
|
161
|
+
"、".join(page.get("permissions") or []) or "-",
|
|
162
|
+
)
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
features = facts.get("features") or []
|
|
166
|
+
for feature in features[:200]:
|
|
167
|
+
lines.append("功能点: " + feature)
|
|
168
|
+
|
|
169
|
+
return "\n".join(lines)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def facts_to_json_text(facts):
|
|
173
|
+
"""结构化事实转 JSON 文本(调试用)。
|
|
174
|
+
|
|
175
|
+
:param facts: 结构化事实 dict
|
|
176
|
+
:return: JSON 字符串
|
|
177
|
+
"""
|
|
178
|
+
return json.dumps(facts, ensure_ascii=False, indent=2, default=str)
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
骨架生成器(无 LLM 兜底路径)。
|
|
4
|
+
|
|
5
|
+
输入:文档事实(docx_loader 结果) + 历史用例树(xmind_loader 结果)
|
|
6
|
+
输出:用例树骨架 —— 章节结构 + 用例标题 + 占位描述行,供人工/LLM 后续填充。
|
|
7
|
+
|
|
8
|
+
策略:
|
|
9
|
+
1. 提供历史用例时:复制其骨架(章节/用例标题,描述置占位,标题可做词替换)
|
|
10
|
+
2. 提供技术方案时:追加「文档功能点清单」章节(接口/权限点/按钮等事实)
|
|
11
|
+
3. 两者都没有时:返回空树(调用方自行提示)
|
|
12
|
+
"""
|
|
13
|
+
from ..extract.xmind_loader import skeleton_from_hist
|
|
14
|
+
|
|
15
|
+
# feature list chapter title
|
|
16
|
+
FEATURES_SECTION_TITLE = "文档功能点清单(待覆盖)"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _features_section(features):
|
|
20
|
+
"""把文档功能点列表转换为一个章节节点。
|
|
21
|
+
|
|
22
|
+
:param features: 功能点描述串列表
|
|
23
|
+
:return: 章节节点 dict
|
|
24
|
+
"""
|
|
25
|
+
if not features:
|
|
26
|
+
return None
|
|
27
|
+
# dedupe preserving order
|
|
28
|
+
seen = set()
|
|
29
|
+
unique = []
|
|
30
|
+
for feature in features:
|
|
31
|
+
if feature and feature not in seen:
|
|
32
|
+
seen.add(feature)
|
|
33
|
+
unique.append(feature)
|
|
34
|
+
return {"t": FEATURES_SECTION_TITLE, "c": [{"t": feature} for feature in unique]}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def build_skeleton(doc_facts=None, hist_tree=None, root_title=None, replace_map=None):
|
|
38
|
+
"""构建用例树骨架。
|
|
39
|
+
|
|
40
|
+
:param doc_facts: docx_loader.load_docx 结果(可选)
|
|
41
|
+
:param hist_tree: 历史用例树(可选)
|
|
42
|
+
:param root_title: 根节点标题(可选,缺省取文档标题)
|
|
43
|
+
:param replace_map: 标题替换规则 {旧词: 新词}(历史骨架词替换)
|
|
44
|
+
:return: 用例树骨架
|
|
45
|
+
"""
|
|
46
|
+
tree = []
|
|
47
|
+
if hist_tree:
|
|
48
|
+
tree.extend(skeleton_from_hist(hist_tree, replace_map))
|
|
49
|
+
if doc_facts:
|
|
50
|
+
section = _features_section(doc_facts.get("features") or [])
|
|
51
|
+
if section:
|
|
52
|
+
tree.append(section)
|
|
53
|
+
if not tree and doc_facts:
|
|
54
|
+
# no historical cases: build chapter shells from document chapter titles
|
|
55
|
+
for section in doc_facts.get("sections") or []:
|
|
56
|
+
title = section.get("title")
|
|
57
|
+
if title and len(section.get("paragraphs") or []) + len(section.get("tables") or []) > 0:
|
|
58
|
+
tree.append({"t": title, "c": [{"t": "tc-P1-功能测试:待补充用例"}]})
|
|
59
|
+
return tree
|