Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 14: Testing Testing used to verify object behavior through designed test suites Can test Classes – “unit” testing Object interactions – “integration”

Similar presentations


Presentation on theme: "Lecture 14: Testing Testing used to verify object behavior through designed test suites Can test Classes – “unit” testing Object interactions – “integration”"— Presentation transcript:

1 Lecture 14: Testing Testing used to verify object behavior through designed test suites Can test Classes – “unit” testing Object interactions – “integration” testing Systems – “system” testing Testing can only show that errors do exist; it cannot guarantee that they do not exist – E. Djikstra

2 People who test Programmers do unit tests to ensure classes do what they are supposed to do Dedicated testers may do unit testing, integration testing and system testing In real-world, testing often done by someone other than programmer. In class, you are it! The best time to perform testing is at the same time you are coding while the logic of the code is fresh in your mind.

3 Categories of Errors Compilation errors – occur during compile
Run-time errors – cause execution to abort Logic errors – cause computed results to be incorrect

4 Compilation Errors Syntax errors – grammatical mistakes
missing semicolon on statement Unmatched parenthesis No comma between method parameters If problem is bad enough, it may cause cascade of errors, because compiler becomes confused. One error could result in the compiler reporting many errors. Semantic errors – code has no meaning Incrementing an int variable before it is initialized Using a variable before it is declared Assigning a String to an int variable Semantic errors may indicate a lack of understanding of the meaning of the programming language constructs being used. X=5 (3+5 myStr = s.substring(0 1); int i; i++;

5 Run-time Errors Ex: Using method on null reference
Name n = Name.read(br); //null if not read if (n.getFirst().equals(”Sammy”)) count++; If Name.read returns a null, then cannot do getFirst method on null reference Exception is thrown and program aborts Fix errors by process of debugging Another example: int x = Integer.parseInt( myBufRdr.readLine() ); int y = 10/x;

6 Finding Run-Time Errors
Java is helpful when an exception is thrown In console window you will see Exception in thread “main” java.lang.NullPointerException at Account.write(Account.java:45) at Program2.solution(Program2.java:75) at Program2.main(Program2.java:8) Tells you method, class, file, line number where exception was thrown This is probably not where the fault is located cascade

7 Logic Errors Programmer made a mistake
Either didn’t understand what was supposed to happen, or made a typographical error Examples: class DoubleTrouble { public int doubleIt() { value += 2; //mistake, want to multiply return value; } private int value; } //comparing two reference variables Boolean sameName = (emp1 == emp2); //when in actuality the programmer wished to compare the //two strings in the string objects, (not the references) Boolean sameName = emp1.equals(emp2);

8 System Testing Can test a whole program by giving it different values for input Goal is to choose test input that are both valid and invalid and see if system behaves as you would expect. Choose input values by looking at problem statement – called “black box” testing

9 Testing Classes Test Driver A method with purpose of testing a class.
Often make a static method of class being tested Partially automates testing of class Code as a method in the class so the class contains its own test code.

10 Example Test Driver class Employee { //…
public static void testDriver() { Employee e = new Employee(”Gerald Weiss”, 25); int hours = 36; System.out.println(”Name: ” + e.getName()); System.out.println(”Rate: ” + e.rate()); System.out.println(”Hours: ” + hours); System.out.println(”Pay: ” + e.calcPay(hours)); } Test code is a method in the class so that class carries around its own test code.

11 Automating Testing public static void testDriver() {
Employee e = new Employee(”Gerald Weiss”, 25); int hours = 36; int correctAnswer = 900; //computed by hand System.out.println(”Name: ” + e.getName()); System.out.println(”Rate: ” + e.rate()); System.out.println(”Hours: ” + hours); int computedPay = e.calcPay(hours); System.out.println(”Pay: (computed)” + computedPay); }

12 Automating Testing // continued from previous page
if (computedPay != correctAnswer) { System.out.print(”****Error: computed pay should be”); System.out.println(correctAnswer); } else System.out.println(”Test successful”);

13 Getting More Information
Helper class makes writing test drivers easier class TestHelper { public static void verify(boolean testCond, String message) { if (!testCond) System.out.print(”****Error – test failure: ”); System.out.println(message); Thread.dumpStack(); } Thread.dumpStack() aborts program and prints exception trace information to indicate where error occurs (see slide 7)

14 Designing Tests Test methods and constructors
Don’t just test “most important” methods Test in logical order test from most primitive to more complex very effective to test all possible pairs of sequences Execute each statement at least once (“line coverage”, “statement coverage”) Can also test each condition (“condition coverage”, “branch coverage”)

15 Choosing Test Values Test special cases
special values: zero, null, empty string boundary conditions: 40 is a boundary condition for condition hours <= 40 (Also try “near” boundary values, like 39 and 41.) Do not worry about efficiency of test, so don’t worry about how many you choose (Testing is not concerned with speed or efficiency)

16 Debugging Process of discovering faults
Already know program has errors First look for method responsible for computation Use output statements to view values of variables. Place statements in particular places to see what values are printed. Compare hand computation with output statements.


Download ppt "Lecture 14: Testing Testing used to verify object behavior through designed test suites Can test Classes – “unit” testing Object interactions – “integration”"

Similar presentations


Ads by Google