The other day I was talking with a client and they asked me the following question: Why is unit testing important?
Before answering the question it’s important to make sure we’re talking about the same thing. What exactly is unit testing?
What is Unit Testing?
Unit testing is just a specialized form of automated testing that software developers write and run. I like to describe it by explaining that developers are “performing intention checking”. In other words, the unit tests check that the program works and continues to work as the developer intended or planned for it to work.
Yes, there are many specialized topics, such as test-driven development (TDD) and the red-green-refactor development methodology, but I find that they clutter up the purpose that underlies unit testing.
How Unit Testing Improves Manageability
Managers are under pressure to perform. This means that they are responsible for accomplishment through the efforts of others. If a change lowers the team’s performance, they can expect to be held accountable for the consequences. This pressure puts the focus on managing and controlling in ways that can favor the status quo. For too many projects this means that nobody expects the developers to write unit tests – which are critical to ensuring long-term quality and correctness.
Ironically, unit testing helps managers better manage and control projects. Project managers and functional managers are concerned with the day-to-day work of developers. For these managers, unit testing improves manageability in the following areas:
- Visibility and reporting
- Control and correction
- Efficiency and speed
- Planning and predictability
- Customer satisfaction
How Automated Testing Can Help
Automated unit testing is a great way to check that code is working and continues to work as intended.
To be effective, automated tests must meet very specific objectives:
- Every developer must be able to run the combined collection of all the developer’s tests
- The continuous integration (CI) server must be able to run the entire suite of tests without any manual intervention
- The outcome of the tests must be unambiguous and repeatable
The first objective is a suite of automated unit tests that allows any developer to verify that their current changes do not break existing code under test. In fact, the team leader or manager should insist that this happen. This virtually eliminates accidental or unintended side-effect problems.
The reason the second objective is important is so that the CI server can run these tests as part of the full and thorough build cycle. In this way, automated tests allow the CI server to fulfill a key role of efficiently verifying that the system is working properly. Also, any developer needs to be able to run the entire automated test suite with zero setups or configuration.
The final objective is at the heart of manageability. For example, if there are 193 passed unit tests and 7 failed then it must be clear that those 7 failed because something changed in the code. Otherwise, the debate quickly turns toward whether the tests were passing before or if the tests will pass if they are re-run. It is important that these tests be clear, correct, and consistent.
The Bottom Line
If unit testing is written and performed properly and consistently, software projects are a lot more effective at delivering the correct solution in a predictable and managed way.
While the purpose of this post is not to describe how to write your first unit test, I can provide you with 3 key objectives to keep in mind when starting to write your unit tests:
- Readability: Writing test code that is easy to understand and communicates well
- Maintainability: Writing tests that are robust and hold up well over time
- Automation: Writing tests that require little setup and configuration (preferably none)
What are your thoughts about unit testing?
Source: Stephen Ritchie, Pro .NET Best Practices (New York: Apress, 2011). Excerpted in part with permission of the author.