Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a...
-
Upload
kristopher-parsons -
Category
Documents
-
view
221 -
download
1
Transcript of Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a...
Testing and DebuggingVersion 1.0
All kinds of things can go wrong when you are developing a program.
The compiler discovers syntax errors in your codeYour program runs, but produces the wrong answers
Your program crashes unexpectedly
Learning to interpret compiler messages andspot syntax errors in your code comes with lotsof experience. There is no magic bullet here.
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.
Unit Testing
Unit testing is a powerful tool programmers use totest individual methods, or simple programs, to makesure that they are working correctly.
Unit Testing
When unit testing a method, compile the methodoutside of the program it will be used in, along witha test harness that will feed the method a seriesof test values.
Test values may come as user input, from a loop,or from a random number generator.
Suppose, for example, that you have writtena method that calculates ex using the series
ex = x / 1 + x2 / 2 + x3 / 6 + x4 / 24 + x5 / 120 + …
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 loopthat keeps adding terms to the series until the change in the result is smaller than 0.0001
Now write a test harness. This code is simply a driver that repeatedly tests themethod with different values. In this casethe input comes from the user.
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 Etoxmethod and prints theresults, which you canverify manually.
How do you know what values touse when testing the method?
You should test with
• Typical values that a user might call the method with• Boundary values• Invalid values that you know should fail
As a method of the real variable x, the graph of y=ex 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.
Some interesting test cases might be when x = 0, andwhen x is a large negative number.
As x gets very very large or very very small, we can expectthis method to overflow, i.e. a double won’t be big enough to hold the answer.
This kind of testing is known as Black-Boxtesting, since there is no consideration ofthe internal workings of the method. Weonly give it some fixed set of inputs andsee if the method produces the correctoutputs.
Testing that looks at what is happeninginside of a method as it executes iscalled White Box testing. In white box testing you should be sure that your testcases exercise every code path in themethod.
Many branches in a program are providedto handle bad input values (for example, aninvalid file name). Be sure that You test these branches too. Your program should do the correct thing in the presence of bad input.
How do you know if your outputsare 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
Debugging
When testing reveals problems in your code,you have to find out why your code doesn’twork correctly and fix it. This process is calleddebugging.
The basic steps required to debug code are:
1. Recognize that an error (bug) exists in the code2. Isolate the error – where is it in the code3. Identify the source of the bug – what causes it4. Figure out how to fix the code5. Apply the fix and re-test the code.
Where’s the bug?
Before you can successfully locate a bug inYour 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? . . .
Things you can do to understand the flow of control
* Draw an activity diagram * Desk check your code
Program Traces
If you are not sure what paths are being taken in aprogram, or how values may change as the programis executed, you can insert trace messages at key points within the program. You can print out valuesof parameters or other important data elementsat these points.
Each time a method is enteredEach time a method returnsEach time a loop is enteredEach time a branch is taken
Program Traces
Using program traces can be time consuming, and …you have to be careful to take all trace statementsout of your program when you are done … but it canbe a very effective tool.
Using a Debugger
Most modern software development environmentsinclude a built in debugger. Debuggers allow youto
• Create breakpoints (stopping points in your code)• Step through your code a line at a time (single step)• Inspect variables (watch windows)
The lab assignment this week introducesyou to the debugger that is built in toVisual C# Express Edition.