软件工程 实践者的研究方法 第24章答案
Problem:
In your own words, describe why the class is the smallest reasonable unit for testing within an OO system.
Answer:
4633-19-1P SA: 9420
SR: 6376
Classes are the most important and basic element in an Object Oriented system. A class is a template which contains the various attributes and functions of an object. It encapsulates data and operations. In OO system, an operation can not be created by itself without being a part of a class.
Classes usually encapsulate data and operations to perform a certain function. As a class is the basic structure used to provide functions, it can be considered as the smallest reasonable unit for testing within an OO system.
As the methods in the OO system are packaged as a unit of classes, testing just the methods one-by-one would be sterilized and would not uncover errors.
Testing the whole class as one unit will make the testing more efficient and productive. This is especially appropriate if strong consistency and loose coupling is applied effectively in the class design.
Problem:
Why do we have to retest subclasses that are instantiated from an existing class, if the existing class has already been thoroughly tested? Can we use the test-case design for the existing class?
Answer:
We have to retest subclasses that are instantiated from an existing class because the operations defined in an existing class may be used in a different context when it is in a subclass. That means same operation may be used in different contexts depending on whether it is in a subclass or an existing class. So it is necessary to test the operations in the context of each of the subclasses.
If the subclass instantiated from an existing class is used within the same problem domain, it is likely that the set of test cases designed for the existing class can be used when testing the subclass. However, if the subclass is used in an entirely different context, the existing class test cases will have little applicability and a new set of tests musts be designed.
Problem:
Why should “testing” begin with object-oriented analysis and design?
Answer:
4633-19-3P SA: 9420
SR: 6376
The testing begins with object-oriented analysis and design because it provides substantial information about the structure and behavior of the system. At each stage, the object oriented models can be tested in an attempt to uncover errors prior to their propagation to the next iteration.
Problem:
Derive a set of CRC index cards for SafeHome, and conduct the steps noted in Section 24.2.2 to determine if inconsistencies exist.
Answer:
A CRC Model is a collection of cards those are divided into three sections.
1. Class
2. Responsibility
3. Collaborator
A CRC is a collection of standard index cards that represent classes. The cards are divided into three sections. Along the top of the card you write the name of the class. In the body of the card you list the class responsibilities on the left and the collaborators on the right.
The SafeHome security function enables the homeowner to configure the security system when it is installed, monitors all sensors connected to the security system, and interacts with the homeowner through the Internet, a PC, or a control panel.
CRC index cards for SafeHome:
Problem:
What is the difference between thread-based and use-based strategies for integration testing? How does cluster testing fit in?
Answer:
4633-19-5P SA: 9420
SR: 6376
Thread-based testing:
• Thread-based testing integrates the set of classes required to respond to one input or event for the system.
• Each thread is integrated and tested individually.
• Regression testing is applied to ensure that no side effects occur.
Use-based testing:
• Use-based testing integrates the set of classes required to respond to one use case.
• It begins testing by from independent classes (the construction of the system by testing those classes).
• After independent classes are tested, dependent classes (the next layer of classes) which depend on independent classes are tested. This sequence of testing layers will be continued until the entire system is constructed.
Cluster testing is one step in the integration testing of object oriented software. In this step, a cluster of collaborating classes is exercised by designing test cases that attempt to uncover errors in the collaborations.
Problem:
Apply random testing and partitioning to three classes defined in the design for the SafeHome system. Produce test cases that indicate the operation sequences that will be invoked.
Answer:
4633-19-6P SA: 9420
SR: 6376
Random testing for safe home system classes:-
Testing in the small focuses on a single class and the methods that are encapsulated by the class. Random testing and partitioning testing are methods that can be used to exercise a class during OO testing.
When we consider the safe home system, we have the following three classes
1. Control panel
2. Sensor
3. Communication
Operations that are manipulated are adding, deleting, reformatting, and selecting. Each of these operations can be applied for control panel, sensor and communication classes.
Minimum behavioral life history of an instance of above classes indicates following operations.
Open * window * add * select * reformat * delete * close
A variety of different operations sequence can be generated randomly.
Test cases for control panel class:-
Test case
In the control panel class there are operations like. , comparing, locked, selecting.
Case 1:
Open * Read * Compare * Lock * Select * close
Case 2:
Open * enter password * Read * Compare * Request look up * lock * Max tries * Select * Close
Partition testing at the class level:-
• Partition testing reduces the number of test cases that are required to exercise the class in much the same manner as equivalence partitioning
• Partition testing is breaks down the input domain of a program into classes of data from which test cases can be derived.
• Input and Output are categories the class operations bases on their ability to change the state of the class.
We consider the common operations of the three classes:
Adding, deleting, reformatting, selecting.
Here, Tests are designed in a way that exercises operations that change and those that do not change state separately.
Test Case :
Open * Add * Select * Reformat * Delete * Close
Test Case :
Open * Window * Add * Select * Reformat * Delete * Close
Here Test case changes the state and test case exercises operations based on the attributed that they use.
Problem:
Apply multiple class testing and tests derived from the behavioral model for the Safe-Home design.
Answer:
4633-19-7P SA: 9420
SR: 6376
State transition diagram for safe home
This state transaction diagram represents the dynamic behavior of a class.
• This diagram for a class can be used to help derive a sequence of tests that will exercise the dynamic behavior of the class.
• State transition diagram for the safe home class 1s.
• Starting transactions move through reading the user’s input and invoking the monitor for the system status.
• Majority of all behaviors for the instances of the safe home class occurs while in the action on a sensor event.
• Sensor event and display cause the feedback class to make transactions to the display state.
Tests should achieve all state coverage
:
Open the user input page invoke the monitor & system status * action on the sensor event (initial) * display the use of feed back (final) * Close.
:
Open the user input page * Read the user’s input * invoke the monitoring * system status * action on a sensor event (initial) * sensor event invoke * display the user feed back * display (final).
This sequence is the minimum test sequence. Now, when class behavior results in collaboration with one or more class multiple state transaction diagrams are used to track the behavioral flow of the system.
Problem:
Derive four additional tests using random testing and partitioning methods as well as multiple class testing and tests derived from the behavioral model for the banking application presented in Sections 24.5 and 24.6.
Answer:
Random tests cases are generated to exercise a class and its functionality. Once such test case for a banking application is given below:
open.setup.deposit.deposit.summarize.deposit.summarize.withdraw.summarize.withdraw.close
This test case tests different operations which are randomly generated.
State operations and non-state operations can be exercised using partition testing. An example of state operations test case is given below which includes the deposit() operation as well as the withdraw() operation:
open.setup.deposit.withdraw.withdraw.deposit.withdraw.close
Another example of state which includes the balance() operation is given below to illustrate non-state operation:
open.setup.deposit.balance.withdraw.close
Tests should also be conducted to check the functioning between different classes. The following test case exercises several operations sequence for classes in a banking application:
verifyAcct.verifyPIN.withdrawReq.balanceInfoReq
The Bank class should collaborate with class ValidationInfo to be able to execute the operations verifyAcct() and verifyPIN(). The Bank class must be able to collabrote with the Account class also to execute the withdrawReq() and balanceInfoReq() operations. These collaborations can be exercised using the following new test case:
VerifyAcct[Bank:validAccValidationInfo].VerifyPIN[Bank:validPinValidationInfo].withdrawReq[Bank:wihdrawaccount].balanceInfoReq[Bank:balanceEnquiryInfo]
The dynamic behaviour of a class is tested by a test -case that covers every state of the class from working to non-working state. Once such sequence of operations is given below:
open.setupAccnt.deposit(initial).withdraw.accntInfo.deposit.accntInfo.withdraw.accntInfo.withdraw(final).close
These are few additional test cases which can be used for testing banking application.
Solution: CHAPTER24: TESTING OBJECT-ORIENTED APPLICATIONS
24.1 The class encapsulates data and the operations that manipulate the data. Since these are packaged as a unit, testing just the methods one-by-one would be sterile and would not uncover errors associated with messaging, responsibilities and collaborations.
24.2 Because each subclass of a given class is applied within the context of private attributes and operations (and the fact that these private attributes and operations) can add complexity), subclasses must be retested in their operational context. Test cases can be reused, but it is important to extend them to address the private attributes and operations of the subclass.
24.3 During latter stages of their development, OO analysis and design models provide
substantial information about the structure and behavior of the system. For this reason, these models should be subjected to rigorous review prior to the generation of code. All object-oriented models should be tested (in this context, the term testing incorporates technical reviews) for correctness, completeness, and consistency within the context of the model’s syntax, semantics and pragmatics. These reviews have the potential to eliminate much unnecessary work and rework (again the earlier errors are discovered the cheaper they are to fix).
24.4 Answers will vary
24.5 Thread-based testing is used to integrate a set of classes that are required to respond to a single program input or a single event. Use based testing is an integration testing strategy that begins by integrating the classes that do not collaborate with “server” classes (these are called independent classes). Then, classes that collaborate with the independent classes are tested in a layered fashion.
24.6 Answers will vary
24.7Answers will vary
24.8 Answers will vary