Testing your Programs | Komputasi | Suatu Permulaan

J.E.D.I

6.4 Testing your Programs

Software testing can be done in several tasks or phases. Program testing is concerned with testing individual programs unit testing and their relationship with one another integration testing. This section discusses concepts and methods that allows one to test programs. Unit Testing Unit testing is the basic level of testing. It has an intention to test the smaller building blocks of a program. It is the process of executing each module to confirm that each performs its assigned function. It involves testing the interface, local data structures, boundary conditions, independent paths and error handling paths. The environment to which unit tests can be performed is shown in Figure 6.4. To test the module, a driver and stub is used. A Driver is a program that accepts test case data, passes data to the component to be tested, and prints relevant results. A Stub is program that performs support activities such as data manipulation, queries of states of the component being tested and prints verification of entry. If the driver and stub require a lot of effort to develop, unit testing may be delayed until integration testing. To create effective unit tests, one needs to understand the behavior of the unit of software that one is testing. This is usually done by decomposing the software requirements into simple testable behaviors. It is important that software requirements can be translated into tests. In object-oriented software engineering, the concept of encapsulation is used to define a class, i.e., the data attributes and functions methods or operation are grouped together in a class. The smallest testable units are the operations defined in the class. As opposed to conventional way of testing, operations defined in class cannot be tested separately. It should be tested in the context to which objects of the class are instantiated. As an example, consider the class diagram shown in Figure 6.5. Software Engineering 281 Figure 6.4 Unit Test Environment Driver stub stub result TEST CASES COMPONENT or MODULE input data J.E.D.I The draw method is defined in Shape class. All subclasses of this base class inherit this operation. When a subclass uses the draw method, it is being executed together with the private attributes and other methods within the context of the subclass. The context in which the method is used varies depending on what subclass executes it. Therefore, it is necessary to test draw method in all subclasses that uses it. As the number of subclasses is defined for a base class, the more testing is required for the base class. Integration Testing After unit testing, all classes must be tested for integration. Integration testing verifies that each component performs correctly within collaboration and that each interface is correct. Object-oriented software does not have an obvious hierarchy of control structure which a characteristic of conventional way of developing software. Traditional way of integration testing top-down and bottom-up strategies has little meaning in such software. However, there are two approaches that can be employed in performing integration testing. Thread-based Testing Approach Integration Testing is based on a group of classes that collaborate or interact when one input needs to be processed or one event has been trigger. A thread is a path of communication among classes that needs to process a single input or respond to an event. All possible threads should be tested. The sequence diagrams and collaboration diagrams can be used as the basis for this test. As an example, consider the sequence diagram that retrieves an athlete record as shown in Figure 6.6. In integration testing, just follow the order of the interaction of the objects through their messages. Software Engineering 282 Figure 6.5 Sample Class Hierarchy Shape draw Circle Square Rectangle J.E.D.I The sequence of the test would involve the following steps: 1. The club staff will click the Find Button of the Athlete Record User Interface .

2. The FindAthleteRecordUI Screen will be displayed.

3. The club staff enters the search criteria.

4. The club staff clicks the OK button which will instantiate the

FindAthleteRecord Controller. 5. The FindAthleteRecord Controller communicates with the DBAthlete to populate the PCLAthleteList.

6. The AthleteListUI Screen will be displayed to show a list of athletes that

satisfy the search criteria. 7. The club staff highlights the athlete he wants to retrieve. 8. The club staff clicks the OK button or press enters. 9. The AthleteRecordUI Screen should display the athlete information on the appropriate text field. Software Engineering 283 Figure 6.6 Retrieving an Athlete Record J.E.D.I Use-based Testing Approach Integration Testing starts by identifying independent classes. Independent classes are those classes that do not use or use few server classes. Independent classes are tested first. After testing these classes, the next set of classes called dependent classes are tested. Dependent classes are those classes that use the independent classes. As an example, consider the classes shown in Figure 6.2. The Athlete and PCLAthlete classes can be considered as independent classes. Their methods can be tested independently. If tests are successful, the DBAthlete class can be tested. To help manage integration, clustering can be used. Clustering is the process of defining a set of classes that are integrated and tested together. They are called clusters because they are considered as one unit. In the examples, these three classes combined together can be known as the DBAthlete cluster. Regression Testing Sometimes when errors are corrected, it is necessary to re-test the software components. Regression testing is re-execution of some subset of tests to ensure that changes have not produced unintended side effects. Re-testing occurs for the software function that was affected, software component that has been changed and software components are likely to be affected by the change. Software Engineering 284 Figure 6.7 DBAthlete Cluster J.E.D.I

6.5 Test-driven Development Methodology