← Latest papers
💻 computer science

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

This paper presents a large-scale empirical study demonstrating that co-locating test syntax with implementation code significantly improves the quality, correctness, and preservation of AI-generated code across diverse foundation models and architectures compared to separated test structures.

Original authors: Éric Jacopin

Published 2026-04-23
📖 5 min read🧠 Deep dive

Original authors: Éric Jacopin

Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer

Imagine you are hiring a brilliant but slightly literal-minded robot assistant to write a complex computer program for you. You give it a blueprint (the instructions) and a checklist of things the program must do (the tests).

This paper asks a simple question: Does it matter where you put that checklist?

The researchers found that the answer is a resounding yes. How you structure your "to-do list" for the AI changes how well the AI does its job.

Here is the breakdown using everyday analogies:

1. The Two Ways to Write the Checklist

The study compared two main ways of organizing these tests:

  • The "Inline" Method (The Sticky Note): You write the test right next to the specific instruction it checks, like a sticky note stuck directly onto the relevant paragraph of a manual. In programming terms, this is like Python doctests, where the test lives inside the function's description.
  • The "Separated" Method (The Appendix): You write the code first, and then put all the tests in a separate section at the very end of the document, or in a completely different file. In programming terms, this is like Rust #[test] blocks, which sit in a separate "test module" at the bottom of the file.

2. The Big Discovery: Proximity Matters

The researchers found that Inline (Sticky Notes) wins every time.

  • When tests are right next to the code: The AI acts like a super-focused student. It sees the instruction and the test together, so it understands exactly what to build. It rarely makes mistakes, and it almost always keeps the tests in the final product.
  • When tests are separated: The AI gets confused. It's like asking someone to build a house and then handing them the inspection checklist in a different room. The AI might build a perfect house (the code works), but it often throws away the checklist entirely, or it builds the house but forgets to check if the doors open.

3. The "Robot Personality" Problem

One of the most interesting findings is that different AI models behave like different people with different habits.

  • The "Honest" Models: Some models (like the lower-tier "Haiku" or "Sonnet" versions) are very obedient. Even if you put the tests in the "Appendix," they still keep them.
  • The "Overachiever" Models: Some high-powered models (like the "Opus" series) act like over-eager interns. When they see a separated test list, they think, "Oh, I don't need to keep this list; I'll just build the thing perfectly based on the list and throw the list away." They build perfect code but delete the tests, leaving you with no way to verify it later.
  • The "Forgetful" Models: Some models are so confused by separated tests that they build a broken house and throw away the checklist.

The Twist: The researchers watched these robots evolve. One model (Opus 4.6) suddenly changed its mind and started keeping the separated tests, while its older siblings didn't. This proves that AI behavior isn't fixed; it changes with updates, so you can't just assume "Model X is good" forever.

4. Why Does This Happen? (The "Brain Scan")

The researchers didn't just guess; they looked inside the AI's "brain" (using a technique called mechanistic interpretability).

They found that when tests are inline (right next to the code), the AI's attention mechanism (its focus) is 3 to 4 times stronger. It's like the AI is wearing a pair of glasses that makes the test instructions glow bright red when they are next to the code. When the tests are separated, the instructions are dim and far away, so the AI's brain barely notices them.

Even more surprisingly, this "glowing" effect happens in different types of AI brains, not just the most common ones. This suggests that physical closeness in the text is a fundamental rule for how these machines learn.

5. The "Temperature" Surprise

The study also found that setting the AI to "Temperature 0" (which usually means "be as predictable as possible") doesn't actually make the AI 100% predictable. It's like telling a human to "be consistent," but they still might write a different sentence structure every time. The code works, but the words change. This means you can't just run a test once; you have to run it many times to be sure.

The Bottom Line: What Should You Do?

If you are using AI to write code, the paper gives you a clear design rule:

Put your tests right next to the code they are testing.

Don't hide your tests in a separate file or a distant section of the document. Keep them glued to the function they verify.

  • For AI Assistants: This isn't just about "clean code"; it's about communication. You are speaking the AI's language.
  • For Teams: If you use AI, your testing strategy is no longer just a developer preference; it's a critical part of the software design. If you separate your tests, you might get great code but lose the safety net (the tests) that proves it works.

In short: If you want the AI to do its best work, don't make it hunt for the instructions. Put the checklist right next to the task.

Drowning in papers in your field?

Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.

Try Digest →