Tl;dr : a unit test is code that tests other code.
Unit tests are the smallest type of software testing. They are meant to verify that a single piece of code is working as intended. Unit testing is typically automated, and developers run tests as part of the development process to make sure everything is working appropriately.
Unit tests are commonly run on every build or change to the codebase.
- Unit tests are small and typically quick to run. The speed makes them easier to check and hastens development.
- Unit testing can be helpful documentation because it shows how the code is properly used and can give insight into functionality.
- Developers can use unit tests as a helpful sanity check to make sure their code is working as intended and under many scenarios.
- Since unit tests are checking only small chunks of code, they cannot catch large errors that exist on the system level. Other testing methods like integration tests are required for verifying the system behavior.
- Writing and maintaining unit tests can often be difficult because developers are focused on writing deliverable code for customers.
- Unit tests, like all testing, may not cover every edge case or may be incorrect. Therefore, relying too heavily on unit tests creates a false sense of security that the code is working as intended.
Unit tests are just the first level of testing. More types of testing can be read about in my series on software testing and its methods.