Docstoc

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
          system
   – 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
  code

                               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:"
                          +c1.toString());

  // Test parameterized constructor
  Clock c2 = new Clock(12,34);
  if (!(c2.toString()).equals("12:34"))
     System.out.println("Parameter constructor failure:"
                        +c2.toString());
       Scripted Testing Example
// Test ability to set new time
c2.setTime(9,1);
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);
c3.nextMinute();
if (!(c3.toString()).equals("12:00"))
  System.out.println("next failure: "+c3.toString());
Clock c4 = new Clock(23, 59);
c4.nextMinute();
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: "
                     +c2.toString());
    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
c2.setTime(-1,12);
if (!(c2.toString()).equals("09:01"))
   System.out.println("setTime validation failure: "
                      +c2.toString());
c2.setTime(24,12);
if (!(c2.toString()).equals("09:01"))
   System.out.println("setTime validation failure: "
                      +c2.toString());

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:2
posted:8/4/2012
language:English
pages:11