arXiv:2604.19826cs.SEcs.AI2026-04中稿 · AIware 2026

将测试代码与实现代码放在一起,能显著提升AI生成代码的质量。

Co-Located Tests, Better AI Code: How Test Syntax Structure Affects Foundation Model Code Generation

  • 测试与代码共处一地时,模型更准确还原原始逻辑。
  • 共处时正确率92%-100%,分离时差异巨大且不可预测。
  • 该设计对主流模型都有效,适合开发AI辅助编程的团队。

AI编程助手越来越多地在生成代码的同时附带测试。测试代码的组织方式——是与实现代码内联还是分开放置——传统上属于测试哲学范畴。本文通过大规模实证研究(830+生成文件,12个模型,3个提供商)和SEGA评估框架(衡量确定性、保留性、正确性),对比了Python的doctests(内联)与Rust的#[test]块(分离)在d-ary堆实现中的效果。结果表明:(1) 内联测试在所有模型中均实现近乎完美的保留性(100%)和正确性(92%-100%);(2) 分离测试导致模型间正确率差距悬殊(0%-100%),且保留性与正确性解耦;(3) 模型行为随生成迭代演化,其中一个模型打破了前三个模型的测试抑制模式;(4) 对7个开源架构(6个Transformer及一个门控线性RNN)的机制分析显示,内联测试标记在5/7个模型中获得2.8-4.4倍更强的注意力,通过消融与引导实验在4个代码专用Transformer及RWKV-6上验证因果关系。该机制也适用于非Transformer架构,说明该设计建议对未来架构具有鲁棒性。在基础模型时代,测试语法结构已成为软件设计问题:将测试与实现共处一处,可显著提升AI生成代码质量。附录进一步指出该效应受模型能力与编程语言限制。

原文摘要 · Abstract (English)

AI coding assistants increasingly generate code alongside tests. How developers structure test code, whether inline with the implementation or in separate blocks, has traditionally been a matter of testing philosophy. We investigate whether this choice affects AI code generation quality. We conduct a large-scale empirical study (830+ generated files, 12 models, 3 providers) using SEGA, a three-dimensional evaluation framework measuring Determinism, Preservation, and Correctness. Comparing inline test syntax (Python doctests) against separated test syntax (Rust #[test] blocks) on a d-ary heap implementation, we find that: (1) inline tests yield near-perfect preservation (100%) and correctness (92-100%) across all models; (2) separated tests expose stark model-tier gaps (0-100% correctness) and independence between preservation and correctness; (3) model behavior evolves across generations, and notably one model breaks the test suppression pattern of its three predecessors; (4) mechanistic analysis on 7 open-source architectures (6 transformers and a gated-linear Recurrent Neural Network (RNN)) reveals inline test markers receive 2.8-4.4$\times$ stronger attention in 5/7 models, with causal validation via knockout and steering experiments on the 4 code-specialized transformers and RWKV-6; the co-location mechanism extends to a non-transformer architecture, suggesting the design recommendation is robust to future architectural shifts. In the Foundation Model era, test syntax structure is a software design concern: co-locating tests with implementation code produces measurably better AI-generated code. This arxiv long version includes appendices that further qualify the effect as bounded by both model capability and programming language.

AI编程测试共处代码生成模型机制

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。