Asking ChatGPT to Write the Tests Before the Function
Generating tests with AI before the function makes the AI's actual code much easier to trust.
7 min · Reviewed 2026
The big idea
If you ask ChatGPT 'write me a function that does X' you get code you have to trust. If you first ask 'write me 5 tests for a function that does X' and then 'now write the function to pass these tests' — you can run the tests and *see* it works. Same AI, way more trustworthy.
Some examples
You ask Claude for tests for a 'parse a date string' function — the tests reveal you never decided about empty strings.
ChatGPT writes tests for a leap-year checker that catch year 1900 (not a leap year, despite divisible by 4).
Cursor writes tests for a username validator that include emoji — you decide your policy on the spot.
Copilot tests a sort function with already-sorted, reverse-sorted, and one-element arrays — pro-grade coverage you'd skip alone.
Try it!
Pick a tiny function (max 10 lines) you need this week. Ask an AI for 5 tests first. Read them, fix any wrong ones, then ask for the function. Run the tests.
End-of-lesson check
8 questions · take it digitally for instant feedback at tendril.neural-forge.io/learn/quiz/end-builders-aicoding-test-first-with-ai-r7a8-teen
What is the main idea of "Asking ChatGPT to Write the Tests Before the Function"?
Generating tests with AI before the function makes the AI's actual code much easier to trust.
Use AI as the final authority for the whole decision
Avoid checking the answer once it sounds polished
Focus only on speed instead of judgment
Which concept is most central to "Asking ChatGPT to Write the Tests Before the Function"?
AI testing
test-driven development
specifications
verification
Which use of AI fits this topic best?
Let the AI decide what matters without your review
Use the answer before checking whether it fits the situation
You ask Claude for tests for a 'parse a date string' function — the tests reveal you never decided about empty strings.
Use the first answer without checking it
What should a careful learner remember about "The rule"?
Tests first, code second. If the AI's tests look wrong, fix them — the function is now easy.
Skip the context so the tool can guess faster
Treat the output as private even after sharing it online
Use the answer without checking the source
You want to use AI after this lesson. What is the safest next step?
Act immediately because the AI answer is written clearly
Use the AI answer as a draft, then check it against a reliable source.
Hide uncertainty so the final answer looks cleaner
Use private or sensitive details before checking permission
How should AI output about test-driven development be treated?
As proof that no other source is needed
As a replacement for context, consent, or expert review
As a draft or helper output that still needs human judgment and verification
As something that becomes correct when it sounds confident
Name one way to verify an AI answer about test-driven development.
Which action would help you apply "Asking ChatGPT to Write the Tests Before the Function" responsibly?
Use the tool to avoid thinking through the tradeoff
Keep going even if the output conflicts with a trusted source
Use the first answer without checking it
ChatGPT writes tests for a leap-year checker that catch year 1900 (not a leap year, despite divisible by 4).