HTML and CSS Reference
developed by perusing the tests. This point is reinforced by the fact that unit tests
also help us write cleaner interfaces, because the tests force us to use the interfaces as
we write them, providing us with shorter feedback loops. As we'll see in Chapter 2,
The Test-Driven Development Process , one of the strongest benefits of unit tests is
their use as a design tool.
Writing unit tests is not always easy. In particular, writing good unit tests takes
practice, and can be challenging. The benefits listed in Section 1.5, Benefits of Unit
Tests all assume that unit tests are implemented following best practices. If you write
bad unit tests, you might find that you gain none of the benefits, and instead are
stuck with a bunch of tests that are time-consuming and hard to maintain.
In order to write truly great unit tests, the code you're testing needs to be
testable . If you ever find yourself retrofitting a test suite onto an existing application
that was not written with testing in mind, you'll invariably discover that parts of
the application will be challenging, if not impossible, to test. As it turns out, testing
units in isolation helps expose too tightly coupled code and promotes separation of
Throughout this topic I will show you, through examples, characteristics of
testable code and good unit tests that allow you to harness the benefits of unit
testing and test-driven development.
In this chapter we have seen the similarities between some of the ad hoc testing we
perform in browser consoles and structured, reproducible unit tests. We've gotten
to know the most important parts of the xUnit testing frameworks: test cases, test
methods, assertions, test fixtures, and how to run them through a test runner. We
implemented a crude proof of concept xUnit framework to test the initial attempt
Integration tests were also dealt with briefly in this chapter, specifically how we
can realize them using said xUnit frameworks. We also looked into how integration
tests and unit tests often can get mixed up, and how we usually can tell them apart
by looking at whether or not they test isolated components of the application.
When looking at benefits of unit testing we see how unit testing is an investment,
how tests save us time in the long run, and how they help execute regression tests.
Additionally, refactoring is hard, if not impossible, to do reliably without tests.