Cosmic Module
S
Qubits of DPK
March 30, 2026
Core SWE @ Google
Unit testing is one of the most fundamental practices in modern software engineering.
A unit test verifies the behavior of a small, isolated part of the code, usually a single function or class.
This chapter explains how unit tests should be designed, why they are valuable, and how they contribute to reliable software systems.
The central idea of this chapter is:
Unit tests provide fast feedback and allow engineers to verify small pieces of code independently.
By testing small units of code, engineers can detect problems early and maintain confidence when modifying software.
What Is a Unit Test?
A unit test verifies the correctness of a single unit of code.
A unit may be:
- a function
- a method
- a class
- a small module
Unit tests ensure that each component behaves correctly in isolation.
Example of a Unit Test
Imagine a function that calculates the sum of two numbers.
java
QUBITS OF DPK
A unit test might verify that the function produces the correct output.
java
QUBITS OF DPK
This test confirms that the function works as expected.
Why Unit Testing Is Important
Unit tests offer several benefits.
Because unit tests are small and fast, they can be run frequently during development.
Isolation in Unit Tests
Unit tests should test only one component at a time.
Dependencies on other components should be minimized.
For example, if a function interacts with:
- databases
- network services
- external APIs
these dependencies should usually be replaced with test substitutes.
This allows the unit test to focus only on the behavior of the code being tested.
Characteristics of Good Unit Tests
Effective unit tests share several important characteristics.
These characteristics ensure that unit tests remain reliable and useful.
Writing Clear Test Cases
Each unit test should verify a specific behavior.
Tests should clearly show:
- what input is given
- what output is expected
- what behavior is being validated
Clear test cases help engineers understand system behavior quickly.
Testing Edge Cases
Unit tests should not only verify normal behavior.
They should also test edge cases.
Examples of edge cases include:
- empty inputs
- extremely large values
- unexpected inputs
- boundary conditions
Testing edge cases helps ensure that code behaves correctly in unusual situations.
Test-Driven Development (TDD)
Some teams follow a development approach called Test-Driven Development.
In this approach:
- #Write a test that describes the desired behavior.
- #Run the test and observe it failing.
- #Write code that satisfies the test.
- #Run the test again to confirm success.
This approach ensures that tests are written early and that code is designed with testing in mind.
Maintaining Unit Tests
Unit tests must evolve alongside the code they test.
When code changes, tests may need to be updated to reflect new behavior.
However, tests should not be modified simply to make them pass.
If a test fails, engineers should first determine whether:
- the code contains a bug
- the test is incorrect
- the behavior of the system has changed intentionally
Careful maintenance ensures that tests remain meaningful.
Avoiding Overly Complex Tests
Unit tests should remain simple.
Tests that become too complex may be difficult to understand and maintain.
Instead of writing one large test, engineers should create multiple smaller tests that each verify a specific behavior.
Simple tests are easier to maintain and debug.
Unit Testing as a Development Habit
Successful engineering teams treat unit testing as a normal part of development.
Engineers are expected to:
- write tests for new code
- maintain existing tests
- fix failing tests quickly
This culture ensures that systems remain stable as they evolve.
Key Lessons from Chapter 12
This chapter emphasizes several important principles.
- #Unit tests verify the behavior of small code components.
- #Tests should be fast, independent, and repeatable.
- #Edge cases should be included in testing.
- #Unit tests enable safe refactoring of code.
- #Maintaining tests is as important as writing them.
Simple Explanation (For Non-Technical Readers)
Imagine building a large machine made of many small parts.
Before assembling the entire machine, engineers test each individual part to make sure it works correctly.
If each part works properly, the final machine is more likely to function correctly.
Unit testing follows the same idea.
By verifying each small piece of code individually, engineers ensure that the entire system works reliably.