testing and debugging version 1.0. all kinds of things can go wrong when you are developing a...

28
Testing and Debugging Version 1.0

Upload: kristopher-parsons

Post on 02-Jan-2016

226 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 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

Testing and DebuggingVersion 1.0

Page 2: 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

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

Page 3: 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

Learning to interpret compiler messages andspot syntax errors in your code comes with lotsof experience. There is no magic bullet here.

Page 4: 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

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.

Page 5: 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

Unit Testing

Unit testing is a powerful tool programmers use totest individual methods, or simple programs, to makesure that they are working correctly.

Page 6: 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

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.

Page 7: 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

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 + …

Page 8: 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

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

Page 9: 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

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.

Page 10: 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

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.

Page 11: 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

How do you know what values touse when testing the method?

Page 12: 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

You should test with

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

Page 13: 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

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.

Page 14: 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

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

Page 15: 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

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.

Page 16: 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

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.

Page 17: 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

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.

Page 18: 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

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.

Page 19: 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

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

Page 20: 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

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.

Page 21: 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

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.

Page 22: 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

Where’s the bug?

Page 23: 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

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? . . .

Page 24: 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

Things you can do to understand the flow of control

* Draw an activity diagram * Desk check your code

Page 25: 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

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

Page 26: 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

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.

Page 27: 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

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)

Page 28: 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

The lab assignment this week introducesyou to the debugger that is built in toVisual C# Express Edition.