aitest-kit 0.3.2__tar.gz → 0.3.3__tar.gz

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.
Files changed (131) hide show
  1. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/PKG-INFO +4 -1
  2. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/README.md +3 -0
  3. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/ir_renderer.py +37 -2
  4. aitest_kit-0.3.3/aitest_kit/helpers/capture.py +276 -0
  5. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/report/cli.py +32 -1
  6. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/report/task_runner.py +30 -2
  7. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/README.md +3 -0
  8. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/aitest_config/refs/config-files.md +32 -0
  9. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit.egg-info/PKG-INFO +4 -1
  10. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit.egg-info/SOURCES.txt +2 -0
  11. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/pyproject.toml +1 -1
  12. aitest_kit-0.3.3/tests/test_capture.py +106 -0
  13. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_codegen_ir.py +29 -0
  14. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_report_cli.py +168 -0
  15. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/LICENSE +0 -0
  16. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/__init__.py +0 -0
  17. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/cli.py +0 -0
  18. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/__init__.py +0 -0
  19. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/case_flow_planner.py +0 -0
  20. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/cli.py +0 -0
  21. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/emitter.py +0 -0
  22. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/explain.py +0 -0
  23. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/health.py +0 -0
  24. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/ir.py +0 -0
  25. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/module_type.py +0 -0
  26. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/parser.py +0 -0
  27. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/planner.py +0 -0
  28. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/profile.py +0 -0
  29. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/profile_merge.py +0 -0
  30. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/profile_schema.py +0 -0
  31. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/profile_validation_report.py +0 -0
  32. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/profile_validator.py +0 -0
  33. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/profile_variables.py +0 -0
  34. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/project_config.py +0 -0
  35. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/promotion.py +0 -0
  36. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/render_utils.py +0 -0
  37. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/structured_assertions.py +0 -0
  38. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/suite.py +0 -0
  39. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/suite_runner.py +0 -0
  40. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/codegen/suite_target.py +0 -0
  41. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/doctor.py +0 -0
  42. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/helpers/__init__.py +0 -0
  43. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/helpers/grpc_ops.py +0 -0
  44. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/helpers/http.py +0 -0
  45. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/helpers/request_binding.py +0 -0
  46. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/helpers/structured_assertions.py +0 -0
  47. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/init_workspace.py +0 -0
  48. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/registry/__init__.py +0 -0
  49. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/registry/cli.py +0 -0
  50. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/registry/loader.py +0 -0
  51. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/registry/models.py +0 -0
  52. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/registry/path_resolver.py +0 -0
  53. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/registry/selection.py +0 -0
  54. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/registry/task_loader.py +0 -0
  55. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/report/__init__.py +0 -0
  56. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/report/classifier.py +0 -0
  57. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/report/codegen_check.py +0 -0
  58. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/report/collector.py +0 -0
  59. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/report/paths.py +0 -0
  60. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/report/renderer.py +0 -0
  61. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/report/sanitizer.py +0 -0
  62. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/runtime_variables.py +0 -0
  63. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/__init__.py +0 -0
  64. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/.gitignore +0 -0
  65. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/AGENTS.md +0 -0
  66. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/CLAUDE.md +0 -0
  67. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/__init__.py +0 -0
  68. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/aitest_config/aitest.yaml +0 -0
  69. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/aitest_config/refs/assertion-strategy.md +0 -0
  70. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/aitest_config/refs/case-format.md +0 -0
  71. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/aitest_config/refs/l1-template.md +0 -0
  72. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/aitest_config/refs/l2-template.md +0 -0
  73. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/aitest_config/refs/mismatch-format.md +0 -0
  74. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/aitest_config/schemas/codegen_profile.schema.json +0 -0
  75. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/docs/.gitkeep +0 -0
  76. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/skills/README.md +0 -0
  77. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/skills/case-migrate/SKILL.md +0 -0
  78. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/skills/doc-gen/SKILL.md +0 -0
  79. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/skills/doc-gen/refs/templates.md +0 -0
  80. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/skills/doc-review/SKILL.md +0 -0
  81. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/skills/emitter-build/SKILL.md +0 -0
  82. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/skills/emitter-build/refs/patterns.md +0 -0
  83. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/skills/knowledge-build/SKILL.md +0 -0
  84. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/skills/test-codegen/SKILL.md +0 -0
  85. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/skills/test-codegen/refs/emitter_rules.md +0 -0
  86. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/skills/test-codegen/refs/selector_reference.md +0 -0
  87. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/skills/test-design/SKILL.md +0 -0
  88. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/skills/test-fix/SKILL.md +0 -0
  89. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/skills/test-maintain/SKILL.md +0 -0
  90. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/skills/test-maintain/refs/routing.md +0 -0
  91. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/skills/test-scaffold/SKILL.md +0 -0
  92. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/skills/test-scaffold/refs/constraints.md +0 -0
  93. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/skills/test-scaffold/refs/formats.md +0 -0
  94. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/test_workspace/generated/.gitkeep +0 -0
  95. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/test_workspace/knowledge/L0_system_architecture.md +0 -0
  96. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/test_workspace/knowledge/L1/.gitkeep +0 -0
  97. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/test_workspace/knowledge/L2/.gitkeep +0 -0
  98. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/test_workspace/knowledge/TEST_SPEC.md +0 -0
  99. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/test_workspace/plans/.gitkeep +0 -0
  100. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/test_workspace/reports/.gitkeep +0 -0
  101. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/test_workspace/results/.gitkeep +0 -0
  102. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/test_workspace/suites/.gitkeep +0 -0
  103. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/test_workspace/targets/.gitkeep +0 -0
  104. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/templates/project_workspace/test_workspace/tasks/.gitkeep +0 -0
  105. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/upgrade_workspace.py +0 -0
  106. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/workspace.py +0 -0
  107. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit/workspace_config.py +0 -0
  108. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit.egg-info/dependency_links.txt +0 -0
  109. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit.egg-info/entry_points.txt +0 -0
  110. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit.egg-info/requires.txt +0 -0
  111. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/aitest_kit.egg-info/top_level.txt +0 -0
  112. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/setup.cfg +0 -0
  113. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_classifier.py +0 -0
  114. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_codegen_health_report.py +0 -0
  115. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_codegen_parser.py +0 -0
  116. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_codegen_planner.py +0 -0
  117. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_codegen_profile_validator.py +0 -0
  118. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_codegen_promotion.py +0 -0
  119. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_codegen_promotion_reports.py +0 -0
  120. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_codegen_protocol_detection.py +0 -0
  121. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_codegen_suite_profile.py +0 -0
  122. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_codegen_suite_target.py +0 -0
  123. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_collector.py +0 -0
  124. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_doctor.py +0 -0
  125. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_registry_contexts.py +0 -0
  126. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_registry_maintenance_cli.py +0 -0
  127. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_renderer.py +0 -0
  128. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_runtime_variables.py +0 -0
  129. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_sanitizer.py +0 -0
  130. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_workspace_config.py +0 -0
  131. {aitest_kit-0.3.2 → aitest_kit-0.3.3}/tests/test_workspace_template.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aitest-kit
