Introduction to Object-oriented Programming

Document Sample
Introduction to Object-oriented Programming Powered By Docstoc
					             Unit Testing
       Part 3: Regression Testing

CSIS 3701: Advanced Object Oriented Programming
                   Regression Testing
• Problem: Fixing bugs can introduce other errors
   – 10% to 20% of bug fixes create new bugs
   – Often happens as part of maintenance
        • Developer who changes one module not familiar with rest of
   – Big problem: What if new bugs in code already tested?

                          Module A
                         tested first
Fixing B creates                               If A not retested,
bugs in A                                      will be shipped
                                               with bugs!
                         Module B
                       tested second
                Regression Testing
• Regression testing:
  Retesting with all test cases after any change to

                               Retest with     Comprehensive
     Fix bugs                                  list of test cases
                              all test cases

                 Find bugs

• Problem: May be thousands of tests!
   – Too many for interactive debugging
             Automated Testing
• Goal: Automate comprehensive testing
  – Run all test cases
  – Notify developer of incorrect results

• Approaches:
  – Creating testing “script” in driver
  – Read test cases and desired results from file
  – Use testing tools (JUnit)
               Scripted Testing

• For each constructor/void method:
  – Execute method
  – Display value returned/exception thrown
  – Get current state to desired state (using toString() )
     • Display current state
     • Compare to desired state
  – Error message if incorrect
     • Display actual state
       Scripted Testing Example
public static void main(String[] args) {
  // Test default constructor
  Clock c1 = new Clock();
  if (!(c1.toString()).equals("00:00"))
     System.out.println("Default constructor failure:"

  // Test parameterized constructor
  Clock c2 = new Clock(12,34);
  if (!(c2.toString()).equals("12:34"))
     System.out.println("Parameter constructor failure:"
       Scripted Testing Example
// Test ability to set new time
if (!(c2.toString()).equals("09:01"))
  System.out.println("setTime failure: "+c2.toString());

// Test ability of nextMinute to increment minute, hour
Clock c3 = new Clock(11, 59);
if (!(c3.toString()).equals("12:00"))
  System.out.println("next failure: "+c3.toString());
Clock c4 = new Clock(23, 59);
if (!(c4.toString()).equals("00:00"))
  System.out.println("next failure: "+c4.toString());
   Scripted Testing for Inspectors
• For each method that returns a value:
   – Execute method
   – Compare actual return value to desired value
   – Compare actual state to desired state
      • Make sure state not changed for inspectors

// Test getter inspectors, make sure state not changed
if (c2.getHour() != 9)
  System.out.println("getHour failure: "+c2.getHour());
if (!(c2.toString()).equals("09:01"))
  System.out.println("getHour changed state: "
    Scripted testing for Validation
• For each validation inspector:
  – Run test case to get value (true or false)
  – Compare to desired value
• For each validation case in modifier:
  – Run test case
  – Make sure state not changed
      Scripted Testing Example
// Test validation inspectors   for border cases
if (c2.isHour(-1))
   System.out.println("isHour   failure: -1");
if (!c2.isHour(0))
   System.out.println("isHour   failure: 0");
if (c2.isHour(24))
   System.out.println("isHour   failure: 24");
if (!c2.isHour(23))
   System.out.println("isHour   failure: 23");
       Scripted Testing Example
// Test validation in modifiers for no state change
if (!(c2.toString()).equals("09:01"))
   System.out.println("setTime validation failure: "
if (!(c2.toString()).equals("09:01"))
   System.out.println("setTime validation failure: "

Shared By: