In part 1/4 of our new series on unit testing, we’re tackling the basics of what unit testing is and why it should be used.
Unit testing is (or rather, should be) a core practice for any software developer. But not everyone uses unit tests. In JetBrains' The State of Developer Ecosystem 2023 report, 63% of respondents (down from 67% in 2022) said they had unit tests in their projects.
This post is for those new to unit testing and will explain what unit tests are, where they come from, and why they should be used. Watch out for upcoming posts in our series for a comprehensive introduction to unit testing!
Check out all the other parts of this series:
- A beginner’s guide to unit testing 2/4: How to use unit tests?
- A beginner’s guide to unit testing 3/4: Automating unit tests
- A beginner’s guide to unit testing 4/4: Unit testing best practices
What are unit tests?
Unit tests are low-level tests that focus on the smallest individual building blocks of your application. In the context of unit testing, think of a unit as an individual method/function in your code, a line of code, or a class – all isolated from the rest of your code. Unit tests are used to check if each of these small blocks of code work as expected.
One thing to note is that when you perform unit testing, you’re not looking to check connections to external systems (e.g. accessing external databases). Rather, you’re only focusing on these isolated units to see if they function as intended. To do that, you’ll try to remove any external dependencies from your test setup. In subsequent parts of this series, we’ll show you how to do that in practice!
Per the testing pyramid, unit testing should form the basis of all software testing as it helps ensure that any problems are caught as early as possible per the shift-left principle.
There are a few core requirements to unit tests that help you understand the underlying principle. A unit test should be:
- As simple as possible
- Easy to write
- Fast to run
- Quick to debug
- And should focus on checking a single specific behavior of a function, method, or line of code.
The origins of unit testing
So where does unit testing come from? Some attribute the earliest forms of unit testing to John von Neumann’s work before the 1950s, and **unit testing as a concept existed in electrical engineering in the 1970s**
But unit testing as we know it today is said to have started in 1997 with Kent Beck and Erich Gamma releasing the first version of JUnit and starting off the XUnit family of testing tools.
Unit testing really took off as more and more people started building software, personal computing getting more widespread and especially with the advent of the world wide web.
Why use unit tests: the benefits
Despite being very simple, unit tests can greatly contribute to the performance and quality of your applications.
😎 Reap the benefits of unit testing with minimal effort
Symflower automatically generates smart test templates and full unit test suites (beta) with values to explore all possible execution paths in your code. The result? Well-tested applications with minimal investment of manual effort. Try Symflower in your IDE.
- Unit tests are great because they help you catch issues early and in real time as you code. This way, problems don’t cause costly trouble down the line.
- Due to their targeted nature, unit tests don’t require any context. There is no need to configure and access external things like databases in order to be able to test: you’ll just mock any communication with external systems.
- Debugging a unit test is easier than it is to debug a system or integration test. That’s because unit tests point you to the right place in the code that needs fixing. Instead of the entire system, you have a small context that you need to look into.
- Unit tests can also help prevent regressions. If you have unit tests in place, existing functionality isn’t as easy to break, which makes refactoring easier.
- The consistent use of tests is also a great measure for documenting your software. Other team members looking at your code and unit tests together will have an easier time understanding what the function is supposed to do.
- Finally, unit tests are also a great way to manage complexity by breaking things down into small pieces. That’s especially true if you use unit tests in a TDD setting, where unit tests are created before any code is written.
👀 Interested in TDD?
Read our guide about getting started with Test-Driven Development!
Summary: the basics of unit testing
So that should have given you an introduction to the fundamentals of unit testing. In subsequent parts of this blog post series, we’ll cover the topics of:
- How to use unit tests?
- Automating unit tests
- Unit testing best practices
Stay tuned for more and send us your feedback to help us improve these pieces!
Make sure you never miss any of our upcoming content by signing up for our newsletter and by following us on Twitter, LinkedIn or Facebook!