3
- Version: 0.3.2
3
+ Version: 0.3.3
4
4
  Summary: AI-assisted testing toolchain: docs → knowledge base → Markdown cases → pytest code → structured reports
5
5
  Author: tlzmw001
6
6
  License-Expression: MIT
@@ -155,6 +155,7 @@ aitest codegen --suite-file <suite.yaml> --validate-profile # profile 门禁
155
155
  aitest codegen --suite-file <suite.yaml> # 生成 pytest
156
156
  aitest codegen --suite-file <suite.yaml> --check # 检查 generated 是否过期
157
157
  aitest run --suite-file <suite.yaml> # 执行一个 suite
158
+ aitest run --suite-file <suite.yaml> --capture # 执行并写入 capture.jsonl
158
159
  aitest run --target <target> [--module <module>] # 按 target/module 回归
159
160
  aitest run --all # 全量回归
160
161
  aitest report --suite-file/--target/--all ... # 重渲染报告
@@ -168,6 +169,8 @@ AITEST_ENV_FILE=/tmp/test.env aitest run --suite-file <suite.yaml>
168
169
 
169
170
  报告只记录变量名,不记录变量值。完整选项见 `aitest --help`。
170
171
 
172
+ 排查失败时可加 `--capture`,运行目录下会生成一个 `capture.jsonl`。框架只自动捕获默认 HTTP 用例;自定义 fixture、gRPC 或 SDK 调用可以手动调用 `aitest_kit.helpers.capture.capture_io()`。capture 不自动脱敏,敏感字段应在用户 fixture 中处理后再写入。
173
+
171
174
  ## AI Skills
172
175
 
173
176
  workspace 内置 agent-neutral 的 `skills/`,按环境复制到对应目录:
@@ -104,6 +104,7 @@ aitest codegen --suite-file <suite.yaml> --validate-profile # profile 门禁
104
104
  aitest codegen --suite-file <suite.yaml> # 生成 pytest
105
105
  aitest codegen --suite-file <suite.yaml> --check # 检查 generated 是否过期
106
106
  aitest run --suite-file <suite.yaml> # 执行一个 suite
107
+ aitest run --suite-file <suite.yaml> --capture # 执行并写入 capture.jsonl
107
108
  aitest run --target <target> [--module <module>] # 按 target/module 回归
108
109
  aitest run --all # 全量回归
109
110
  aitest report --suite-file/--target/--all ... # 重渲染报告
