David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP 112 2014T1.

Slides:



Advertisements
Similar presentations
Copyright W. Howden1 Programming by Contract CSE 111 6/4/2014.
Advertisements

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.
Detecting Bugs Using Assertions Ben Scribner. Defining the Problem  Bugs exist  Unexpected errors happen Hardware failures Loss of data Data may exist.
CS0004: Introduction to Programming Visual Studio 2010 and Controls.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Recursion COMP T1.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Interface COMP T1.
CMSC 345, Version 11/07 SD Vick from S. Mitchell Software Testing.
ITEC200 Week02 Program Correctness and Efficiency.
Program Correctness and Efficiency Chapter 2. Chapter 2: Program Correctness and Efficiency2 Chapter Objectives To understand the differences between.
JUnit. What is unit testing? A unit is the smallest testable part of an application. A unit test automatically verifies the correctness of the unit. There.
Program Correctness and Efficiency Chapter 2. Chapter Objectives  To understand the differences between the three categories of program errors  To understand.
JUnit, Revisited 17-Apr-17.
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
16/27/2015 3:38 AM6/27/2015 3:38 AM6/27/2015 3:38 AMTesting and Debugging Testing The process of verifying the software performs to the specifications.
Writing a Unit test Using JUnit At the top of the file include: import junit.framework.TestCase; The main class of the file must be: public Must extend.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects Real and Java COMP.
Computer Science 340 Software Design & Testing Design By Contract.
Ranga Rodrigo. Class is central to object oriented programming.
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.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington GUI and the UI API COMP.
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.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington While loops and the UI API.
Invitation to Computer Science, Java Version, Second Edition.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Arrays and ArrayLists.
CPSC1301 Computer Science 1 Overview of Dr. Java.
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.  Writing snippets of code that try to use methods (functions) from your program.  Each snippet should test one (and only one) function......by calling.
CSC 395 – Software Engineering Lecture 10: Execution-based Testing –or– We can make it better than it was. Better...faster...agiler.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae david streader, VUW Networking and Concurrency COMP.
Programming with Assertions © Allan C. Milne v
The Software Development Process
Pre- and postconditions, Using assertions and exceptions 1 Pre- and postconditions Using assertions and exceptions.
Week 14 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 1: Introduction Data.
Introduction to programming Carl Smith National Certificate Year 2 – Unit 4.
 Programming - the process of creating computer programs.
Objects First With Java A Practical Introduction Using BlueJ Well-behaved objects 2.1.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
Defensive Programming CNS 3370 Copyright 2003, Fresh Sources, Inc.
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.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
The Java Assertion. 2 Assertion A Java statement in JDK 1.4 & newer Intent: enables code to test assumptions. E.g., a method that calculates the a particle’s.
1 The Software Development Process ► Systems analysis ► Systems design ► Implementation ► Testing ► Documentation ► Evaluation ► Maintenance.
Well-behaved objects Main concepts to be covered Testing Debugging Test automation Writing for maintainability Objects First with Java - A Practical.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Recursion COMP T1.
Compilers and Interpreters
Reasoning and Design (and Assertions). How to Design Your Code The hard way: Just start coding. When something doesn’t work, code some more! The easier.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
14 Compilers, Interpreters and Debuggers
Testing Tutorial 7.
Dept of Computer Science University of Maryland College Park
Logger, Assert and Invariants
Introduction to JUnit CS 4501 / 6501 Software Testing
More JUnit CS 4501 / 6501 Software Testing
CSE 143 Error Handling [Section 2.8] 3/30/98 CSE 143.
COS 260 DAY 17 Tony Gauvin.
Introduction to JUnit CS 4501 / 6501 Software Testing
Programming in Java Assertion.
CSC 143 Error Handling Kinds of errors: invalid input vs programming bugs How to handle: Bugs: use assert to trap during testing Bad data: should never.
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
CSE 1020:Software Development
Lecture 14: Testing Testing used to verify object behavior through designed test suites Can test Classes – “unit” testing Object interactions – “integration”
Computer Science 340 Software Design & Testing
Presentation transcript:

David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1 13

COMP : 2 © David Streader Overview When the code fails to do what you want it to: 1.Stop pretending 2.Ask your self what is the code actually doing? Errors are often not observed directly. 1.Write test code 1.Setup 2.Execute 3.Observe 2.Write assert statements 3.Stop, execute one step at a time, interrogate sate as you go

