19
COMP 121 Week 1: Testing and Debugging

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

Embed Size (px)

Citation preview

Page 1: 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

COMP 121

Week 1:

Testing and Debugging

Page 2: 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

Testing

Program testing can be used to

show the presence of bugs,

but never to show their absence!

~ Edsger Dijkstra

Dijkstra, E. W. (1970). Notes on structured programming. Retrieved September 4, 2007, from http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD249.PDF

Page 3: 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

Testing is an Important Skill

Microsoft … we have as many testers as we have developers. And testers spend all their time testing, and developers spend half their time

testing. We're more of a testing, a quality software organization than we're a software

organization.

~ Bill Gates

Foley, J. & Murphy, C. (2002). Q&A: Bill Gates on trustworthy computing. InformationWeek. Retrieved September 4, 2007, from http://www.informationweek.com/story/IWK20020517S0011

Page 4: 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

Why Write Test Cases? Tests reduce bugs in new

features Tests reduce bugs in

existing features Tests are good

documentation Tests reduce the cost of

change Tests improve design Tests allow refactoring

Tests constrain features Tests defend against

other programmers Testing is fun Testing forces you to

slow down and think Testing makes

development faster Tests reduce fear

Burke, E.M. & Coyner, B.M. (2003). Top 12 reasons to write unit tests. Retrieved September 1, 2007, from http://www.onjava.com/pub/a/onjava/2003/04/02/javaxpckbk.html

Page 5: 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

What is a Test Case?

Informal definition -- a test case is a piece of code that programmatically checks that another piece of code works correctly

Each test case is a method that usually tests another method (often called the “method under test”)

Page 6: 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

Some Types of Test Cases Positive test cases test that the method under test works

correctly with expected, legitimate inputs “happy path” Test for success

Boundary test cases test how the method under test handles values that lie at the boundary of acceptable inputs Legal values that often cause problems in code

zero, empty strings, null object references Negative test cases try to show that the method under

test does not work or that the method under test handles error conditions

Open-ended Test for failure

Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

Page 7: 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

Black-box vs. White-box Testing

Black-box testing describes a testing method that does not take the structure of the implementation into accountBased on what is externally visible (inputs and

expected outputs) White-box testing uses information about

the structure of a programBased on implementation details

Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

Page 8: 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

Regression Testing and Test Coverage Regression testing

Repeating previously run tests to ensure that known failures of prior versions do not appear in the new versions of the software

Test coverageEvery line of executable code should be

executed at least once by test cases Every if / else branch All paths through your code

Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

Page 9: 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

Question: How many test cases do you need to cover all branches of the getDescription method shown below? What are the boundaries?public String getDescription() { String r; if (richter >= 8.0) r = “Most structures fall”; else if (richter >= 7.0) r = “Many buildings destroyed”; else if (richter >= 6.0) r = “Many buildings considerably damaged, some collapse”; else if (richter >= 4.5) r = “Damage to poorly constructed buildings”; else if (richter >= 3.5) r = “Felt by many people, no destruction”; else if (richter >= 0) r = “Generally not felt by people”; else r = “Negative numbers are not valid”; return r; } private double richter;

Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

Page 10: 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

Test-Driven Development

Page 11: 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

Testing in BlueJ

BlueJ allows for ad hoc and systematic unit testing Ad hoc testing allows you to test a method

interactively Systematic testing is done using the JUnit regression

testing framework BlueJ can be used to generate test cases or to build

test cases by hand When you installed BlueJ, it should have

included a tutorial on testing which also can be downloaded from:

http://www.bluej.org/tutorial/testing-tutorial.pdf

Page 12: 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

Debugging

Informal definition -- debugging is the process of finding and fixing problems (bugs) in a computer program or in computer hardware

Page 13: 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

The First Bug

Department of the Navy. (2006). Online library of selected images: Rear Admiral Grace Murray Hopper, USNR, (1906-1992). Retrieved September 4, 2007, from http://www.history.navy.mil/photos/pers-us/uspers-h/g-hoppr.htm

Page 14: 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

Steps to Debug a Program

Reproduce the error Simplify the error Divide and conquer Know what your program should do Look at all details Make sure you understand each bug

before you fix it

Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

Page 15: 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

Debugger

A debugger is a program that you can use to execute another program and analyze its run-time behavior

Basic debugger functions:Setting breakpointsStepping through code Inspecting variables

Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.

Page 16: 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

Debugging in BlueJ A simple debugger is included in the BlueJ

development environment Learning to use it can save you lots of time When you installed BlueJ, it should have

included a tutorial which also can be downloaded from:

http://www.bluej.org/tutorial/tutorial.pdfDebugger demonstration is on pages 27-30

Page 17: 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

Debugging Videos

An ITiCSE working group created a repository of debugging video tutorials that demonstrate some debugging techniques, including how to use the BlueJ debugger

http://debug.csi.muohio.edu/

Page 18: 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

Summary Testing is an important part of software

development Test cases can be used to develop code, find

bugs, and make sure changes don’t break old code

Test cases should provide full coverage, test the inputs and expected outputs, test boundaries, etc.

JUnit is a framework used to develop test cases BlueJ supports JUnit test cases and also

includes a debugger to help fix problems found in code

Page 19: 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

Any Questions?