DEV Community

Sean Coughlin
Sean Coughlin

Posted on • Originally published at blog.seancoughlin.me on

What is a unit test?

Unit Testing Background

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 testing can fall into the category of TDD or test-driven development. TDD is a common method in extreme programming and agile where unit tests are written before the code.

Unit tests can sometimes be difficult to write because code often relies on outside components. In these cases, stubbing or mocking are common techniques to implement unit tests.

Unit Testing Frameworks

There are hundreds of testing frameworks in existence because unit testing is so common. Wikipedia maintains a long list of testing frameworks for many languages. Some of the most popular frameworks are Junit for Java, Mocha for JavaScript, and PyTest for Python.

Advantages

  • 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.

Disadvantages

  • 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.

Further Reading

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.

Latest comments (0)