FresherTechnicalBackend testing
What makes a good unit test?
Try answering first
Model answer
A good unit test is fast, isolated, repeatable, self-checking and readable. These are often remembered as the FIRST properties.
- Fast: it runs in milliseconds, so people run it all the time.
- Isolated: it does not depend on other tests, on test order, or on shared state.
- Repeatable: the same code gives the same result on any machine, with no dependence on the clock, the network or random values.
- Self-checking: it asserts the result itself, and nobody reads logs to decide if it passed.
- Readable: the name states the scenario and the expected outcome, for example
rejects_order_with_negative_quantity, and the body shows arrange, act and assert.
It also tests one behavior, so a failure points to one cause. And it fails for the right reason: breaking the code makes it red, while refactoring that keeps behavior does not.
A good test is a short specification that fails when the behavior breaks.
Key points
- Fast and isolated, with no shared state or order dependence
- Repeatable, with no clock, network or random inputs
- Self-checking, with assertions rather than log reading
- Name states scenario and expected outcome
Likely follow-ups
- Should a unit test contain more than one assertion?
Common mistakes
- Naming tests test1, test2 or testOrder
- Letting one test depend on data created by another