Search⌘ K
AI Features

Catching Common Errors and Asserting Exceptions

Explore techniques for catching typical coding mistakes like off-by-one errors and logic flaws through unit tests. Understand how to assert exceptions for business rules, such as validating username length, to improve error handling and test robustness.

Catching common errors

The traditional view of testing is to think of it as a process to check that code works as intended. Unit tests excel at this and automate the process of running the code with known inputs and checking for expected outputs. As we’re human, all of us make mistakes from time to time as we write code and some of these can have significant impacts. There are several common simple mistakes we can make and unit tests excel at catching them all. The most likely errors are the following:

  • Off-by-one errors

  • Inverted conditional logic

  • Missing conditions

  • Uninitialized data

  • Wrong algorithm

  • Broken equality checks ...

Lowercasing username example