How can you tell whether a unit test written by AI really does anything?
Suggested answer
The interviewer wants to know whether you treat tests as something that needs checking, or feel safe as soon as you see green. A good answer shows that you understand a test is valuable when it goes red while the code is wrong.
The main idea: a test that is green on correct code proves nothing yet. The way to check is to break the code on purpose. You change one small thing, such as flipping a condition, removing a line or changing a constant, and run the tests again. If they stay green, the test doesn't protect that spot. This technique is called mutation testing, and there are tools that do it automatically, such as Stryker for JavaScript.
You can add four habits:
- Say what needs protecting when you ask AI to write tests, for example "two users never share data", instead of just "write tests for this file".
- Don't trust coverage on its own. Coverage only tells you which lines of code ran, not whether the result was checked. Two test suites can both have 100% coverage while one catches far fewer bugs.
- Read every assert. Checks like "it responded" or "it contains this word" are very loose. Comparing against the full expected result is much stricter.
- Try exactly the bugs that have happened before. Every bug you have hit should have at least one test that goes red if it comes back.
Bonus points: give a concrete example, such as a bot that has tests for its expense-logging feature but only tried one user, so when the code accidentally used a single shared list for everyone, the tests stayed green. Only after adding a test with two users did the bug show up.