COMP 121 Week 1 Testing and Debugging Testing
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, E. W. (1970). Notes on structured programming. Retrieved September 4, 2007, from http: //www. cs. utexas. edu/users/EWD/ewd 02 xx/EWD 249. 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. Information. Week. Retrieved September 4, 2007, from http: //www. informationweek. com/story/IWK 20020517 S 0011
Why Write Test Cases? n n n 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 n n n 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? n n 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 n Positive test cases test that the method under test works correctly with expected, legitimate inputs ¨ ¨ n “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 n n 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 n n Open-ended Test for failure Horstmann, C. (2008). Big Java (3 rd ed. ). New York: John Wiley & Sons.
Black-box vs. White-box Testing n 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) n White-box testing uses information about the structure of a program ¨ Based on implementation details Horstmann, C. (2008). Big Java (3 rd ed. ). New York: John Wiley & Sons.
Regression Testing and Test Coverage n Regression testing ¨ Repeating previously run tests to ensure that known failures of prior versions do not appear in the new versions of the software n Test coverage ¨ Every line of executable code should be executed at least once by test cases Every if / else branch n All paths through your code n Horstmann, C. (2008). Big Java (3 rd ed. ). New York: John Wiley & Sons.
Question: How many test cases do you need to cover all branches of the get. Description method shown below? What are the boundaries? public String get. Description() { 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 (3 rd ed. ). New York: John Wiley & Sons.
Test-Driven Development
Testing in Blue. J 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 ¨ Blue. J can be used to generate test cases or to build test cases by hand n When you installed Blue. J, it should have included a tutorial on testing which also can be downloaded from: http: //www. bluej. org/tutorial/testing-tutorial. pdf
Debugging n 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 n Simplify the error n Divide and conquer n Know what your program should do n Look at all details n Make sure you understand each bug before you fix it n Horstmann, C. (2008). Big Java (3 rd 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 n Basic debugger functions: n ¨ Setting breakpoints ¨ Stepping through code ¨ Inspecting variables Horstmann, C. (2008). Big Java (3 rd ed. ). New York: John Wiley & Sons.
Debugging in Blue. J A simple debugger is included in the Blue. J development environment n Learning to use it can save you lots of time n When you installed Blue. J, it should have included a tutorial which also can be downloaded from: n http: //www. bluej. org/tutorial. pdf ¨ Debugger demonstration is on pages 27 -30
Debugging Videos n An ITi. CSE working group created a repository of debugging video tutorials that demonstrate some debugging techniques, including how to use the Blue. J debugger http: //debug. csi. muohio. edu/
Summary n n n 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 Blue. J supports JUnit test cases and also includes a debugger to help fix problems found in code
Any Questions?
- Slides: 19