Presentation is loading. Please wait.

Presentation is loading. Please wait.

How to Test Methods Computer Science 3 Gerb Objective: Test methods properly.

Similar presentations


Presentation on theme: "How to Test Methods Computer Science 3 Gerb Objective: Test methods properly."— Presentation transcript:

1 How to Test Methods Computer Science 3 Gerb Objective: Test methods properly

2 Why Test Methods Modern software systems are built a piece at a time Often reuse pieces of one system for another Therefore, not enough to know that whole system works properly –Want to know each piece works properly –Want to be able to test a piece of the system –Smallest testable pieces are methods

3 What does it mean for a method to work properly? No matter how complex or simple, all methods receive inputs and produce outputs Inputs –Parameters –Input from keyboard, mouse, etc. Outputs –Return value –Output to screen A method works properly when it produces the correct outputs in response to its inputs

4 How can you tell whether a method is correct? Only one way Give it inputs Look at the outputs If the outputs are what you would expect given the inputs, you have reason to believe that the method works in that case

5 What to Test? Not good enough just to call once –Devise multiple tests –Vary the values of the inputs Strive for complete code coverage. I.e. each line is tested at least once Test boundary conditions –Test at least once on each side –Test at the boundary

6 For Example Writing a method to return true if a student is eligible for a scholarship award Specs –Freshmen are not eligible –Sophomores are eligible only with 3.5 GPA or better –Juniors and seniors are eligible with a 3.0 GPA or better

7 Example – What test cases? static boolean eligibleForAward(int grade, double gpa){ boolean eligible=true; if (grade==9) eligible = false; else if (grade==10){ if (gpa < 3.5) eligible = false; } else if (gpa < 3.0) eligible = false; return eligible;}

8 Example – What test cases? static boolean eligibleForAward(int grade, double gpa){ boolean eligible=true; if (grade==9) eligible = false; else if (grade==10){ if (gpa < 3.5) eligible = false; } else if (gpa < 3.0) eligible = false; return eligible;} 9 th grade 10 th grade –gpa < 3.5 –gpa > 3.5 –gpa = 3.5 Other –gpa < 3 –gpa > 3 –gpa = 3

9 How do you test Call the method Look at –Value returned by method –Anything normally output by method Never, never, never, never, never, never do: –Test by putting unnecessary prints in your method –You’re not testing what the method actually does –Possibility of misinterpreting results –Possibility of breaking function when you take them out

10 Don’t do this!!! static boolean eligibleForAward(int grade, double gpa){ boolean eligible=true; if (grade==9) eligible = false; else if (grade==10){ if (gpa < 3.5) eligible = false; } else if (gpa < 3.0) System.out.println(eligible); eligible = false; return eligible;}

11 Drivers Methods used to test other methods Avoid “throw away” tests –Changing your driver for each test or typing test data by hand –Makes retesting hard –Instead, add each new test to existing driver –Now can retest the entire module more easily Often a java class includes a main method that runs tests on the methods in the class

12 Summary Often want to test methods separately Create drivers for this purpose –Look at the method’s return value and output –Never, never, print from a methods for the purpose of testing it! Use multiple test cases No throwaway tests


Download ppt "How to Test Methods Computer Science 3 Gerb Objective: Test methods properly."

Similar presentations


Ads by Google