Introduction to Testing Pyramid and Unit Testing
Learn the fundamental concepts of unit testing, and how it fits into the testing pyramid.
What is unit testing?
Unit testing refers to a methodology in which we write some code to check the validity of a unit.
What is a unit??
This can be a single method, a class, or multiple classes that can be tested.
What makes a good unit test?
The following are some characteristics of a well-written unit test.
- It runs quickly.
- It’s automated and repeatable.
- It’s fully isolated—meaning it runs independently of other tests.
- It’s always consistent—meaning it produces standardized results irrespective of the changes made.
- It’s readable—meaning it acts as documentation for the behavior of the unit.
What are the benefits of unit testing?
- Detects bugs in the early stages of the Software Development Life Cycle (SDLC).
- Makes sure that the code works as expected.
- Provides confidence when refactoring the existing code.
- Helps new developers understand the current behavior of the system.
Now, let’s see how a unit test is placed in the testing pyramid.
Introduction to testing pyramid
Programmer Martin Fowler famously said: “The test pyramid is a way of thinking about how different kinds of automated tests should be used to create a balanced portfolio. Its essential point is that you should have many more low-level UnitTests than high-level BroadStackTests running through a GUI.”
The following are the different levels at which an application is tested in a software project.
- Unit tests
- Integration tests
- UI tests
The diagram below shows how each test represents a level in the testing pyramid.
The higher we climb the pyramid, the tests become more complicated and execute slower. This also increases the integration level. At the bottom of the pyramid, we have multiple simple units.
The cost of fixing a bug also increases as we move to the different levels of the testing pyramid.