COMP 121 Week 1
Download
Report
Transcript COMP 121 Week 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
Dijkstra, E. W. (1970). Notes on structured programming. Retrieved September 4, 2007, from
http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD249.PDF
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
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
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”)
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.
Black-box vs. White-box Testing
Black-box testing describes a testing
method that does not take the structure of
the implementation into account
Based
on what is externally visible (inputs and
expected outputs)
White-box testing uses information about
the structure of a program
Based
on implementation details
Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.
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 coverage
Every
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.
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.
Test-Driven Development
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
Debugging
Informal definition -- debugging is the
process of finding and fixing problems
(bugs) in a computer program or in
computer hardware
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
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.
Debugger
A debugger is a program that you can use
to execute another program and analyze
its run-time behavior
Basic debugger functions:
Setting
breakpoints
Stepping through code
Inspecting variables
Horstmann, C. (2008). Big Java (3rd ed.). New York: John Wiley & Sons.
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.pdf
Debugger demonstration is on pages 27-30
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/
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
Any Questions?