Object-oriented software test case | Testing of Date.increment method

 Object-oriented technology has produced a better system structure and a more standardized coding style. It has greatly optimized the security of data use and improved the reusability of program code. This has led some people to believe that programs developed with object-oriented technology do not require carry out testing. It should be noted that although the basic idea of ​​object-oriented technology guarantees that software should have higher quality, the actual situation is not the case; because no matter what programming technology is used, programmer errors are inevitable, and due to the Software code developed with object technology has a high reuse rate and a higher probability of error recurrence, requiring strict testing to avoid a large number of errors from recurring. This article describes the testing of the Date.increment method.

01. Class description

CRC (Class-Responsibility-Collabortor, Class-Responsibility-Collaborator) is currently a popular object-oriented analysis and modeling method. In CRC modeling, users, designers, and developers will all participate to complete the design of the entire object-oriented project.

The CRC card is a standard collection of index cards, including three parts, namely the class name, the responsibilities of the class, and the collaboration relationship of the class. Each card will represent a class. The class name is the name of the class described by the card, which is written at the top of the entire CRC card; the responsibilities of the class include the class's understanding of its own information and how this information will be used. This part will be written on the left side of the CRC card; The collaboration relationship of a class refers to other classes related to the current class, through which the desired information can be obtained or related operations can be performed. This part is written on the right side of the CRC card.

When testing this method, you should first use the "Class-Responsibility-Collaborator" (CRC) card to describe the Date class, and then analyze the program diagram based on the pseudocode of the Date class as shown in Figure 1.

■ Figure 1 Program diagram of testIt and Date classes

Class CalendarUnit provides a method to set the value from the class it inherits, and provides a Boolean method to indicate whether the attributes in its inherited class can be increased by 1. The pseudo code is as follows.

class CalendarUnit(
//abstract class
int currentpos;
CalendarUnit(pCurrentpos)
currentpos = pCurrentpos;//结束 CalendarUnit
setCurrentpos(pCurrentpos)
currentpos = pCurrentpos;//结束 setCurrentpos
abstract protected Boolean increment();
}

 To test the Date.increment method, you need to develop the class testIt as a test driver, that is, create a test date object, then request the object to increment itself by 1, and finally print the new value. The pseudo code is as follows.

class testItl
main()(
testdate = instaniate Date(testMonth,testDay,testYear);
Testdate.increment();
Testdate.printDate();
//结束 testIt

The information in the CRC card of Date class is given below, as shown in Table 2.

■ Table 2 CRC card of Date class

 The pseudocode of the Date class is as follows.

class Date{
private Day d;
private Month m;
private Year y;
public Date( int pMonth,int pDay,int pYear){
y = instaniate Year(pYear);
m = instaniate Month(pMonth,y);
d = instaniate Day(pDay,m);
)//结束 Date构造函数
increment()
if(!d.increment()){
if(!m.increment())
y.increment();
m.setMonth(1,y);
else
d.setDay(1,m);
//结束 increment
printDate()
System.out.println(m.getMonth() +"/" + d.getDay() + "/"+ y.getYear());
}//结束 printDate
}//结束 Date

 The program diagram of testIt class and Date class is shown in Figure 2.

 

■ Figure 2 Program diagram of testIt and Date classes

02. Design test cases

As introduced in the black box testing section, equivalence class testing is a wise choice for logic-intensive units. The three equivalent classes of Date.increment operation for processing dates are as follows.

D1={date:1≤date<last date of month}.

D2={Date: Date is not the last date of December}.

D3={Date: The date is December 31st}.

In fact, there are three paths corresponding to the Date.increment program graph.

path1:9-10-18.

path2:9-10-11-12-13-14-17-18。

path3:9-10-11-15-16-17-18。

They form the base path of Date.increment. It is not difficult to calculate that the cyclomatic complexity of the Date.increment program graph is 3. Additionally, these equivalence classes appear to be loosely defined, especially D1, which references the last date without a month specification, i.e. without specifying which month it is. In this way, the problem is further transformed into a test of the Month.increment method.

Guess you like

Origin blog.csdn.net/qq_41640218/article/details/132706747