Guides /

What Is Unit Testing?

An introduction to what unit testing is, why it matters, and how it differs from other common types of testing.

Unit testing is a quality assurance technique performed during the coding stage of a software development project to test that each individual element of the application performs as required. Unit testing checks each component building block of an application to ensure that it functions correctly (as expected).

Unit tests are essential for fast, safe software change and transformation. They are an especially part of application modernization projects where code is being refactored, while effective Continuous Integration/Continuous Delivery (CI/CD) isn’t possible without good automated unit testing.

The benefits of unit testing

Identifying, diagnosing, and fixing bugs gets harder the closer code changes get to production deployment. Unit testing is designed to be performed at the earliest possible stage, and the benefits are significant.

Avoid regressions

The fundamental purpose of unit testing is to protect against regressions that might be inadvertently introduced when code changes are made. Such regressions can be embarrassing, expensive, and damaging to your business. Good unit testing illustrates how code changes have altered the behavior of an application and precisely identifies the cause, giving developers the information they need to avoid accidental errors.

Faster and cheaper

Unit testing saves time and money by enabling developers to fix problems early in the development process instead of waiting until later testing phases – like functional, integration or end-to-end testing – where negative outcomes result in much greater delays and expense.

Less waste and context switching

Unit testing promotes agility and reduces waste. Developers don’t have to wait on other teams to find out what impact code changes have had – they can get feedback fast, identify regressions early, and take action without waiting for other people or process steps. Even large unit test suites that take some time to execute will provide feedback faster than other types of testing.

Insight and understanding

That’s not all: unit tests give developers insight into the code base they’re working on. Established enterprise applications can often run into millions of lines of code written by hundreds of different people – many of whom might not even work in the same company today. A well-constructed unit test suite can act as documentation that explains what existing code does, in addition to validating its functionality.

Enable modern development

Modern software development approaches like agile and DevOps usually rely on Continuous Integration (CI) pipelines to provide the speed and automation needed for fast, high-frequency code changes. Good unit testing is a key component in CI as it provides the confidence teams need to change complex applications fast, without fear of regressions.

Unit testing vs integration testing and end-to-end testing

Numerous types of software testing may be involved in a software delivery process - unit testing, integration testing and end-to-end testing are common elements of the ‘testing pyramid’.

Unit tests are low level tests which form the initial foundation on which all the other tests are built. Used to validate a specific unit of software, they involve testing individual methods and functions used by your software, and are generally cheap to automate and quick to run.  

Integration testing considers how different modules or service used by an application work together, validating complex scenarios that often require external resources and verifying how well code works with external dependences. They are more expensive to run than unit tests as they require multiple parts of the application to be up and running. If you find your unit tests have too many elements or dependencies, you may actually be writing an integration test.

End-to-end testing validates an entire product, process or ‘user journey’ from beginning to end by replicating user behavior in a complete application environment to ensure that the application works as expected. End-to-end tests may be necessary to validate user experience across connected or complex systems, but they are slow, expensive to perform and hard to maintain. Shifting testing left helps to reduce the need for end-to-end testing and speeds up software delivery.

What is code coverage?

Code coverage is used as a way to determine how well source code is covered by the unit tests, so you can understand how much of the code is tested and make a related judgement on the risk of code changes.

Code coverage can me measured in various ways, including function coverage, statement coverage, branch coverage, condition coverage, and line coverage. Whichever option is chosen, a simple percentage is the most common metric.

  • Function coverage: how many of the functions defined have been called in unit tests
  • Statement coverage: the number of statements in the program that have been executed in unit tests
  • Branches coverage: how many of the branches have been executed in unit tests
  • Condition coverage: the number of boolean sub-expressions that have been tested to true and false values in unit tests
  • Line coverage: how many lines of source code have been tested by unit tests

