Unit tests are automated tests. In other words, unit testing is performed by software (such as a unit testing framework or unit testing tool) and not manually by a developer. This means unit tests allow automated, repeatable, continuous testing. 

Unit tests are positioned on the first (lowest) test level of the test pyramid because unit tests are the fastest and least expensive tests a developer can write. Put another way, unit tests are the first step in testing an application. Once unit tests have been passed, all subsequent tests like integration tests and E2E tests should be performed. These are more computationally expensive, but they test the application in greater detail.

Automation testing pyramid. Unit testing is at the bottom of the pyramid in green, followed by white box integration testing in blue, black box integration testing in orange, and finally E2E tests at the top in red
Software testing pyramid. | Image: Artem Oppermann


Why Do Unit Testing?

The advantage of automation testing (and unit testing specifically) is that these types of testing make it possible to test quickly and thus more frequently, whereas manual testing requires much more human intervention. Unit testing allows developers to detect errors in a timely manner, especially regression errors, which result from changes to the program code. As a result, unit tests are ideal to help identify local errors in the code rapidly during the development phase for new features and code adaptations. 

In a broader sense, there are plenty of good reasons to test software properly. First and foremost is the sobering fact that software always contains errors, which can lead to software misbehavior. This misbehavior, in turn, can cost money on the one hand or, depending on the location or purpose, human lives. In 2018 Uber’s self-driving car caused an accident that killed a pedestrian. More precise testing of the corresponding software in advance could have prevented the malfunction of this autonomous vehicle.

Tests are living documentation. They provide information about the quality of the software and thus build our confidence in it. In addition, once the expected initial difficulties have been overcome, testing can noticeably accelerate software development. 

More From Artem OppermannArtificial Intelligence vs. Machine Learning vs. Deep Learning


Unit Testing Benefits

With unit tests, a software developer can easily uncover changes in the code’s behavior. Unit tests also reveal unintended effects in newly added functions when features are introduced to an existing application.

Unit tests bring many advantages to a software development project:

  • Developers receive fast feedback on code quality through regular execution of unit tests.
  • Unit tests force developers to work on the code instead of just writing it. In other words, the developer must constantly rethink their own methodology and optimize the written code after receiving feedback from the unit test.
  • Unit tests enable high test coverage. 
  • It’s possible to perform automated, high-quality testing of the entire software unit by unit with speed and accuracy. 

We use unit tests to detect errors and problems in the code at an early stage of development. If the test discovers an error, it must necessarily be in the small source code unit that has just been tested.


Unit Testing Tools

There are many ways to implement unit tests. The most popular way is using JUnit (Java Unit), a widely used framework that became the go-to solution for automated unit testing of application methods and classes written in Java. There are a number of similar frameworks and extensions that have adopted the concept of JUnit and enable unit tests in other programming languages: NUnit (.Net), CppUnit (C++), DbUnit (databases), PHPUnit (PHP), HTTPUnit (web development) and more. Such frameworks for unit testing are commonly referred to as “xUnits.”

What is Unit Testing, Why We Use It, and Sample Test Cases | Video: Let’s Build That App


How to Write a Unit Test

The following example shows a simple test used to verify that the comparison operation for a Euro class is implemented correctly. 

public class EuroTest {

  class Euro {

    private Double euroAmount;

    public Euro(double euroAmount) {
      this.euroAmount = Double.valueOf(euroAmount);

    public Double getEuroAmount() {
      return this.euroAmount;

    public int compareTo(Euro euro) {
      if (this == euro) {
        return 0;
      if (euro != null) {
        return this.euroAmount.compareTo(euro.getEuroAmount());
      return 0;

  public void compareEuroClass() {

    Euro oneEuro = new Euro(1);
    Euro twoEuro = new Euro(2);
    // Test that oneEuro equals to itself
    assertEquals(0, oneEuro.compareTo(oneEuro));

    //Test that oneEuro is smaller than twoEuro
    assertTrue(oneEuro.compareTo(twoEuro) < 0);
    //Test that twoEuro is larger than oneEuro
    assertTrue(twoEuro.compareTo(oneEuro) > 0);


If the Euro object behaves as expected, JUnit confirms this with a green bar:

Results of a unit test in JUnit with a green bar running across the screen.
JUnit unit test result. | Image: Artem Oppermann

JUnit acknowledges behavior that deviates from the test with a red bar:

Results of a unit test in JUnit with a red bar running across the screen.
JUnit unit test result. | Image: Artem Oppermann

Related Reading From Built In ExpertsSmoke Testing: An Overview and How-to Guide


What Makes a Good Unit Test?

An effective unit test must fulfill certain features, which are:

  • The individual tests must be independent of each other. The order of execution does not matter and has no influence on the entire test result. Also, if one test fails, the failed test does not have an influence on other unit tests.
  • Each unit test focuses on exactly one property of the code.
  • Unit tests must be completely automated. Usually, the tests are executed frequently to check for regression errors.
  • The tests must be easy to understand to the developers and collaborators who are working on the same code
  • The quality of the unit tests must be as high as the quality of the production code.
Expert Contributors

Built In’s expert contributor network publishes thoughtful, solutions-oriented stories written by innovative tech professionals. It is the tech industry’s definitive destination for sharing compelling, first-person accounts of problem-solving on the road to innovation.

Learn More

Great Companies Need Great People. That's Where We Come In.

Recruit With Us