juniorUnit Testing
What is the purpose of assertions in unit testing?
Updated May 6, 2026
Short answer
Assertions verify that expected outcomes match actual results.
Deep explanation
Assertions are core to unit testing as they validate whether a function behaves correctly. If an assertion fails, the test fails. They help define expected behavior explicitly and act as checkpoints in test cases.
Real-world example
Checking if a user authentication function returns a valid token.
Common mistakes
- Not adding enough assertions in a test case.
Follow-up questions
- What happens when an assertion fails?
- Can tests have multiple assertions?