Testing and Debugging in VB.NET

24
Last Modified: 03/23/22 University of South Alabama School of CIS 1 ITE 370: Debugging in VB.NET Testing and Debugging in VB.NET Testing and Debugging Types of errors Testing and debugging strategies Bug Lists Exception Handling Debugging tools The Command window The Locals Window

description

Testing and Debugging in VB.NET. Testing and Debugging Types of errors Testing and debugging strategies Bug Lists Exception Handling Debugging tools The Command window The Locals Window. Types of Errors. Syntax errors Execution errors Logic errors. Syntax (Compile-time) Errors. - PowerPoint PPT Presentation

Transcript of Testing and Debugging in VB.NET

Last Modified: 04/19/23

University of South Alabama School of CIS 1

ITE 370: Debugging in VB.NET

Testing and Debugging in VB.NET

• Testing and Debugging

– Types of errors

– Testing and debugging strategies

– Bug Lists

– Exception Handling

– Debugging tools

– The Command window

– The Locals Window

Last Modified: 04/19/23

University of South Alabama School of CIS 2

ITE 370: Debugging in VB.NET

Types of Errors

• Syntax errors

• Execution errors

• Logic errors

Last Modified: 04/19/23

University of South Alabama School of CIS 3

ITE 370: Debugging in VB.NET

Syntax (Compile-time) Errors

• Caused by incorrectly constructed code

– misspelling a keyword

– comma out of place

• Detected at compile time

• it is a recommended practice to first correct all syntax errors before doing execution-based testing

Last Modified: 04/19/23

University of South Alabama School of CIS 4

ITE 370: Debugging in VB.NET

Execution (Run-time) Errors

• Occur during program execution

• When program is syntactically correct

• Program usually stops running (crashes)

• Usually caused by computer’s attempt to carry out an impossible operation, such as

– Division by zero

– Read past end-of-file

– Type mismatch

Last Modified: 04/19/23

University of South Alabama School of CIS 5

ITE 370: Debugging in VB.NET

Logic Errors

• Occur when program does not perform according to its specifications

• Syntactically correct, executes without any RTEs, but produces incorrect results due to faulty program logic

• Unlike syntax and execution errors, logic errors cannot be detected by the computer system, but by

– Inspections (code reviews)

– Observation (execution-based testing, observing incorrect results)

Last Modified: 04/19/23

University of South Alabama School of CIS 6

ITE 370: Debugging in VB.NET

Getting Help with Errors

• Help button delivers help with exceptions

• Break button invokes code window at point of error

Last Modified: 04/19/23

University of South Alabama School of CIS 7

ITE 370: Debugging in VB.NET

Testing and Debugging Strategies

• Thoroughly understand what each section of code is trying to accomplish

• Test systematically

– using stubs (top-down)

– and drivers (bottom-up)

• Code and test incrementally

– “code a little, test a lot, save a lot”

• Pay close attention to text of any error messages

• Note what code editor highlights

Last Modified: 04/19/23

University of South Alabama School of CIS 8

ITE 370: Debugging in VB.NET

Testing and Debugging Strategies (cont.)

• Test cases

– Black box - based on the program specifications

– White box - based on programming logic

– Consist of input data and expected result

• Test with large and small values of numbers and strings

• Test around and at boundary values

• Test your program on other computer systems

• Re-run tests after making changes to program (regression testing)

Last Modified: 04/19/23

University of South Alabama School of CIS 9

ITE 370: Debugging in VB.NET

Testing and Debugging Strategies (cont.)

• Use debugging tools and/or message boxes to examine the results of actions

• Understand the state of variables, controls, and files and databases at the time of the error

– prior conditions contribute to problem

– example: use of .Read method failed because end-of-file condition was already true

• Get someone else to run your program

– they might test something you didn’t think of

• Test how your program handles errors

– Exception handling

Last Modified: 04/19/23

University of South Alabama School of CIS 10

ITE 370: Debugging in VB.NET

Bug Lists

• Use bug lists as you test and debug

• A bug list is simply a list of failures encountered during execution-based testing

• Informal, handwritten lists are acceptable

• Three columns of information in a bug list

– A description of the failure

– A column to check once you have repaired the error

– A column to check once you have verified that the error has been repaired

Last Modified: 04/19/23

University of South Alabama School of CIS 11

ITE 370: Debugging in VB.NET

Bug Lists (cont.)

• Use bug lists to help you remember

– what failures need to be found and repaired

– which repairs need to be verified

• Add more failures to the list as you encounter them during retesting

• Unless you need to keep the list for documentation purposes, simply throw it away when done

Last Modified: 04/19/23

University of South Alabama School of CIS 12

ITE 370: Debugging in VB.NET

Testing and Debugging Strategies (cont.)

