Comp1004: Building Better Classes I Testing and Debugging Partly based on BlueJ Book – Chapter 6.

Slides:



Advertisements
Similar presentations
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Advertisements

Programming Types of Testing.
FIT FIT1002 Computer Programming Unit 19 Testing and Debugging.
Debugging Techniques1. 2 Introduction Bugs How to debug Using of debugger provided by the IDE Exception Handling Techniques.
1 CS2200 Software Development Lecture: Testing and Design A. O’Riordan, 2008 K. Brown,
Well-behaved objects 4.0 Testing. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts and Debugging.
Well-behaved objects Debugging. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Prevention vs Detection.
Well-behaved objects Improving your coding skills 1.0.
Testing and Debugging. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling (Reminder) Zuul Assignment Two.
Make Sure You Know All This!. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling 2 Objects and Classes.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts, Testing and Debugging.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
PRE-PROGRAMMING PHASE
1. 2 Chapter 1 Introduction to Computers, Programs, and Java.
CS1101: Programming Methodology Aaron Tan.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
Testing. Definition From the dictionary- the means by which the presence, quality, or genuineness of anything is determined; a means of trial. For software.
1 Debugging and Testing Overview Defensive Programming The goal is to prevent failures Debugging The goal is to find cause of failures and fix it Testing.
Invitation to Computer Science, Java Version, Second Edition.
Designing and Debugging Batch and Interactive COBOL Programs Chapter 5.
Debugging. Compile problems Read the whole complaint! Runtime problems –Exceptions –Incorrect behavior.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
1 Intro to Java Week 12 (Slides courtesy of Charatan & Kans, chapter 8)
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
COMP 121 Week 1: Testing and Debugging. Testing Program testing can be used to show the presence of bugs, but never to show their absence! ~ Edsger Dijkstra.
Unit Testing 101 Black Box v. White Box. Definition of V&V Verification - is the product correct Validation - is it the correct product.
Testing and Debugging Session 9 LBSC 790 / INFM 718B Building the Human-Computer Interface.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
1 Program Planning and Design Important stages before actual program is written.
KUFA UNIVERSITY Department of Computer Science 06/12/2015.
OOPDA Intro 5.0. Topics Website and Syllabus Rowan VPN and H:drive BlueJ application and projects Programming Style (Appendix J) Javadoc (Appendix I)
Testing. Have you contacted your project members lately?
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
Objects First With Java A Practical Introduction Using BlueJ Well-behaved objects 2.1.
1 COS 260 DAY 15 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz Today –Chapter 6 Assignment 4 posted –Due Nov 9 Capstone progress reports are due –Brief.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
Bugs CS100 how to prevent them, how to find them and how to terminate them.
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
Comp1004: Environments The Java Library. Coming up Recap – Encapsulation – Constructors – Loops – Arrays – ArrayList – Iterators The Java Library – Implementation.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability © 2017 Pearson Education, Inc. Hoboken,
Review. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects and Classes Objects and Classes –State.
Wrapper Classes Debugging Interlude 1
Objektorienterad programmering d2, förel. 6
Objects First with Java
Comp1004: Loops and Arrays II
Testing and Debugging PPT By :Dr. R. Mall.
Objektorienterad programmering d2, förel. 6
CS1101X Programming Methodology
Testing and Debugging.
Loop Structures.
Debugging CMSC 202.
2_Testing Testing techniques.
COS 260 DAY 17 Tony Gauvin.
Unit# 8: Introduction to Computer Programming
Objects First with Java
Designing and Debugging Batch and Interactive COBOL Programs
COS 260 DAY 16 Tony Gauvin.
COS 260 DAY 15 Tony Gauvin.
Exercise 1 Declare a constant of type int called SIZE and initialize it to value 10 Declare two int arrays of size “SIZE” Assume that those int arrays.
COS 260 DAY 16 Tony Gauvin.
PROGRAMMING FUNDAMENTALS Lecture # 03. Programming Language A Programming language used to write computer programs. Its mean of communication between.
An Introduction to Debugging
Lecture 14: Testing Testing used to verify object behavior through designed test suites Can test Classes – “unit” testing Object interactions – “integration”
Chap 7. Advanced Control Statements in Java
Presentation transcript:

Comp1004: Building Better Classes I Testing and Debugging Partly based on BlueJ Book – Chapter 6

