Page 164 -
P. 164

Unit Testing

                          Before a build is delivered, the person or program building the software should execute
                          unit tests to verify that each unit functions properly. All code is made up of a set of objects,
                          functions, modules, or other non-trivial units. Each unit is built to perform a certain func-
                          tion. The purpose of unit testing is to create a set of tests for each unit to verify that it per-
                          forms its function correctly. Each unit test should be automated: it should perform a test
                          without any input or intervention, and should result in a pass (meaning that the test gen-
                          erated the expected results), failure (the results of the test differed from what were
                          expected), or error (meaning the code reached an error condition before the test could
                          pass or fail). Many people require that unit tests have no dependencies on external sys-
                          tems (networks, databases, shared folders, etc.).

                          Automated unit testing is a stepping stone to test-driven development. Test-driven develop-
                          ment is a programming technique in which a programmer writes the unit tests before he
                          writes the unit that they verify. By writing the tests first, the programmer ensures that he
                          fully understands the requirements. It also guarantees that the tests will be in place, so
                          that they aren’t left until after all of the other programming activities are completed (and
                          then possibly dropped, due to schedule pressure).
                          The main activity in unit testing is creating test cases that verify the software. A test case is a
                          piece of code that verifies one particular behavior of the software. Each test should be able
                          to run without any user input; its only output is whether it passed, failed, or halted due to
                          an error. The test cases for a software project are generally grouped together into suites,
                          where there may be a number of suites that verify the entire software. It’s often useful to
                          design the suites so that each one verifies specific units or features; this makes the test
                          cases easier to maintain.

                          The most common (and effective) way for programmers to do unit testing is to use a
                          framework, a piece of software that automatically runs the tests and reports the results. A
                          framework typically allows a programmer to write a set of test cases for each unit. Most
                          frameworks provide an automated system for executing a suite of unit tests and reporting
                          the results. This allows a full battery of unit tests to be executed automatically at any time,
                          with little or no effort. Unit testing frameworks are available for most modern program-
                          ming languages.

                          The framework usually provides some sort of object model, API, or other language inter-
                          face that provides test cases with functionality for reporting whether the test passed or
                          failed. Most frameworks allow the programmer to indicate which tests are associated with
                          various units, and to group the test cases into suites. Table 7-9 shows some of the test
                          frameworks available for various languages. (This list is by no means exhaustive—there
                          are many other frameworks available for these and other languages.)








                   156  CHAPTER SEVEN
   159   160   161   162   163   164   165   166   167   168   169