Java Reference
In-Depth Information
4.1
The need for unit tests
The main goal of unit testing is to verify that your application works as expected and
to catch bugs early. Although functional testing accomplishes the same goal, unit tests
are extremely powerful and versatile and offer much more than verifying that the
application works. Unit tests
Allow greater test coverage than functional tests
Increase team productivity
Detect regressions and limit the need for debugging
Give us the confidence to refactor and, in general, make changes
Improve implementation
Document expected behavior
Enable code coverage and other metrics
4.1.1
Allowing greater test coverage
Unit tests are the first type of test any application should have. If you had to choose
between writing unit tests and writing functional tests, you should choose the latter. In
our experience, functional tests are able to cover about 70 percent of the application
code. If you wish to go further and provide more test coverage, then you need to write
unit tests.
Unit tests can easily simulate error conditions, which is extremely difficult to do
with functional tests (it's impossible in some instances). Unit tests provide much more
than just testing, as explained in the following sections.
4.1.2
Increasing team productivity
Imagine you're on a team working on a large application. Unit tests allow you to
deliver quality code (tested code) without having to wait for all the other components
to be ready. On the other hand, functional tests are more coarse grained and need the
full application (or a good part of it) to be ready before you can test it.
4.1.3
Detecting regressions and limiting debugging
A passing unit test suite confirms your code works and gives you the confidence to
modify your existing code, either for refactoring or to add and modify new features.
As a developer, you'll get no better feeling than knowing that someone is watching
your back and will warn you if you break something.
A suite of unit tests reduces the need to debug an application to find out why
something is failing. Whereas a functional test tells you that a bug exists some-
where in the implementation of a use case, a unit test tells you that a specific
method is failing for a specific reason. You no longer need to spend hours trying to
find the problem.
 
 
 
 
 
 
 
 
 
 
Search WWH ::




Custom Search