Software Errors Are A Problem Mars Climate Orbiter – $125 million dollar probe – Communications relay for future NASA Mars missions Launched successfully on Dec 11, 1998 and travelled 669 million km in 286 days And finally fired its engines, causing it to plunge into the atmosphere and never be seen again :-(

Software Errors Are A Problem The Problem? NASA built the engines to take navigational instructions in metric units (m/sec) Lockhead Martin provided them in imperial units (ft/sec) So the probe dropped too far, and its engines overheated in the atmosphere

Coming up we aim to save $125 million dollars Debugging – Syntax and Logical Errors Testing Strategies – Equivalence Classes and Boundary Values – Test Automation Debuggers

We have to deal with errors Early errors are usually syntax errors. – The compiler will spot these. Later errors are usually logic errors. – The compiler cannot help with these. – Also known as bugs. Some logical errors have no immediately obvious manifestation. – Commercial software is rarely error free.

Syntax Errors? ArrayList kennel = new ArrayList ; kennel.add(new Dog(“Rover”)); kennel.add(new Dog(“Fido”)); kennel.add(new Dog(“Patch”)); kennel.add(new Dog(“Eric”)); kennel.add(new Dog(“Binky”)); for(int i = 0; i <= kennel.size(); i == i + 1) { kennel.get(i).bark() }

Syntax Errors? ArrayList kennel = new ArrayList (); kennel.add(new Dog(“Rover”)); kennel.add(new Dog(“Fido”)); kennel.add(new Dog(“Patch”)); kennel.add(new Dog(“Eric”)); kennel.add(new Dog(“Binky”)); for(int i = 0; i <= kennel.size(); i == i + 1) { kennel.get(i).bark(); } Forgot to call the Constructor Forgot to terminate the statement with a semi- colon

Logical Errors? ArrayList kennel = new ArrayList (); kennel.add(new Dog(“Rover”)); kennel.add(new Dog(“Fido”)); kennel.add(new Dog(“Patch”)); kennel.add(new Dog(“Eric”)); kennel.add(new Dog(“Binky”)); for(int i = 0; i <= kennel.size(); i == i + 1) { kennel.get(i).bark(); } Forgot to call the Constructor Forgot to terminate the statement with a semi- colon

Logical Errors? ArrayList kennel = new ArrayList (); kennel.add(new Dog(“Rover”)); kennel.add(new Dog(“Fido”)); kennel.add(new Dog(“Patch”)); kennel.add(new Dog(“Eric”)); kennel.add(new Dog(“Binky”)); for(int i = 0; i < kennel.size(); i = i + 1) { kennel.get(i).bark(); } Forgot to call the Constructor Forgot to terminate the statement with a semi- colon Need to use < or the loop will overrun the arraylist Need to use assignment operator = rather than the equality operator ==

Prevention vs Detection (Developer vs Maintainer) We can lessen the likelihood of errors. – Use software engineering techniques, like encapsulation. We can improve the chances of detection. – Use software engineering practices, like modularization and documentation. We can develop detection skills.

Testing and debugging These are crucial skills. Testing searches for the presence of errors. Debugging searches for the source of errors. – The manifestation of an error may well occur some ‘distance’ from its source.

Method 1: Unit testing Each unit of an application may be tested. – Method, class, module (package in Java). Can (should) be done during development. – Finding and fixing early lowers development costs (e.g. programmer time). – A test suite is built up.

Testing fundamentals Understand what the unit should do – its contract. – You will be looking for violations. – Use positive tests and negative tests. Test boundaries. – Zero, One, Full. Search an empty collection. Add to a full collection.

Equivalence Classes and Boundary Values “A theme park is testing a new ticketing system. Adults aged 18 or over pay £20, children age 5 years or younger get in free, and other children pay £12. In all cases if the person is a member of the theme park fan club the cost is reduced by 50%.” What are the equivalence classes? (the sets of values that have the same result) Member Age 18+ Member Age 18+ £10

Equivalence Classes and Boundary Values “A theme park is testing a new ticketing system. Adults aged 18 or over pay £20, children age 5 years or younger get in free, and other children pay £12. In all cases if the person is a member of the theme park fan club the cost is reduced by 50%.” What are the equivalence classes? (the sets of values that have the same result) Member Age 18+ Member Age 18+ Non-Member Age 18+ Non-Member Age 18+ Member Age Member Age Non-Member Age Non-Member Age Member or Non-Member Age Member or Non-Member Age £10 £20 £6 £12 FREE

Equivalence Classes and Boundary Values “A theme park is testing a new ticketing system. Adults aged 18 or over pay £20, children age 5 years or younger get in free, and other children pay £12. In all cases if the person is a member of the theme park fan club the cost is reduced by 50%.” Member Age 18+ Member Age 18+ Non-Member Age 18+ Non-Member Age 18+ Member Age Member Age Non-Member Age Non-Member Age Member or Non-Member Age Member or Non-Member Age £10 £20 £6 £12 FREE What are the boundary values? (the points at which the classes change)

Age? 5/6 Equivalence Classes and Boundary Values “A theme park is testing a new ticketing system. Adults aged 18 or over pay £20, children age 5 years or younger get in free, and other children pay £12. In all cases if the person is a member of the theme park fan club the cost is reduced by 50%.” What are the boundary values? (the points at which the classes change) Member Age 18+ Member Age 18+ Non-Member Age 18+ Non-Member Age 18+ Member Age Member Age Non-Member Age Non-Member Age Member or Non-Member Age Member or Non-Member Age £10 £20 £6 £12 FREE Member? true/false Age? 17/18

Test automation Good testing is a creative process, but thorough testing is time consuming and repetitive. Regression testing involves re-running tests to see if new changes have caused old code to break Use of a test rig or test harness can relieve some of the burden. – Classes are written to perform the testing.

Age? 5/6 Test Harness What might our test harness look like? Member Age 18+ Member Age 18+ Non-Member Age 18+ Non-Member Age 18+ Member Age Member Age Non-Member Age Non-Member Age Member or Non-Member Age Member or Non-Member Age £10 £20 £6 £12 FREE Member? true/false Age? 17/18

Age? 5/6 Test Harness What might our test harness look like? Member Age 18+ Member Age 18+ Non-Member Age 18+ Non-Member Age 18+ Member Age Member Age Non-Member Age Non-Member Age Member or Non-Member Age Member or Non-Member Age £10 £20 £6 £12 FREE Member? true/false Age? 17/18 public testCosts() { //Test Harness System.out.println(“Starting Cost Tests”); int cost; cost = calcCost(18, true); if(cost == 10) System.out.println(“Test 1 Pass”); else System.out.println(“Test 1 Fail”); cost = calcCost(18, false); if(cost == 20) System.out.println(“Test 2 Pass”); else System.out.println(“Test 2 Fail”); cost = calcCost(17, true); if(cost == 6) System.out.println(“Test 1 Pass”); else System.out.println(“Test 1 Fail”); cost = calcCost(17, false); if(cost == 12) System.out.println(“Test 1 Pass”); else System.out.println(“Test 1 Fail”); //etc – until all boundaries are tested System.out.println(“Ending Cost Tests”); }

Method 2: Manual walkthroughs Relatively underused. – A low-tech approach. – More powerful than appreciated. Get away from the computer! ‘Run’ a program by hand. High-level (Step) or low-level (Step into) views.

Tabulating object state An object’s behavior is usually determined by its state. Incorrect behavior is often the result of incorrect state. Tabulate the values of all fields. Document state changes after each method call.

Method 3: Verbal walkthroughs Explain to someone else what the code is doing. – They might spot the error. – The process of explaining might help you to spot it for yourself. Group-based processes exist for conducting formal walkthroughs or inspections.

Method 4: Print statements The most popular technique. No special tools required. All programming languages support them. Only effective if the right methods are documented. Output may be voluminous! Turning off and on requires forethought. public int sumAllCosts() { int costs[] = getAllCosts(); int totalcost = 0; for(int n : costs){ totalcost += n; } return totalcost; }

Method 4: Print statements The most popular technique. No special tools required. All programming languages support them. Only effective if the right methods are documented. Output may be voluminous! Turning off and on requires forethought. public int sumAllCosts() { int costs[] = getAllCosts(); if(costs == null) System.out.println(“Costs array is null!”); else { System.out.println(“Costs array returned”); System.out.println(“Size:” + costs.size()); } int totalcost = 0; for(int n : costs){ System.out.println(totalcost); totalcost += n; System.out.println(totalcost); } System.out.println(“Finished”); System.out.println(totalcost); return totalcost; }

Choosing a test strategy Be aware of the available strategies. Choose strategies appropriate to the point of development. Automate whenever possible. – Reduces tedium. – Reduces human error. – Makes (re)testing more likely.

Debuggers Debuggers are both language- and environment-specific. – BlueJ and Eclipse have integrated debugger. Support breakpoints. Step and Step-into controlled execution. Call sequence (stack). Object state.

Debuggers Debuggers are both language- and environment-specific. – BlueJ and Eclipse have integrated debugger. Support breakpoints. Step and Step-into controlled execution. Call sequence (stack). Object state. public void markWork(Work w) { int mark = allocateMark(w); if(w.late()) mark = mark * w.getPenalty(); w.setMark(mark); } public void markStudent(Student s) { Work work[] = s.getWork(); if(work != null) { for(Work w : work){ markWork(w); } public void markAllStudents() { Students students[] = getStudents(); if(students != null) { for(Student s : students){ markStudent(s); } Call stack

Debuggers Debuggers are both language- and environment-specific. – BlueJ and Eclipse have integrated debugger. Support breakpoints. Step and Step-into controlled execution. Call sequence (stack). Object state. public void markWork(Work w) { int mark = allocateMark(w); if(w.late()) mark = mark * w.getPenalty(); w.setMark(mark); } public void markStudent(Student s) { Work work[] = s.getWork(); if(work != null) { for(Work w : work){ markWork(w); } public void markAllStudents() { Students students[] = getStudents(); if(students != null) { for(Student s : students){ markStudent(s); } markAllStudents Call stack

Debuggers Debuggers are both language- and environment-specific. – BlueJ and Eclipse have integrated debugger. Support breakpoints. Step and Step-into controlled execution. Call sequence (stack). Object state. public void markWork(Work w) { int mark = allocateMark(w); if(w.late()) mark = mark * w.getPenalty(); w.setMark(mark); } public void markStudent(Student s) { Work work[] = s.getWork(); if(work != null) { for(Work w : work){ markWork(w); } public void markAllStudents() { Students students[] = getStudents(); if(students != null) { for(Student s : students){ markStudent(s); } markAllStudents markStudent Call Call stack

Debuggers Debuggers are both language- and environment-specific. – BlueJ and Eclipse have integrated debugger. Support breakpoints. Step and Step-into controlled execution. Call sequence (stack). Object state. public void markWork(Work w) { int mark = allocateMark(w); if(w.late()) mark = mark * w.getPenalty(); w.setMark(mark); } public void markStudent(Student s) { Work work[] = s.getWork(); if(work != null) { for(Work w : work){ markWork(w); } public void markAllStudents() { Students students[] = getStudents(); if(students != null) { for(Student s : students){ markStudent(s); } markAllStudents markStudent markWork Call Call stack

Debuggers Debuggers are both language- and environment-specific. – BlueJ and Eclipse have integrated debugger. Support breakpoints. Step and Step-into controlled execution. Call sequence (stack). Object state. public void markWork(Work w) { int mark = allocateMark(w); if(w.late()) mark = mark * w.getPenalty(); w.setMark(mark); } public void markStudent(Student s) { Work work[] = s.getWork(); if(work != null) { for(Work w : work){ markWork(w); } public void markAllStudents() { Students students[] = getStudents(); if(students != null) { for(Student s : students){ markStudent(s); } markAllStudents markStudent markWork Call Return Call stack

Debuggers Debuggers are both language- and environment-specific. – BlueJ and Eclipse have integrated debugger. Support breakpoints. Step and Step-into controlled execution. Call sequence (stack). Object state. public void markWork(Work w) { int mark = allocateMark(w); if(w.late()) mark = mark * w.getPenalty(); w.setMark(mark); } public void markStudent(Student s) { Work work[] = s.getWork(); if(work != null) { for(Work w : work){ markWork(w); } public void markAllStudents() { Students students[] = getStudents(); if(students != null) { for(Student s : students){ markStudent(s); } markAllStudents markStudent Call Call stack

Debuggers Debuggers are both language- and environment-specific. – BlueJ and Eclipse have integrated debugger. Support breakpoints. Step and Step-into controlled execution. Call sequence (stack). Object state. public void markWork(Work w) { int mark = allocateMark(w); if(w.late()) mark = mark * w.getPenalty(); w.setMark(mark); } public void markStudent(Student s) { Work work[] = s.getWork(); if(work != null) { for(Work w : work){ markWork(w); } public void markAllStudents() { Students students[] = getStudents(); if(students != null) { for(Student s : students){ markStudent(s); } markAllStudents markStudent markWork Call Call stack

Debuggers Debuggers are both language- and environment-specific. – BlueJ and Eclipse have integrated debugger. Support breakpoints. Step and Step-into controlled execution. Call sequence (stack). Object state. public void markWork(Work w) { int mark = allocateMark(w); if(w.late()) mark = mark * w.getPenalty(); w.setMark(mark); } public void markStudent(Student s) { Work work[] = s.getWork(); if(work != null) { for(Work w : work){ markWork(w); } public void markAllStudents() { Students students[] = getStudents(); if(students != null) { for(Student s : students){ markStudent(s); } markAllStudents markStudent markWork Call Return Call stack

Debuggers Debuggers are both language- and environment-specific. – BlueJ and Eclipse have integrated debugger. Support breakpoints. Step and Step-into controlled execution. Call sequence (stack). Object state. public void markWork(Work w) { int mark = allocateMark(w); if(w.late()) mark = mark * w.getPenalty(); w.setMark(mark); } public void markStudent(Student s) { Work work[] = s.getWork(); if(work != null) { for(Work w : work){ markWork(w); } public void markAllStudents() { Students students[] = getStudents(); if(students != null) { for(Student s : students){ markStudent(s); } markAllStudents markStudent Call Call stack

Debuggers Debuggers are both language- and environment-specific. – BlueJ and Eclipse have integrated debugger. Support breakpoints. Step and Step-into controlled execution. Call sequence (stack). Object state. public void markWork(Work w) { int mark = allocateMark(w); if(w.late()) mark = mark * w.getPenalty(); w.setMark(mark); } public void markStudent(Student s) { Work work[] = s.getWork(); if(work != null) { for(Work w : work){ markWork(w); } public void markAllStudents() { Students students[] = getStudents(); if(students != null) { for(Student s : students){ markStudent(s); } markAllStudents markStudent markWork Call Call stack

Debuggers Debuggers are both language- and environment-specific. – BlueJ and Eclipse have integrated debugger. Support breakpoints. Step and Step-into controlled execution. Call sequence (stack). Object state. public void markWork(Work w) { int mark = allocateMark(w); if(w.late()) mark = mark * w.getPenalty(); w.setMark(mark); } public void markStudent(Student s) { Work work[] = s.getWork(); if(work != null) { for(Work w : work){ markWork(w); } public void markAllStudents() { Students students[] = getStudents(); if(students != null) { for(Student s : students){ markStudent(s); } markAllStudents markStudent markWork Call Return Call stack

Debuggers Debuggers are both language- and environment-specific. – BlueJ and Eclipse have integrated debugger. Support breakpoints. Step and Step-into controlled execution. Call sequence (stack). Object state. public void markWork(Work w) { int mark = allocateMark(w); if(w.late()) mark = mark * w.getPenalty(); w.setMark(mark); } public void markStudent(Student s) { Work work[] = s.getWork(); if(work != null) { for(Work w : work){ markWork(w); } public void markAllStudents() { Students students[] = getStudents(); if(students != null) { for(Student s : students){ markStudent(s); } markAllStudents markStudent Call Call stack

Debuggers Debuggers are both language- and environment-specific. – BlueJ and Eclipse have integrated debugger. Support breakpoints. Step and Step-into controlled execution. Call sequence (stack). Object state. public void markWork(Work w) { int mark = allocateMark(w); if(w.late()) mark = mark * w.getPenalty(); w.setMark(mark); } public void markStudent(Student s) { Work work[] = s.getWork(); if(work != null) { for(Work w : work){ markWork(w); } public void markAllStudents() { Students students[] = getStudents(); if(students != null) { for(Student s : students){ markStudent(s); } markAllStudents markStudent Call Return Call stack

Debuggers Debuggers are both language- and environment-specific. – BlueJ and Eclipse have integrated debugger. Support breakpoints. Step and Step-into controlled execution. Call sequence (stack). Object state. public void markWork(Work w) { int mark = allocateMark(w); if(w.late()) mark = mark * w.getPenalty(); w.setMark(mark); } public void markStudent(Student s) { Work work[] = s.getWork(); if(work != null) { for(Work w : work){ markWork(w); } public void markAllStudents() { Students students[] = getStudents(); if(students != null) { for(Student s : students){ markStudent(s); } markAllStudents Call stack

Debuggers Debuggers are both language- and environment-specific. – BlueJ and Eclipse have integrated debugger. Support breakpoints. Step and Step-into controlled execution. Call sequence (stack). Object state. public void markWork(Work w) { int mark = allocateMark(w); if(w.late()) mark = mark * w.getPenalty(); w.setMark(mark); } public void markStudent(Student s) { Work work[] = s.getWork(); if(work != null) { for(Work w : work){ markWork(w); } public void markAllStudents() { Students students[] = getStudents(); if(students != null) { for(Student s : students){ markStudent(s); } markAllStudents markStudent Call Call stack Etc…

Summary we aimed to save $125 million dollars Debugging – Syntax and Logical Errors Testing Strategies – Equivalence Classes and Boundary Values – Test Automation Debuggers