Unit Test First Prompting: Generate Tests Before Implementation

Bekah Funning Nov 20 2026 Artificial Intelligence
Unit Test First Prompting: Generate Tests Before Implementation

You know that sinking feeling when an AI writes a function for you, it looks perfect, you copy-paste it, and then your app crashes three minutes later? It’s annoying. You trusted the machine because the syntax was clean. But clean syntax doesn't mean correct logic. That is exactly why Unit Test First Prompting is changing how we work with large language models. Instead of asking AI to write the code, you ask it to write the tests first. This simple inversion of workflow forces the AI to define success before it attempts to achieve it.

This isn't just about being picky. It's about control. When you let AI generate implementation code directly, you're hoping it understood your intent. When you make it generate tests first, you're forcing it to articulate what "working" actually means. If the tests are wrong, you catch it immediately. If the tests are right, the subsequent code has a hard boundary to stay within. Let's break down how this works, why it matters for security, and how you can start doing it today without becoming a prompt engineering wizard.

The Core Concept: Inverting the Workflow

Traditional development often follows a loose path: think of a feature, write some code, maybe add tests later if there's time. Test-Driven Development (TDD) flips this: write a failing test, write code to pass it, refactor. Unit Test First Prompting applies this same discipline to AI interactions. It treats the Large Language Model (LLM) not as an oracle, but as a junior developer who needs clear specifications.

Here is the basic loop:

  • Define Intent: Tell the AI what the function should do in plain English.
  • Generate Tests: Ask the AI to create unit tests based on that description. Do not ask for implementation yet.
  • Review Tests: Check if the tests cover edge cases, security risks, and normal flows. Fix them if needed.
  • Generate Implementation: Now ask the AI to write the code that passes those specific tests.
  • Verify: Run the tests. If they pass, you have working code. If they fail, you iterate.

This approach reduces hallucinations. LLMs sometimes invent libraries or methods that don't exist. By locking down the behavior via tests first, you limit the space where the model can wander off into nonsense.

The Red-Green-Refactor Cycle for AI

If you've ever practiced TDD, you know the Red-Green-Refactor cycle. Adapting this for AI prompting requires slight adjustments. The goal is to keep the AI focused on one task at a time.

Stage 1: The Red Stage (Test Generation)

In traditional TDD, "Red" means the test fails because the code doesn't exist. In AI prompting, "Red" means you generate tests that *would* fail if run against empty stubs. Your prompt here must be precise. Don't just say "write tests." Say "write tests for a username validator that checks length and character types."

A good prompt for this stage looks like this:

"Act as a senior QA engineer. I need unit tests for a function called `validateUsername`. Requirements: Usernames must be 3-16 characters long, start with a letter, and contain only alphanumeric characters. Include tests for edge cases like empty strings, special characters, and maximum length. Use Python's pytest framework. Do not write the implementation code yet."

Notice the constraints. You specified the role, the exact requirements, the edge cases, and the tool. You explicitly forbade implementation code. This prevents the AI from mixing concerns.

Stage 2: The Green Stage (Implementation)

Once you approve the tests, you paste them back into the context window along with the original requirement. Then you ask for the code. Because the AI sees the tests, it understands the expected input-output pairs. It knows that `validateUsername("a")` should return False. It knows `validateUsername("Alice123")` should return True.

Prompt example:

"Here are the unit tests for `validateUsername`: [paste tests]. Write the Python implementation for `validateUsername` that passes all these tests. Ensure the code is efficient and readable."

The AI now has a contract. It cannot just guess; it has to satisfy the conditions laid out in the test file.

Stage 3: The Refactor Stage

After the code passes, you might want cleaner code or better performance. You can ask the AI to refactor while keeping tests passing. "Optimize this function for speed, but ensure all existing tests still pass." This keeps quality high without breaking functionality.

Why Security Matters More Here

One of the biggest risks in AI-assisted coding is silent security failures. An AI might write a function that works for valid inputs but crashes or leaks data on malicious ones. Common Weakness Enumeration (CWE) mitigations are hard to enforce after the fact. They are much easier to enforce via tests.

When generating tests, explicitly include security scenarios. For example, if you are building a database query builder, ask the AI to generate tests for SQL injection attempts. If the test suite includes a case where a user input contains `' OR 1=1 --`, and the implementation handles it correctly, you have proof of security. If the test fails, you know the AI-generated code is vulnerable.

Security Benefits of Test-First Prompting vs. Direct Code Generation
Aspect Direct Code Generation Test-First Prompting
Input Validation Often assumed or generic Explicitly tested for invalid/malicious inputs
Error Handling Frequently missing or inconsistent Tests force defined error states
Edge Cases Left to chance Enumerated in test scenarios
CWE Mitigation Reactive (found during audit) Proactive (defined in spec/tests)
Detailed etching depicting the red-green-refactor cycle for AI coding.