@@ -117,6 +118,8 @@ AITEST_ENV_FILE=/tmp/test.env aitest run --suite-file <suite.yaml>
117
118
 
118
119
  报告只记录变量名,不记录变量值。完整选项见 `aitest --help`。
119
120
 
121
+ 排查失败时可加 `--capture`,运行目录下会生成一个 `capture.jsonl`。框架只自动捕获默认 HTTP 用例;自定义 fixture、gRPC 或 SDK 调用可以手动调用 `aitest_kit.helpers.capture.capture_io()`。capture 不自动脱敏,敏感字段应在用户 fixture 中处理后再写入。
122
+
120
123
  ## AI Skills
121
124
 
122
125
  workspace 内置 agent-neutral 的 `skills/`,按环境复制到对应目录:
@@ -47,6 +47,7 @@ def _render_header(
47
47
  ctx: EmitContext,
48
48
  has_profile_variables: bool = False,
49
49
  has_structured_assertions: bool = False,
50
+ has_default_http: bool = False,
50
51
  ) -> list[str]:
51
52
  regenerate_hint = _regenerate_hint(ctx)
52
53
  lines = [
@@ -59,6 +60,8 @@ def _render_header(
59
60
  ])
60
61
  if ctx.shared_config.base_request_http:
61
62
  lines.append("from aitest_kit.helpers.request_binding import build_request")
63
+ if has_default_http:
64
+ lines.append("from aitest_kit.helpers.capture import capture_io")
62
65
  if has_profile_variables:
63
66
  lines.append("from aitest_kit.runtime_variables import resolve_profile_variables")
64
67
  if has_structured_assertions:
