170 likes | 192 Views
Testing & Debugging. CSC 171 FALL 2004 LECTURE 13. Reading. Read Chapter 8 of Horstmann We will be doing this in labs. BUGS?. TERMINOLOGY. Using the term “fault” instead of “bug” is one step toward professionalism. Therac-25. PATRIOT MISSILE.
E N D
Testing & Debugging CSC 171 FALL 2004 LECTURE 13
Reading • Read Chapter 8 of Horstmann • We will be doing this in labs
TERMINOLOGY • Using the term “fault” instead of “bug” is one step toward professionalism
PATRIOT MISSILE • On February 25, 1991, a Patriot missile defense system operating at Dhahran, Saudi Arabia, during Operation Desert Storm failed to track and intercept an incoming Scud. This Scud subsequently hit an Army barracks, killing 28 Americans. • The Patriot battery at Dhahran failed to track and intercept the Scud missile because of a software problem in the system's weapons control computer.
UNIT TESTING • Test classes in isolation, outside the program in which they are used • Test one class at a time • Supply test inputs through test harness • Test harness = program that feeds test inputs to a class
public static double power(int a, int n) { int r = 1; int b = a; int i = n ; while (i>0) { if (i%2 == 0) { b = b * b; i = i / 2;} else { r = r * b; i--; } } return r; }
Sources of Test Data • Provided by humans • Computer-generated sequence • Random sequence
Test Cases • Positive test case: expect positive outcomeE.g square root of 100 • Negative test case: expect negative outcomeE.g square root of 100 • Boundary test case: at boundary of domainFrequent cause for errorsE.g square root of 0
Test Case Evaluation • Manual • Check property of resultE.g. square root squared = original value • Oracle = slower way of computing answerE.g. square root of x = x1/2
Regression Testing • Save test cases • Automate testingjava Program < test1.in > test1.outjava Program < test2.in > test2.outjava Program < test3.in > test3.out • Repeat test whenever program changes • Test suite = collection of test cases • Cycling = bug that is fixed but reappears in later versions • Regression testing = testing against past failures
Test Coverage • Black-box testing: test functionality without understanding internal structure • White-box testing: take internal structure into account when designing tests • Test coverage: the code that is actually executed during test cases • Easy to overlook error branches during testing • Make sure to execute each branch in at least one test case
Program Trace • Output statements in your program for diagnostic purposesif (status == SINGLE) { System.out.println("status is SINGLE"); ... }
Stack Trace • Stack trace tells you the contents of the call stackThrowable t = new Throwable(); t.printStackTrace(System.out); • Looks like exception report:java.lang.Throwable at TaxReturn.getTax(TaxReturn.java:26) at TaxReturnTest.main(TaxReturnTest.java:30) • Drawback of trace messages: Need to remove them when testing is complete, stick them back in when another error is found
Logging • Get global logger object:Logger logger = Logger.getLogger("global"); • Log a messagelogger.info("status is SINGLE"); • By default, logged messages are printed. Turn them off withlogger.setLevel(Level.OFF);
Assertions • Assertion = assumption that you believe to be trueassert y >= 0;root = Math.sqrt(y); • If assertion fails, program is terminated • Can be used to assert pre/postconditions • Must compile/run with special flagsjavac -source 1.4 MyProg.javajava -enableassertions MyProg