Programmers
use unit testing to verify how well a specific section of their code
works. It is used to check that
the new sections written do not affect the functionality of the previously
written sections. Basically, unit
testing is done to see if the code does what the developer thinks it should
do. In the “Pragmatic Unit Testing
in Java” chapter we read from The Pragmatic Programmers book, the authors describe unit testing as
“essential.” It is an inexpensive
way to gain confidence in the program and makes the development process run
more smoothly.
With a short story, the chapter compares
a case in which a developer, Dale, uses unit testing with a case in which
another developer, Pat, does not.
Pat cranks out method after method, stopping every so often to see that
it all still compiles. He works
quickly, but when the deadline approaches, the code is completely
nonfunctional. He spends hours
working with the debugger. When
one bug is fixed, many more present themselves and Pat misses the project
deadline. Dale, on the other hand,
works more slowly, writing a section then writing a test to go along with
it. He checks that it does what
it’s supposed to do before he moves on to the next step. He rarely needs to use the debugger and
when glitches happen, they’re easy to fix. He has a working copy ready to present when the deadline
rolls around. Unfortunately, I
read this article right before the iteration 1 due date for our project and
realized my partner and I had been employing Pat’s faulty technique. We realized our mistake a bit too late,
but the CxxTesting lab assignment that followed, taught us some valuable skills
for unit testing to use on the rest of the iterations.
CxxTest is a specific framework for
unit testing. The programmer
writes the header file containing the assertions to be verified. The header file creates the class that
will implement the tests, which saves the programmer time. The tests are run, checking each
assertion, and a success rate is returned to the tester. If any tests failed, it indicates which
ones, generally with an error message saying what was expected and what was
found. This lets the developer
know where to focus their attention to fix the bug. To help find errors quicker, it is helpful to give the tests
descriptive names, too. Some
common assertion macros used for writing tests include:
TS_ASSERT
TS_ASSERT_EQUALS
TS_ASSERT_DIFFERS
TS_ASSERT_LESS_THAN
TS_ASSERT_THROWS
There
are many more assertions available for the tester to use in a variety of
situations. The main advantage to
CxxTesting is that the framework is very portable. Everything needed to run the tests is included in the header
files written and its own library.
No external libraries are needed which makes CxxTests easy to implement.
Programmers
tend to not be very fond of unit testing because it can be tedious and seems to
stand in the way of actually coding more of their program right away. It can feel time consuming and
difficult. But how much effort
should one put into unit testing?
The answer is that developers should put in as much effort as they need
to convince themselves that the code (previously and newly written) is working
as well as expected in order to move ahead with the next section in the
project. With more detailed
testing, bugs are easier to find and fix, and programmers gain more confidence
in their code when the tests are completed successfully. Putting more effort into unit testing
translates into more valuable feedback for the developers. Dale’s “slow and steady” approach,
which places the importance of quality before speed, although monotonous at
times, is the most beneficial approach when developing software and helps
prevent the headaches that come along with debugging.