← Knowledge

Public EcoRouter Knowledge · AI-generated content ·

How do I prompt an AI to write meaningful automated tests for my code?

The question

How do I prompt an AI to write meaningful automated tests for my code?

Efficient

To get meaningful automated tests from an AI, you need to move beyond asking it to "write tests" and start describing the behavior and edge cases you care about.

Here is a simple framework for prompting effectively:

1. Provide Context

AI writes better tests when it understands what the code is meant to do.

  • The "What": Paste your function or class.
  • The "Why": Briefly state the purpose. (e.g., "This function handles user registration, ensuring the email is unique and the password meets complexity requirements.")

2. Define the Scope and Framework

Specify the tool and the depth of testing you need.

  • Framework: Tell it explicitly: "Write these tests using Jest/PyTest/JUnit."
  • Style: Do you want unit tests, integration tests, or TDD-style tests? If you don't specify, you might get generic unit tests.

3. Use the "Behavior-Driven" Approach

Instead of asking for lines of code, describe scenarios. Use this prompt template:

"I have the following code [insert code]. Please write a suite of tests for it.

  1. Start with the 'happy path' where inputs are valid.
  2. Include edge cases, such as [mention specific constraints, e.g., empty strings, null values, negative numbers].
  3. Mock any external dependencies like [API calls/Database connections].
  4. Use descriptive test names that explain the expected behavior."

4. Ask for Negative Testing

This is where AI shines. Explicitly ask it to try to "break" your code.

  • Prompt: "Please generate a set of negative test cases that verify how the function handles invalid input. Ensure it throws the correct errors or returns appropriate validation messages."

5. Iterative Refinement

If the first draft is too simple:

  • "Make it more robust": Ask for property-based testing (if your language supports libraries like hypothesis or fast-check).
  • "Explain the gap": If you aren't sure what to test, ask: "What are the most likely points of failure in this logic, and how should I test for them?"
  • "Refactor for testability": If the code is hard to test, ask: "Is this code testable? If not, suggest a refactor to make it easier to write unit tests for."

A Quick Tip

Always verify the tests yourself. An AI might create a test that passes because it mocks something incorrectly or overlooks a side effect. Treat the AI’s output as a draft that helps you reach high coverage faster, rather than a final product.

Answering this used an estimated 90% less than EcoRouter’s configured frontier baseline

Routed to
Efficient
Tokens
374 in / 584 out
Cost
$0.0010
Baseline
$0.0099

Figures recorded by EcoRouter when this answer was generated, and fixed at that moment. Cost comparisons are against a configured reference model, not a measurement of electricity, carbon or water.

Ask about this

Ask EcoRouter a follow-up using this Knowledge as context. Nothing becomes public unless you choose to publish it.

Ask a follow-up
0 views

Comments

No comments yet.