COMP : 3 © David Streader Errors and Failures Unexpected behavior is a program error. It is errors that need to be corrected. A program failure occurs when the error is observed. The point at which the failure occurs need not be the same as the point at which the error occurs. The larger the gap the harder to debug. Assertions can narrow the gap between errors and failures.

COMP : 4 © David Streader Test early or suffer badly! As systems grow the cost of change grows. For a system with 1000 components changing one component can easily require the testing of 500 to 1000 components The more quality test code you have the quicker the testing. Develop software using small cycles of code and test Later you will learn Test Driven Development where Junit tests are written before the code. A suite of Junit tests can be run at the touch of button and evaluated at a glance Junit tests will be set up and run using BlueJ.

COMP : 5 © David Streader Test Code Testing can often be broken into three steps: 1.set up the execution 2.execute the method 3.display the results Having run a test the results need to be interpreted

COMP : 6 © David Streader Assertions Java Assert statements verify that some condition is true or throw an Assert exception if they are not true. assert : Use javac –ea or java –enableassertions to compile Java and include assert checks. The default is to omit them. Assert statements serve two main purposes: 1.Document what you believe to be true 2.Aid debugging Production code should not be compiled to include them.

COMP : 7 © David Streader BlueJ compiling assert To Make BlueJ compile assert statements: Very carefully edit line 507 of file Windows - “Program Files\BlueJ\lib\bluej.defs” Mac - bluej.app/Contents/Resources/Java/bluej.defs From bluej.javame.compiler.options=-source 1.4 -target 1.4 To bluej.javame.compiler.options=-source 1.4 -target 1.4 -ea

COMP : 8 © David Streader Where to place assertions Assertion as: 1.Preconditions 2.Postconditions 3.Loop invariants 4.Class invariants Pre and Post conditions often referred to as a contract. Google design by contract

COMP : 9 © David Streader Contracts A contract is an assume guarantee pair Preconditions define what a method assumes. Postconditions define what a method guarantees. The contract for a method is: 1.You give me what I expect then 2.I will guarantee to behave as you want When a precondition fails the error is in the calling code When a post condition fails the error is in the method Knowing where to look for a bug can speed up development. For complex methods it can be easier to read the contract than the code.

COMP : 10 © David Streader Pre Post Conditions To increase sofar the method adds an int parameter The intention behind the method is that: 1.assume that y is not negative. 2.guarantee that sofar is greater than 0. You could not know this by inspecting the code. Strengthen precondition to guarantee postcondition

COMP : 11 © David Streader Exercise Can the isin postcondition be broken? To be sure you need to look at all the methods of the class!

COMP : 12 © David Streader Invariants An invariant is simply a statement that is always true: Loop invariants a statements that are true every time the loop body is executed Object invariants are true before and after any method execution. Pre and post conditions are checked before and after every execution of the method. Constructing useful invariants is far from easy but can be very very useful.

COMP : 13 © David Streader Loop invariant and Postcondition Understand Code Or understand contract

COMP : 14 © David Streader Time is Money The time it takes to write a good program is related to the time it takes to debug your mistakes. Not the time it takes to write the final code. Testing should be undertaken as early as is possible. Only a fool waits till the whole program is written before testing starts How long will it take to run and interpret the tests for 1000 methods? How often will you have to repeat this process? Running and interpreting tests needs to be quick and easy else it will be done far less that is best.

COMP : 15 © David Streader Summary Larger the System the more pay off there is in adopting better test practices Design by contract – Use pre and post conditions Use loop invariants Assertions 1.Capture intention 2.Declare object invariants 3.Can be easier to understand than code 4.Apportion blame 5.Fail faster

COMP : 16 © David Streader Junit Junit has been widely adopted as a framework for unit testing java programs. The tests for the methods of class Foo should appear in a separate class FooTesT. The Junit framework makes use of @Test, … to control when tests are to link test classes into test suites. Test results are recorded using the junit Assert statements APIs provide clear visual feed back as to what tests have been passed and what failed. The point being at the push of a button all the tests can be performed and if all tests are passed a light turns green.

COMP : 17 © David Streader BlueJ and Junit Check Preferences>Interface>Show unit testing tools BlueJ allows objects to be built and methods to be executed via interaction with an easy to understand GUI. Hence once helper methods have been written you can test a method by interacting with the GUI to set up the data, run the method under test and inspect the results. Having indicted that you wish to build a test class BlueJ allows you to build a test method and then record your interaction with the GUI as method calls in the Junit test class. The output from BlueJ is a Junit test class that can be edited just like any other Java class file.