49. Case Study - Money Class class Money { private int fAmount; private String fCurrency; public Money(int amount, String currency) { fAmount= amount; fCurrency= currency; } public int amount() { return fAmount; } public String currency() { return fCurrency; } public Money add(Money m) { return new Money(amount()+m.amount(), currency()); } }
75. SQLUnit Output Buildfile: build.xml init: test-ant-task: [sqlunit] Getting connection... [sqlunit] Setting up test... [sqlunit] Running test[1]: Adding department HR [sqlunit] Running test[2]: Adding department InfoTech using non-Callable form [sqlunit] Running test[3]: Adding Employee John Doe to InfoTech [sqlunit] Running test[4]: Adding John Doe again [sqlunit] Running test[5]: Adding Jane Doe to HR [sqlunit] Running test[6]: Adding Dick Tracy to InfoTech [sqlunit] Running test[7]: Updating Hourly Rate for John [sqlunit] Running test[8]: Looking up John Doe by name [sqlunit] Running test[9]: Looking up all employees in InfoTech [sqlunit] Running test[10]: Adding timecard for John [sqlunit] Running test[11]: Adding another timecard for John [sqlunit] Running test[12]: Adding timecard for Dick
81. Nunit Example Stack.cs using System; using System.Collections; public class Stack { private ArrayList elements = new ArrayList(); public bool IsEmpty { get { return (elements.Count == 0); } } public void Push(object element) { elements.Insert(0, element); } public object Pop() { object top = Top(); elements.RemoveAt(0); return top; } public object Top() { if(IsEmpty) throw new InvalidOperationException( “Stack is Empty”); return elements[0]; } }
118. Example Test Checklists Generic 2 General 1 Does input data vary, including maximum, minimum, and nominal values? (All alike data, especially all zeroes, is usually a poor choice.) 2 Is erroneous input data used? (All error conditions should be checked.) 3 Do the tests demonstrate that the code completely satisfies each requirement? 4 Does the actual output match the expected output? Data-Declaration Error Testing 5 Have all the data structures been explicitly tested? 6 Has all data been properly initialized? (e.g., initialized with the correct data type, default values) 7 Have all global data structures been tested? Data Referencing Errors 8 Are all variables set to the proper value or initialized correctly?
119.
120.
121.
122.
123.
124.
125.
126.
127. Data Driven Test Harness The test harness usually conforms to the following structure: while data file is not empty read inputs and outputs from data file call function with inputs if results of function match expected outputs register TC passed else register TC failed end if end while output summary
128. Example - Harness /* Calculate the hypotenuse of a right angle */ /* triangle . nOpposite and nAdjacent must values */ /* be non negative . Return the hypotenuse or –1 */ /* for invalid input */ double calcHypotenuse ( double nOpposite, double nAdjacent ) { if ( nOpposite < 0 or nAdjacent < 0 ) { return –1; } return ( sqrt(pow(nOpposite,2)+ pow(nAdjacent,2)) ); } /* Test Harness – this does all the work */ main(…) { int count = 0; // count the no of tests executed int fail = 0; double result; … /* open the input script */ … while ( line = readLine(inputFile) != EOF ) { if ( is_comment(line) ) { continue; } count++; /* split line into testid,opposite,adjacent,expected */ … result = calcHypotenuse ( opposite, adjacent ); if ( result != expected ) { fail++; output(“Test failed: %s( %d, %d ) got %d expected %d”, testid, opposite, adjacent, result, expected ); } } /* output the final stats */ output(“Tests run: %d”, count); output(“Tests OK: %d”, count-fail); output(“Tests failed: %d”, fail); }
129.
130.
131.
132.
133.
134.
135.
136. Coordinating Harnesses and Stubs TC01:3,4,1998; 30 ;T # normal date TC02:29,2,1999; 28 ;F # not a leap year TC03:29,2,2000; 29 ;T # leap year TC04:29,2,2100; 28 ;F # not a leap year The data file may have fields indicating data that should be returned by stubbed functions during the test. The stubbed functions return global variables that are set by the test harness after reading the appropriate value from the test file. For example, the daysInMonth function used by checkDate is stubbed. It returns a pre-determined value from the test harness data file (shown in bold ). bool checkDate(int d, int m, int y) { if ( y <= 0 ) return false; if ( m < 1 || m > 12 ) return false; if ( d < 1 || d > daysInMonth(m, isLeapYear(y)) ) return false; return true; }
137. Coordinating Harnesses and Stubs (cont) TC01:3,4,1998; 30 ;T # normal date TC02:29,2,1999; 28 ;F # not a leap year TC03:29,2,2000; 29 ;T # leap year TC04:29,2,2100; 28 ;F # not a leap year int daysInMonth (int month, bool leapYear) { return globalDaysInMonth ; } int isLeapYear(int y) { return true; } /* simple stub */ while (data remaining in file) { read data line into (TCID, (d, m, y), globalDaysInMonth , Expected) if (checkDate(d,m,y) == Expected) { register (TCID,pass) else register (TCID,fail) } bool checkDate(int d, int m, int y) { if ( y <= 0 ) return false; if ( m < 1 || m > 12 ) return false; if ( d < 1 || d > daysInMonth(m, isLeapYear(y)) ) return false; return true; }