Historically, many organizations have assumed it’s best to aim for the the highest possible level of code coverage. But not all unit tests have the same value. Aiming for unnecessarily high coverage can result in significant amounts of wasted effort, and sometimes even ‘gaming the system’ with low quality tests that allow teams to hit the number. It’s more important to understand what parts of your codebase are covered and why that matters.

Unit testing best practice

There are few hard and fast rules when it comes to unit testing, but some common themes are good practice. Most developers have their own view but examples include:

Only test one thing

Make sure unit tests are designed to only test one thing at a time so you can isolate a unit and make sure it works.

Keep tests as simple as possible

The more complex a test is, the more likely it will have bugs. The lower complexity your test, the easier it is to understand and maintain, and the less likely it is to have bugs.

Create consistent naming

Think about naming conventions and where possible try to adopt names that are easy to understand, and convey what the test method is about and the scenario they are testing.

Tests should be fast

Unit test execution should be measured in milliseconds rather than seconds, so should be developed to run as quickly as possible. Otherwise, developer time will be wasted. Naturally, fast tests can be a result of writing simple tests.

Tests should be readable

If your tests are hard to read, developers are more likely to misunderstand them and introduce bugs. Using the Arrange, Act, Assert pattern is the best way to define the different phases.

Tests should be deterministic

Your tests should be deterministic and always present the same behavior if no changes were made to the code under the test, so that developers trust them.

Common unit testing challenges

Arguably the biggest challenge with unit testing is the time they take to create and maintain, which distracts developers and slows down software delivery. Developers might spend anywhere between 20% and 50% of their time just one unit tests. 

Unit tests, although vital, are often seen as unproductive work by IT leaders because they’re never actually delivered to end users, and developers think writing them is tedious, repetitive work (40% would prefer never to write on again).

Good coverage is hard to achieve if unit tests haven’t been written from day 1. On average, writing and developing a single unit test can take a developer 10 minutes - and tens of thousands might be needed in order to achieve high coverage.

The time needed to write unit test code isn’t the only drain on developer time. Writing a unit test is only part of the job because in order to do it effectively you must first understand what the code in question does - which can be difficult and time-consuming task, especially when it was written by someone else or it is poorly documented.

Finally, unit tests are only as good as the people writing them. Developer experience and talent varies, which means that the quality, style, and consistency of the tests can vary hugely, making maintenance hard.

Using AI to automate unit test writing

Automating tasks such a unit test writing is an ideal application of AI for code, enabling you to accelerate your shift left and catch regressions earlier in your pipeline.

Diffblue Cover can write a unit test in about 2.5 seconds, enabling developers to achieve meaningful test coverage quickly so you can modernize your applications faster, reduce bottlenecks, allow for agile planning, and improve developer experience.

Diffblue Cover key features

Diffblue Cover gives enterprises and developers the ability to unlock more value from unit testing, enabling the fast delivery of high-quality code

Thanks to patented reinforcement learning AI technology, Diffblue Cover can autonomously write more tests in hours than a typical developer could create in a year. Cover also comes with a suite of supporting tools to help enterprises write and maintain better code, faster. Key features include:

Diffblue Cover Core

Cover Core is the AI for Code engine at the center of the Diffblue Cover platform. It autonomously writes and maintains Java unit tests at massive scale. Learn more about Cover Core.

Diffblue Cover Reports

Cover Reports provides granular insight into test coverage. It goes beyond high-level coverage metric to provide actionable insights that improve quality and efficiency. Learn more about Cover Reports.

Diffblue Cover Optimize

Cover Optimize cuts unit test execution times for quicker deployment and lower costs. Learn more about Optimize.

Diffblue Cover Refactor

Cover Refactor automatically updates existing code to make it more testable, automatically increasing test coverage. Learn more about Refactor.

Take the next step

Autonomous unit test writing and maintenance enables you to identify and fix issues early, keeping you ahead of the competition and able to adapt to change

To find out more about how Diffblue Cover can improve your unit testing, contact us today to book a demo.