Software Development

What is Unit Testing?

Quick answer

Testing individual pieces of code, such as functions or classes, in isolation to confirm they behave as expected.

Unit Testing, explained

A unit test checks one small unit of code with specific inputs and verifies the output. For example, a function that calculates reading time for a blog post might be tested to confirm that a 1,000-word article returns about five minutes at a set reading speed, and that an empty article returns zero.

Unit tests are fast and run automatically, often on every code change in a CI/CD pipeline. They catch regressions early and make refactoring safer. Popular frameworks include pytest for Python, JUnit for Java, and Jest or Vitest for JavaScript and TypeScript. Test-driven development is a practice where developers write the test before the code. Unit tests complement, but do not replace, integration and end-to-end tests.

Related terms

Guides that mention Unit Testing