Advanced Prompting Techniques for Better Tests

Not all prompts yield great tests. To get professional-grade outputs, you need to use specific techniques. These aren't magic spells; they are ways to structure information so the LLM pays attention to the right details.

Role Priming

Start by telling the AI who it is. "You are an expert software architect specializing in secure web applications." This shifts the model's internal weights toward more rigorous, standard-compliant outputs. A general assistant might give you a quick hack. An "expert architect" gives you a robust solution.

Few-Shot Prompting

Give the AI examples. If you want tests in a specific style, show it two examples of tests you like. "Here is an example of a well-written test for a login function: [example]. Now write similar tests for a signup function." This aligns the output format and depth with your preferences.

Scenario Enumeration

Don't assume the AI knows all the edge cases. List them. "Consider these scenarios: null input, unicode characters, extremely long strings, and concurrent access." By listing these, you guide the AI to generate tests for each one. Without this list, the AI might skip the tricky stuff.

Common Pitfalls and How to Avoid Them

Even with a good method, things go wrong. Here are the most common issues developers face when using Unit Test First Prompting.

Hallucinated Mocks

AI loves to invent mock objects that don't exist in your testing library. It might call `mock_database.query()` when your library uses `db.execute()`. Always review generated tests for external dependencies. If the AI uses a mock, verify it matches your actual testing framework setup.

Overloading the Prompt

Trying to describe a complex system in one prompt leads to confusion. Break it down. Do one function at a time. If you try to generate tests for a whole module, the AI will likely miss details or mix up contexts. Keep the scope small.

Ignoring Compilation Errors

Sometimes the generated tests won't even compile. Maybe the syntax is slightly off, or the imports are wrong. Don't ignore this. Feed the error message back to the AI. "This test failed to compile with error X. Fix the test code." Iteration is key. The first attempt is rarely perfect.

Whimsical drawing of a lock mechanism blocking security threats via tests.

Tools and Frameworks to Support This Workflow

You don't have to do this manually in a chat window every time. Several tools help integrate test-first prompting into your IDE.

GitHub Copilot allows you to select code and use the `/tests` command. While this often generates tests for existing code, you can adapt it by pasting a function signature and docstring, then asking for tests before writing the body.

For larger projects, consider using configuration files like `.cursorrules` or custom markdown instructions. These act as guardrails. You can set a rule: "Always generate tests before implementation for any new utility function." This ensures consistency across a team without relying on individual memory.

Practical Example: Building a Password Strength Checker

Let's walk through a real scenario. You need a function that checks password strength.

Step 1: The Prompt for Tests

"Write pytest unit tests for a function `check_password_strength(password)`. Rules: Must be 8+ chars, contain uppercase, lowercase, digit, and special char. Return 'Weak', 'Medium', or 'Strong'. Include tests for empty string, only numbers, and mixed cases."

Step 2: Review the Output

The AI generates tests. You notice it didn't test for passwords with spaces. You add that manually or ask the AI to add it. "Add a test for a password containing spaces."

Step 3: The Prompt for Implementation

"Implement `check_password_strength` to pass these tests: [paste final tests]. Use regex for validation."

Step 4: Verification

You run the tests. They pass. You check the code. It looks good. You're done. Total time: 5 minutes. Confidence: High.

FAQ

Is Unit Test First Prompting slower than just asking for code?

Initially, yes, it takes a bit longer because you are managing two steps instead of one. However, it saves significant time in debugging and refactoring later. The confidence you gain from verified tests usually outweighs the extra few minutes spent on prompt crafting.

Do I need to know TDD to use this method?

No, but understanding the basics helps. You don't need to write tests by hand. You need to understand what makes a test valid. If you can read a test and see if it covers the main cases, you have enough knowledge to start.

Which AI models work best for generating unit tests?

Models with strong reasoning capabilities and large context windows perform best. GPT-4o, Claude 3.5 Sonnet, and Gemini 1.5 Pro are currently top choices. Smaller models may struggle with complex edge cases or maintaining consistency between tests and implementation.

What if the AI generates bad tests?

This happens often. Treat the AI as a draft generator. Review the tests critically. Are they testing the right things? Are they too strict or too loose? Iterate on the prompt. Add more specific constraints or examples until the tests match your expectations.

Can this be used for frontend UI components?

Yes, but it requires different tools. You would generate tests using frameworks like Jest or Cypress. The principle remains the same: define the expected behavior (e.g., button click triggers state change) in a test before implementing the component logic.

Similar Post You May Like