Description of failure Fixed? Verified? Heading shows up for first invoice only x x Values in sub-totals are incorrect x Last line item for each invoice shows up as first item on the next (incorrect) invoice

x

Heading shows up for all but the first invoice

x

Grand totals are not showing up x x Grand total values are incorrect (equal to the sub-total for the last invoice only)

• Bug lists are useful for test and debug process

Last Modified: 04/19/23

University of South Alabama School of CIS 13

ITE 370: Debugging in VB.NET

Exception Handling in VB.NET

• An exception is

– A runtime error

– A class containing information about a runtime error

• When an invalid operation occurs, it is said that “an exception will be thrown”

– This means that VB will check to see if your program is prepared to handle the exception

– Or else an error message is displayed and your program is terminated

Last Modified: 04/19/23

University of South Alabama School of CIS 14

ITE 370: Debugging in VB.NET

Writing an Exception Handler

• An Exception is handled inside of a Try statement

• Write a code block suspected of causing a RTE inside the Try block

• Write the exception handler inside the Catch block

– An exception handler is code that is executed whenever a runtime error occurs inside the Try block

• When a RTE occurs inside a Try block

– Your program is interrupted

– Control is passed to a Catch handler

Last Modified: 04/19/23

University of South Alabama School of CIS 15

ITE 370: Debugging in VB.NET

Exception Handling in VB.NET

Try ' Starts a structured exception handler. ' Place executable statements that may generate ' an exception in this block.

Catch [optional filters] ' This code runs if the statements listed in ' the Try block fail and the filter on the Catch statement is true.

[Additional Catch blocks] Finally

' This code always runs immediately before ' the Try statement exits.

End Try ' Ends a structured exception handler.

Last Modified: 04/19/23

University of South Alabama School of CIS 16

ITE 370: Debugging in VB.NET

Exception Handler - Example

connBus.ConnectionString = "Provider=.Jet.OLEDB.4.0…” Try connBus.Open()Catch DbErr As Exception MsgBox("Run-time error: " & DbErr.Message & _

”; source = " & DbErr.Source) Throw ‘* lets another handler handle the errorFinally MsgBox(“Call the system administrator”)End Try

Last Modified: 04/19/23

University of South Alabama School of CIS 17

ITE 370: Debugging in VB.NET

The Debug Toolbar

Step Into

Step Over

Step Out

BreakpointsStop

Break

Continue

Restart

Show next statement

Last Modified: 04/19/23

University of South Alabama School of CIS 18

ITE 370: Debugging in VB.NET

Debugging Tools

• The Breakpoint– Enables you to suspend program execution when a

specified line of code is encountered– Set by putting insertion point in the line and clicking

the breakpoint button– Breakpoints are indicated by brown highlight– You may have multiple breakpoints set in the program– Clicking the run button again causes program

execution to continue to next breakpoint or pause– Debugging menu has “Toggle breakpoints” and “Clear

all breakpoints” choices

Last Modified: 04/19/23

University of South Alabama School of CIS 19

ITE 370: Debugging in VB.NET

Debugging Tools (cont.)

• Quick Watch

– Allows you to examine the value of an expression

– Program must be halted

• Paused at a breakpoint

• Paused using the pause button

• Paused by a runtime error

– Highlight expression or variable

– Debug | QuickWatch

Last Modified: 04/19/23

University of South Alabama School of CIS 20

ITE 370: Debugging in VB.NET

Debugging Tools (cont.)

• The Calls button– Enables you to

examine the calls stack, which consists of the procedures that are active when the program is paused with a breakpoint

– Window shows sequence of active calls

– Double-click to display in code window

Last Modified: 04/19/23

University of South Alabama School of CIS 21

ITE 370: Debugging in VB.NET

Debugging Tools (cont.)

• Stepping through code

– Step To Cursor: executes statements and pauses at the line where cursor sits in code window

– Step Into: executes next statement and steps into a procedure, pausing at its first statement

– Step Over: executes next statement, executing a procedure call without stepping into procedure; pauses at the next line of the current routine

– Step Out: executes remaining statements in a procedure and pauses at the first line after the procedure call in the calling procedure

Last Modified: 04/19/23

University of South Alabama School of CIS 22

ITE 370: Debugging in VB.NET

The Command Window – Immediate mode

• Allows immediate execution of code

– View current variable values with ? statement if program is paused

– Set variable values

Last Modified: 04/19/23

University of South Alabama School of CIS 23

ITE 370: Debugging in VB.NET

Last Modified: 04/19/23

University of South Alabama School of CIS 24

ITE 370: Debugging in VB.NET

The Locals Window

• Automatically displays all of the declared variables in a procedure

• Includes value and data type

• Updated each time program enters break mode