@@ -383,10 +386,40 @@ def _render_default_body(
383
386
  return lines, unparsed, diagnostics
384
387
 
385
388
  req_call = _render_req_call(case_ir.request)
389
+ api_path = dict_to_python_compact(case_ir.call.api_path)
386
390
  lines.append("")
391
+ lines.append(f" __aitest_request = {req_call}")
392
+ lines.append(" __aitest_response = None")
393
+ lines.append(" try:")
394
+ if case_ir.call.helper == "http_helper.post":
395
+ lines.append(' if hasattr(http_helper, "post_response"):')
396
+ lines.append(
397
+ f" __aitest_response = http_helper.post_response("
398
+ f"{case_ir.call.target}, {api_path}, json=__aitest_request)"
399
+ )
400
+ lines.append(" __aitest_response.raise_for_status()")
401
+ lines.append(" resp = __aitest_response.json()")
402
+ lines.append(" else:")
403
+ lines.append(
404
+ f" resp = {case_ir.call.helper}("
405
+ f"{case_ir.call.target}, {api_path}, json=__aitest_request)"
406
+ )
407
+ lines.append(" __aitest_response = resp")
408
+ else:
409
+ lines.append(
410
+ f" resp = {case_ir.call.helper}("
411
+ f"{case_ir.call.target}, {api_path}, json=__aitest_request)"
412
+ )
413
+ lines.append(" __aitest_response = resp")
414
+ lines.append(" except Exception as exc:")
415
+ lines.append(
416
+ f' capture_io(__tc_meta__["tc_id"], label={api_path}, protocol="http", '
417
+ "request=__aitest_request, response=__aitest_response, exception=exc)"
418
+ )
419
+ lines.append(" raise")
387
420
  lines.append(
388
- f' resp = {case_ir.call.helper}({case_ir.call.target}, '
389
- f'"{case_ir.call.api_path}", json={req_call})'
421
+ f' capture_io(__tc_meta__["tc_id"], label={api_path}, protocol="http", '
422
+ "request=__aitest_request, response=__aitest_response)"
390
423
  )
391
424
 
392
425
  common_assertions, case_assertions = _split_default_assertions(case_ir, ctx)
@@ -439,11 +472,13 @@ def render_file_from_ir(
439
472
  for case in file_ir.cases
440
473
  for assertion in case.assertions
441
474
  )
475
+ has_default_http = any(case.strategy == "default_http" for case in file_ir.cases)
442
476
 
443
477
  all_lines.extend(_render_header(
444
478
  ctx,
445
479
  has_profile_variables=has_profile_variables,
446
480
  has_structured_assertions=has_structured_assertions,
481
+ has_default_http=has_default_http,
447
482
  ))
448
483
  all_lines.extend(_render_base_request(ctx))
449
484
  if ctx.shared_config.base_request_http:
@@ -0,0 +1,276 @@
1
+ """Runtime capture helpers for generated tests and user fixtures."""
2
+ from __future__ import annotations
3
+
4
+ import json
5
+ import os
6
+ from dataclasses import asdict, dataclass, is_dataclass
7
+ from datetime import datetime
8
+ from pathlib import Path
9
+ from typing import Any
10
+
11
+ import yaml
12
+
13
+
14
+ CAPTURE_CONFIG_PATH = Path("aitest_config/capture.yaml")
15
+ _UNSET = object()
16
+
17
+
18
+ @dataclass(frozen=True)
19
+ class CaptureSettings:
20
+ enabled: bool = False
21
+ include_request: bool = True
22
+ include_response: bool = True
23
+ include_exception: bool = True
24
+ include_metadata: bool = True
25
+ string_length: int = 4096
26
+ output_file: str = "capture.jsonl"
27
+
28
+
29
+ def load_capture_settings(*, enabled_override: bool = False) -> CaptureSettings:
30
+ """Load optional capture.yaml and apply the CLI enabled override."""
31
+ data: dict[str, Any] = {}
32
+ if CAPTURE_CONFIG_PATH.exists():
33
+ raw = yaml.safe_load(CAPTURE_CONFIG_PATH.read_text(encoding="utf-8")) or {}
34
+ if not isinstance(raw, dict):
35
+ raise RuntimeError(f"{CAPTURE_CONFIG_PATH} must be a YAML mapping")
36
+ data = raw
37
+
38
+ include = data.get("include", {})
39
+ if not isinstance(include, dict):
40
+ include = {}
41
+ limits = data.get("limits", {})
42
+ if not isinstance(limits, dict):
43
+ limits = {}
44
+ output = data.get("output", {})
45
+ if not isinstance(output, dict):
46
+ output = {}
47
+
48
+ output_file = str(output.get("file") or "capture.jsonl")
49
+ _validate_output_file(output_file)
50
+ return CaptureSettings(
51
+ enabled=bool(data.get("enabled", False)) or enabled_override,
52
+ include_request=bool(include.get("request", True)),
53
+ include_response=bool(include.get("response", True)),
54
+ include_exception=bool(include.get("exception", True)),
55
+ include_metadata=bool(include.get("metadata", True)),
56
+ string_length=_positive_int(limits.get("string_length"), 4096),
57
+ output_file=output_file,
58
+ )
59
+
60
+
61
+ def capture_env(settings: CaptureSettings, capture_file: str | Path) -> dict[str, str]:
62
+ """Return environment variables consumed by capture_io in pytest subprocesses."""
63
+ if not settings.enabled:
64
+ return {}
65
+ return {
66
+ "AITEST_CAPTURE": "1",
67
+ "AITEST_CAPTURE_FILE": str(capture_file),
68
+ "AITEST_CAPTURE_INCLUDE": json.dumps({
69
+ "request": settings.include_request,
70
+ "response": settings.include_response,
71
+ "exception": settings.include_exception,
72
+ "metadata": settings.include_metadata,
73
+ }),
74
+ "AITEST_CAPTURE_STRING_LENGTH": str(settings.string_length),
75
+ }
76
+
77
+
78
+ def capture_file_for_run(
79
+ run_dir: str | Path,
80
+ settings: CaptureSettings,
81
+ *,
82
+ capture_file: str | Path | None = None,
83
+ ) -> Path | None:
84
+ """Return the capture file path for a run, or None when capture is disabled."""
85
+ if not settings.enabled:
86
+ return None
87
+ if capture_file is not None:
88
+ return Path(capture_file)
89
+ return Path(run_dir) / settings.output_file
90
+
91
+
92
+ def capture_io(
93
+ case_id: str,
94
+ *,
95
+ label: str = "",
96
+ protocol: str = "",
97
+ request: Any = _UNSET,
98
+ response: Any = _UNSET,
99
+ exception: Any = _UNSET,
100
+ metadata: dict[str, Any] | None = None,
101
+ ) -> None:
102
+ """Append one capture record when runtime capture is enabled.
103
+
104
+ Capture does not redact. Users should pass already-safe objects when needed.
105
+ Capture failures are intentionally ignored so debugging output cannot fail tests.
106
+ """
107
+ if os.environ.get("AITEST_CAPTURE") != "1":
108
+ return
109
+ capture_path = os.environ.get("AITEST_CAPTURE_FILE", "")
110
+ if not capture_path:
111
+ return
112
+
113
+ try:
114
+ include = _include_settings()
115
+ limit = _positive_int(os.environ.get("AITEST_CAPTURE_STRING_LENGTH"), 4096)
116
+ record: dict[str, Any] = {
117
+ "timestamp": datetime.now().astimezone().isoformat(timespec="seconds"),
118
+ "case_id": str(case_id),
119
+ }
120
+ if label:
121
+ record["label"] = str(label)
122
+ if protocol:
123
+ record["protocol"] = str(protocol)
124
+ if request is not _UNSET and include.get("request", True):
125
+ record["request"] = _to_capture_data(request, string_length=limit)
126
+ if response is not _UNSET and include.get("response", True):
127
+ record["response"] = _to_capture_data(response, string_length=limit)
128
+ if exception is not _UNSET and include.get("exception", True):
129
+ record["exception"] = _to_capture_data(exception, string_length=limit)
130
+ if metadata and include.get("metadata", True):
131
+ record["metadata"] = _to_capture_data(metadata, string_length=limit)
132
+
133
+ path = Path(capture_path)
134
+ path.parent.mkdir(parents=True, exist_ok=True)
135
+ with path.open("a", encoding="utf-8") as fh:
136
+ fh.write(json.dumps(record, ensure_ascii=False, default=str) + "\n")
137
+ except Exception:
138
+ return
139
+
140
+
141
+ def _include_settings() -> dict[str, bool]:
142
+ raw = os.environ.get("AITEST_CAPTURE_INCLUDE", "")
143
+ if not raw:
144
+ return {"request": True, "response": True, "exception": True, "metadata": True}
145
+ try:
146
+ data = json.loads(raw)
147
+ except json.JSONDecodeError:
148
+ return {"request": True, "response": True, "exception": True, "metadata": True}
149
+ if not isinstance(data, dict):
150
+ return {"request": True, "response": True, "exception": True, "metadata": True}
151
+ return {
152
+ "request": bool(data.get("request", True)),
153
+ "response": bool(data.get("response", True)),
154
+ "exception": bool(data.get("exception", True)),
155
+ "metadata": bool(data.get("metadata", True)),
156
+ }
157
+
158
+
159
+ def _to_capture_data(value: Any, *, string_length: int, _seen: set[int] | None = None) -> Any:
160
+ seen = _seen if _seen is not None else set()
161
+ obj_id = id(value)
162
+ if obj_id in seen:
163
+ return "<recursive>"
164
+
165
+ if value is None or isinstance(value, (bool, int, float)):
166
+ return value
167
+ if isinstance(value, str):
168
+ return _limit_string(value, string_length)
169
+ if isinstance(value, bytes):
170
+ try:
171
+ return _limit_string(value.decode("utf-8"), string_length)
172
+ except UnicodeDecodeError:
173
+ return _limit_string(repr(value), string_length)
174
+ if isinstance(value, BaseException):
175
+ return {
176
+ "type": value.__class__.__name__,
177
+ "message": _limit_string(str(value), string_length),
178
+ }
179
+
180
+ httpx_response = _httpx_response(value, string_length=string_length)
181
+ if httpx_response is not None:
182
+ return httpx_response
183
+
184
+ protobuf_message = _protobuf_message(value)
185
+ if protobuf_message is not None:
186
+ return _to_capture_data(protobuf_message, string_length=string_length, _seen=seen)
187
+
188
+ if is_dataclass(value) and not isinstance(value, type):
189
+ return _to_capture_data(asdict(value), string_length=string_length, _seen=seen)
190
+
191
+ if isinstance(value, dict):
192
+ seen.add(obj_id)
193
+ try:
194
+ return {
195
+ str(key): _to_capture_data(item, string_length=string_length, _seen=seen)
196
+ for key, item in value.items()
197
+ }
198
+ finally:
199
+ seen.discard(obj_id)
200
+
201
+ if isinstance(value, (list, tuple, set)):
202
+ seen.add(obj_id)
203
+ try:
204
+ return [
205
+ _to_capture_data(item, string_length=string_length, _seen=seen)
206
+ for item in value
207
+ ]
208
+ finally:
209
+ seen.discard(obj_id)
210
+
211
+ model_dump = getattr(value, "model_dump", None)
212
+ if callable(model_dump):
213
+ try:
214
+ return _to_capture_data(model_dump(), string_length=string_length, _seen=seen)
215
+ except Exception:
216
+ pass
217
+ dict_method = getattr(value, "dict", None)
218
+ if callable(dict_method):
219
+ try:
220
+ return _to_capture_data(dict_method(), string_length=string_length, _seen=seen)
221
+ except Exception:
222
+ pass
223
+
224
+ return {"__repr__": _limit_string(repr(value), string_length)}
225
+
226
+
227
+ def _httpx_response(value: Any, *, string_length: int) -> dict[str, Any] | None:
228
+ try:
229
+ import httpx
230
+ except Exception:
231
+ return None
232
+ if not isinstance(value, httpx.Response):
233
+ return None
234
+ body: Any
235
+ try:
236
+ body = value.json()
237
+ except Exception:
238
+ body = value.text
239
+ return {
240
+ "status_code": value.status_code,
241
+ "body": _to_capture_data(body, string_length=string_length),
242
+ }
243
+
244
+
245
+ def _protobuf_message(value: Any) -> dict[str, Any] | None:
246
+ try:
247
+ from google.protobuf.json_format import MessageToDict
248
+ from google.protobuf.message import Message
249
+ except Exception:
250
+ return None
251
+ if not isinstance(value, Message):
252
+ return None
253
+ try:
254
+ return MessageToDict(value, preserving_proto_field_name=True)
255
+ except Exception:
256
+ return {"__repr__": repr(value)}
257
+
258
+
259
+ def _limit_string(value: str, limit: int) -> str:
260
+ if limit <= 0 or len(value) <= limit:
261
+ return value
262
+ return value[:limit] + f"...<truncated {len(value) - limit} chars>"
263
+
264
+
265
+ def _positive_int(value: Any, default: int) -> int:
266
+ try:
267
+ parsed = int(value)
268
+ except (TypeError, ValueError):
269
+ return default
270
+ return parsed if parsed > 0 else default
271
+
272
+
273
+ def _validate_output_file(value: str) -> None:
274
+ path = Path(value)
275
+ if path.is_absolute() or path.parent != Path("."):
276
+ raise RuntimeError("capture output.file must be a file name under the run directory")
@@ -18,6 +18,12 @@ from aitest_kit.report.codegen_check import run_codegen_check
18
18
  from aitest_kit.report.collector import blocked_result, collect_result, generated_nodeids_for_case_ids
19
19
  from aitest_kit.report.paths import selector_report_bucket, suite_report_bucket, task_report_bucket
20
20
  from aitest_kit.report.renderer import render_markdown
21
+ from aitest_kit.helpers.capture import (
22
+ CaptureSettings,
23
+ capture_env,
24
+ capture_file_for_run,
25
+ load_capture_settings,
26
+ )
21
27
  from aitest_kit.runtime_variables import (
22
28
  ProfileVariableError,
23
29
  dotenv_path,
@@ -69,6 +75,7 @@ def _load_paths() -> ReportPaths:
69
75
  @click.option("--module", "module_name", help="Run active suites registered under one module")
70
76
  @click.option("--all", "all_suites", is_flag=True, help="Run all active suites in the registry")
71
77
  @click.option("--case-id", "case_ids", multiple=True, help="Run one case id; can be repeated")
78
+ @click.option("--capture", is_flag=True, help="Write runtime request/response capture to capture.jsonl")
72
79
  @click.option("--workspace", type=click.Path(file_okay=False, dir_okay=True), help="Run from another AITest workspace root")
73
80
  @click.argument("args", nargs=-1, type=click.UNPROCESSED, metavar="[PYTEST_ARGS]...")
74
81
  def run_command(
@@ -80,6 +87,7 @@ def run_command(
80
87
  module_name: str | None,
81
88
  all_suites: bool,
82
89
  case_ids: tuple[str, ...],
90
+ capture: bool,
83
91
  workspace: str | None,
84
92
  args: tuple[str, ...],
85
93
  ):
@@ -107,6 +115,7 @@ def run_command(
107
115
  module_name=module_name,
108
116
  all_suites=all_suites,
109
117
  case_ids=case_ids,
118
+ capture=capture,
110
119
  )
111
120
  except (FileNotFoundError, NotADirectoryError) as exc:
112
121
  raise click.ClickException(str(exc)) from exc
@@ -127,6 +136,10 @@ def _run_command_impl(
127
136
  report_run_dir: Path | None = None,
128
137
  run_id_override: str | None = None,
129
138
  update_latest: bool = True,
139
+ capture: bool = False,
140
+ capture_settings: CaptureSettings | None = None,
141
+ capture_file_path: Path | None = None,
142
+ echo_capture_path: bool = True,
130
143
  ) -> None:
131
144
  extra_args = list(args)
132
145
  if suite_file and task_file:
@@ -149,6 +162,8 @@ def _run_command_impl(
149
162
  task_file,
150
163
  extra_args,
151
164
  case_ids=list(case_ids),
165
+ capture=capture,
166
+ capture_settings=capture_settings,
152
167
  )
153
168
  return
154
169
  if selector_used:
@@ -167,6 +182,8 @@ def _run_command_impl(
167
182
  include_manual=include_manual,
168
183
  skip_codegen_check=skip_codegen_check,
169
184
  extra_args=extra_args,
185
+ capture=capture,
186
+ capture_settings=capture_settings,
170
187
  )
171
188
  return
172
189
  paths = _load_paths()
@@ -202,8 +219,18 @@ def _run_command_impl(
202
219
  else:
203
220
  run_id, run_dir = _create_run_dir(paths.reports_dir)
204
221
 
222
+ effective_capture = capture_settings or load_capture_settings(enabled_override=capture)
223
+ capture_file = capture_file_for_run(
224
+ run_dir,
225
+ effective_capture,
226
+ capture_file=capture_file_path,
227
+ )
228
+
229
+ command_parts = ["aitest", "run", "--suite-file", suite_file or ""]
230
+ if effective_capture.enabled:
231
+ command_parts.append("--capture")
205
232
  command = _format_run_command(
206
- ["aitest", "run", "--suite-file", suite_file or ""],
233
+ command_parts,
207
234
  case_ids=case_ids,
208
235
  pytest_args=extra_args,
209
236
  )
@@ -229,6 +256,8 @@ def _run_command_impl(
229
256
  _write_result(run_dir, paths.reports_dir, result, update_latest=update_latest)
230
257
  click.echo(f"BLOCKED_RUN: {exc}")
231
258
  raise SystemExit(10) from exc
259
+ if capture_file is not None:
260
+ pytest_env.update(capture_env(effective_capture, capture_file))
232
261
 
233
262
  codegen_check = run_codegen_check(
234
263
  skip_codegen_check,
@@ -306,6 +335,8 @@ def _run_command_impl(
306
335
  f"Report written: {run_dir / 'report.md'} "
307
336
  f"(passed={summary['passed']}, failed={summary['failed']}, error={summary['error']})"
308
337
  )
338
+ if echo_capture_path and capture_file is not None and capture_file.exists():
339
+ click.echo(f"Capture written: {capture_file}")
309
340
  raise SystemExit(completed.returncode)
310
341
 
311
342
  @click.command(name="report")
@@ -9,6 +9,7 @@ from typing import Any
9
9
 
10
10
  import click
11
11
 
12
+ from aitest_kit.helpers.capture import CaptureSettings, capture_file_for_run, load_capture_settings
12
13
  from aitest_kit.registry import load_task_context
13
14
  from aitest_kit.registry.models import TaskContext, TaskUnit
14
15
  from aitest_kit.registry.selection import filter_task_context_by_case_ids
@@ -21,6 +22,8 @@ def run_task_command_impl(
21
22
  task_file: str,
22
23
  extra_args: list[str],
23
24
  case_ids: list[str] | None = None,
25
+ capture: bool = False,
26
+ capture_settings: CaptureSettings | None = None,
24
27
  ) -> None:
25
28
  """Run each suite unit in a task, then write a task-level aggregate report."""
26
29
  task = load_task_context(task_file)
@@ -37,6 +40,8 @@ def run_task_command_impl(
37
40
  include_manual=include_manual,
38
41
  skip_codegen_check=skip_codegen_check,
39
42
  extra_args=extra_args,
43
+ capture=capture,
44
+ capture_settings=capture_settings,
40
45
  )
41
46
 
42
47
 
@@ -46,6 +51,8 @@ def run_task_context_command_impl(
46
51
  include_manual: bool,
47
52
  skip_codegen_check: bool,
48
53
  extra_args: list[str],
54
+ capture: bool = False,
55
+ capture_settings: CaptureSettings | None = None,
49
56
  ) -> None:
50
57
  """Run a pre-resolved task context and write a task-level aggregate report."""
51
58
  from aitest_kit.report.cli import _create_run_dir, _load_paths, _run_command_impl, _write_result
@@ -63,6 +70,8 @@ def run_task_context_command_impl(
63
70
  paths = _load_paths()
64
71
  task_reports_dir = _task_reports_dir(task, paths.reports_dir)
65
72
  task_run_id, task_run_dir = _create_run_dir(task_reports_dir)
73
+ effective_capture = capture_settings or load_capture_settings(enabled_override=capture)
74
+ capture_file = capture_file_for_run(task_run_dir, effective_capture)
66
75
  started = time.monotonic()
67
76
  unit_results: list[dict[str, Any]] = []
68
77
  exit_code = 0
@@ -79,6 +88,9 @@ def run_task_context_command_impl(
79
88
  _run_command_impl,
80
89
  task_run_id,
81
90
  task_run_dir,
91
+ capture=effective_capture.enabled,
92
+ capture_settings=effective_capture,
93
+ capture_file_path=capture_file,
82
94
  )
83
95
  unit_results.append(result)
84
96
  if code:
@@ -92,6 +104,7 @@ def run_task_context_command_impl(
92
104
  exit_code=exit_code,
93
105
  include_manual=include_manual,
94
106
  extra_args=extra_args,
107
+ capture=effective_capture.enabled,
95
108
  )
96
109
  _write_result(task_run_dir, task_reports_dir, aggregate)
97
110
  summary = aggregate["summary"]
@@ -99,6 +112,8 @@ def run_task_context_command_impl(
99
112
  f"Task report written: {task_run_dir / 'report.md'} "
100
113
  f"(passed={summary['passed']}, failed={summary['failed']}, error={summary['error']})"
101
114
  )
115
+ if capture_file is not None and capture_file.exists():
116
+ click.echo(f"Capture written: {capture_file}")
102
117
  raise SystemExit(exit_code)
103
118
 
104
119
 
@@ -112,6 +127,10 @@ def _run_task_unit(
112
127
  run_suite,
113
128
  task_run_id: str,
114
129
  task_run_dir: Path,
130
+ *,
131
+ capture: bool = False,
132
+ capture_settings: CaptureSettings | None = None,
133
+ capture_file_path: Path | None = None,
115
134
  ) -> tuple[dict[str, Any], int]:
116
135
  if unit.suite_file is None:
117
136
  click.echo(f"\n[{index}] task unit requires suite_file")
@@ -141,6 +160,10 @@ def _run_task_unit(
141
160
  report_run_dir=unit_dir,
142
161
  run_id_override=task_run_id,
143
162
  update_latest=False,
163
+ capture=capture,
164
+ capture_settings=capture_settings,
165
+ capture_file_path=capture_file_path,
166
+ echo_capture_path=False,
144
167
  )
145
168
  code = 0
146
169
  except SystemExit as exc:
@@ -170,6 +193,7 @@ def _task_result(
170
193
  exit_code: int,
171
194
  include_manual: bool,
172
195
  extra_args: list[str],
196
+ capture: bool = False,
173
197
  ) -> dict[str, Any]:
174
198
  cases: list[dict[str, Any]] = []
175
199
  skipped: list[dict[str, Any]] = []
@@ -210,7 +234,7 @@ def _task_result(
210
234
  "status": "COMPLETED" if exit_code == 0 else "FAILED_RUN",
211
235
  "timestamp": datetime.now().astimezone().isoformat(timespec="seconds"),
212
236
  "duration_seconds": duration_seconds,
213
- "command": _task_command(task, extra_args),
237
+ "command": _task_command(task, extra_args, capture=capture),
214
238
  "task_file": str(task.task_path),
215
239
  "run_scope": run_scope,
216
240
  "project_config_version": unit_results[0].get("project_config_version", "missing") if unit_results else "missing",
@@ -234,7 +258,7 @@ def _task_result(
234
258
  }
235
259
 
236
260
 
237
- def _task_command(task: TaskContext, extra_args: list[str] | None = None) -> str:
261
+ def _task_command(task: TaskContext, extra_args: list[str] | None = None, *, capture: bool = False) -> str:
238
262
  selector = task.metadata.get("selector") if isinstance(task.metadata, dict) else None
239
263
  if isinstance(selector, dict):
240
264
  parts = ["aitest", "run"]
@@ -246,6 +270,8 @@ def _task_command(task: TaskContext, extra_args: list[str] | None = None) -> str
246
270
  parts.extend(["--target", str(target)])
247
271
  if module:
248
272
  parts.extend(["--module", str(module)])
273
+ if capture:
274
+ parts.append("--capture")
249
275
  for case_id in selector.get("case_ids") or []:
250
276
  parts.extend(["--case-id", str(case_id)])
251
277
  if extra_args:
@@ -254,6 +280,8 @@ def _task_command(task: TaskContext, extra_args: list[str] | None = None) -> str
254
280
  return " ".join(parts)
255
281
 
256
282
  parts = ["aitest", "run", "--task-file", str(task.task_path)]
283
+ if capture:
284
+ parts.append("--capture")
257
285
  for case_id in task.metadata.get("case_ids", []) if isinstance(task.metadata, dict) else []:
258
286
  parts.extend(["--case-id", str(case_id)])
259
287
  if extra_args:
@@ -188,6 +188,7 @@ aitest codegen --suite-file test_workspace/suites/<target>/<suite>/suite.yaml
188
188
  aitest codegen --suite-file test_workspace/suites/<target>/<suite>/suite.yaml --check
189
189
 
190
190
  aitest run --suite-file test_workspace/suites/<target>/<suite>/suite.yaml
191
+ aitest run --suite-file test_workspace/suites/<target>/<suite>/suite.yaml --capture
191
192
  aitest run --task-file test_workspace/tasks/<task>.yaml
192
193
  aitest report
193
194
  ```
@@ -200,6 +201,8 @@ AITEST_ENV_FILE=/tmp/your-system-test.env aitest run --suite-file test_workspace
200
201
 
201
202
  `aitest run` 会把 env 文件注入 pytest 子进程;报告只记录变量名,不记录变量值。真实 shell 环境变量优先于 env 文件。
202
203
 
204
+ 排查失败时可加 `--capture`,当前 run 目录会写入一个 `capture.jsonl`。默认只自动捕获框架生成的 HTTP 用例;自定义 fixture、gRPC 或 SDK 调用可以手动调用 `aitest_kit.helpers.capture.capture_io()`。capture 不自动脱敏,敏感字段应先在 fixture 中处理。
205
+
203
206
  从本目录外执行时,追加:
204
207
 
205
208
  ```bash