Unit Testing
Unit Testing
– In a task in WBS
– As the s/w component to be tested is small in size & simple in function, it is easier to
design, execute, record & analyze test results and easier to locate & repair defects.
• Unit testing is testing whether the smallest possible testable s/w component works as
specified in its specification.
Documents that provide inputs for the unit test plan are project plan, requirements,
specification and design documents,
o Need information from the unit specification & detailed design description
o Planner decides which features of each unit will be tested
o Features not covered by the tests should be mentioned and their risks should be
assessed
o Input output characteristics associated with each unit should be identified
o Planner – refines the plan – add details to the approach, resources & scheduling
portions of the test plan
Test hareness for individual classes will be more complex than those needed for individual procedures.
➢ The tester must determine from the results whether the unit has passed/failed the test. If the
test is failed, the problem should be recorded in the Test Incident report.
➢ Deviations from the expected behavior should be recorded and described in detail. This gives
clues to locate defects
➢ During testing, tester may decide whether additional tests are required
➢ When a unit fails, reasons may be
– A class is mainly for reuse. So, a residual defect in a class affect every instance of reuse.
• A delay in finding these defects, will affect wherever these classes are used.
– A class may have different features. Clients may use different features of the class.
Unless the class is tested as a unit first, there may be features of a class that may never
get tested.
– A class is a combination of data and methods, the sync of data and methods should be
tested at the unit level. Otherwise it may lead to a confusion later.
– Stress testing → to test the multiple instances (objects) of classes by different users
– State based testing → For the messages that trigger the state transition
• Test the object through its life cycle from its construction(birth) to
destruction(death).
• Test simple methods first and then test the complex functions.
• Test the methods from private through public methods. Private methods are
methods that are not visible outside the object/class.
• Send a message to every method atleast once. This ensures that every method
is tested atleast once.
• Test the constructor methods first. Each class may get constructed by multiple
constructor messages. All the constructor methods should be tested
individually.
• Test the get methods or accessor methods in order to ensure that the variables
in the class definition are accessible by the appropriate methods.
• Test the methods that modify the object variables. Test the methods that test
the contents of the variables, set/update the contents of variables, methods
that loop through various variables
– Encapsulation is meant to hide the details of class from the clients of the class
– A class can:-
• Define new variables & methods that are specific to it & not applicable to parent
• When a class is introduced for the first time, all variables & methods have to be tested fully.
• From then on whenever a class is derived from a parent class, the following need to be tested:-
✓ The changes made to the base class variables & methods & attributes
✓ New variables & methods that have been introduced in derived class
Testing all changes or new additions at the point of creation & selective re-testing of other unchanged
attributes is called incremental class testing.