Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Testing and Debugging Version 1.0

2 All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code Your program runs, but produces the wrong answers Your program crashes unexpectedly

3 Learning to interpret compiler messages and spot syntax errors in your code comes with lots of experience. There is no magic bullet here.

4 But what do you after your program compiles. How do you know that it runs correctly? How do you isolate and fix problems? That’s where testing and debugging come in.

5 Unit Testing Unit testing is a powerful tool programmers use to test individual methods, or simple programs, to make sure that they are working correctly.

6 Unit Testing When unit testing a method, compile the method outside of the program it will be used in, along with a test harness that will feed the method a series of test values. Test values may come as user input, from a loop, or from a random number generator.

7 Suppose, for example, that you have written a method that calculates e x using the series e x = x / 1 + x 2 / 2 + x 3 / 6 + x 4 / 24 + x 5 / 120 + …

8 Write the method (for integer values of x) // The Etox method static double Etox(int x) { double divisor = 1.0; double newex = 1.0; double ex = 0.0; double EPSILON = 0.0001; int p = 1; do { ex = newex; divisor = divisor * p; newex = ex + Math.Pow(x, p) / divisor; p++; } while (Math.Abs(newex - ex) > EPSILON); return ex; }//End Etox() This method uses a loop that keeps adding terms to the series until the change in the result is smaller than 0.0001

9 Now write a test harness. This code is simply a driver that repeatedly tests the method with different values. In this case the input comes from the user.

10 The Test Harness static void Main() { int inputValue = 0; Console.WriteLine("Test Harness for e to the x method"); do { Console.Write("Input a test value... 0 to quit: "); inputValue = int.Parse(Console.ReadLine()); double ex = etox(inputValue); Console.WriteLine("For a value of x = {0} e to the x = {1}", inputValue, ex); } while (inputValue != 0); }//End Main() This methodcalls the Etox method and prints the results, which you can verify manually.

11 How do you know what values to use when testing the method?

12 You should test with Typical values that a user might call the method with Boundary values Invalid values that you know should fail

13 As a method of the real variable x, the graph of y=e x is always positive (above the x axis) and increasing (viewed left-to-right). It never touches the x axis, although it gets arbitrarily close to it.

14 Some interesting test cases might be when x = 0, and when x is a large negative number.

15 As x gets very very large or very very small, we can expect this method to overflow, i.e. a double won’t be big enough to hold the answer.

16 This kind of testing is known as Black-Box testing, since there is no consideration of the internal workings of the method. We only give it some fixed set of inputs and see if the method produces the correct outputs.

17 Testing that looks at what is happening inside of a method as it executes is called White Box testing. In white box testing you should be sure that your test cases exercise every code path in the method.

18 Many branches in a program are provided to handle bad input values (for example, an invalid file name). Be sure that You test these branches too. Your program should do the correct thing in the presence of bad input.

19 How do you know if your outputs are correct or not? You can calculate them by hand You can look them up in a table You can do a “reverse” calculation You can use another, perhaps less efficient means of calculating the result

20 Debugging When testing reveals problems in your code, you have to find out why your code doesn’t work correctly and fix it. This process is called debugging.

21 The basic steps required to debug code are: 1.Recognize that an error (bug) exists in the code 2.Isolate the error – where is it in the code 3.Identify the source of the bug – what causes it 4.Figure out how to fix the code 5.Apply the fix and re-test the code.

22 Where’s the bug?

23 Before you can successfully locate a bug in Your code you have to know what it is that your program is supposed to be doing. Why is each branch taken? How many times should a loop be executed? What values do you expect in your variables at each step of the program?...

24 Things you can do to understand the flow of control * Draw an activity diagram * Desk check your code

25 Program Traces If you are not sure what paths are being taken in a program, or how values may change as the program is executed, you can insert trace messages at key points within the program. You can print out values of parameters or other important data elements at these points. Each time a method is entered Each time a method returns Each time a loop is entered Each time a branch is taken

26 Program Traces Using program traces can be time consuming, and … you have to be careful to take all trace statements out of your program when you are done … but it can be a very effective tool.

27 Using a Debugger Most modern software development environments include a built in debugger. Debuggers allow you to Create breakpoints (stopping points in your code) Step through your code a line at a time (single step) Inspect variables (watch windows)

28 The lab assignment this week introduces you to the debugger that is built in to Visual C# Express Edition.


Download ppt "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."

Similar presentations


Ads by Google