Testing interview question · Most asked
What makes a good unit test?
Summary
A good unit test is fast, deterministic, isolated, and readable; it tests one behavior through the public interface, has a descriptive name, follows Arrange-Act-Assert, and fails for exactly one reason with a clear message.
Longer Answer
Avoid testing implementation details (private methods, exact call sequences) so refactoring doesn't break tests. Prefer real collaborators when they're cheap and deterministic; mock only boundaries such as networks, clocks and randomness.
Code Example
def test_discount_applies_to_orders_over_100():
# Arrange
order = Order(items=[Item(price=120)])
# Act
total = apply_discount(order, percent=10)
# Assert
assert total == 108
Key Points
- Fast and deterministic
- Tests behavior, not implementation
- Arrange-Act-Assert
- Descriptive names