Chapter 11: Testing, Testing…1, 2, 3 (Selecting Test Data)
Chapter Objectives After studying Chapter 11, you should be able to: Select appropriate test data for an application Prevent the entry of unwanted characters in a text box Create a message box with the MessageBox.Show method Trim leading and trailing spaces from a string Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
Will Your Application Pass the Test? Test a program using both valid and invalid data Valid data is data that the program is expecting the user to enter Invalid data is data that the program is not expecting the user to enter Invalid data typically is the result of the user making a typing error, entering the data in an incorrect format, or neglecting to make a required entry Figure 11-1 Guidelines for selecting an application’s test data Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The Coffee Palace- Version 1 Application The interface provides a text box for entering the number of pounds of coffee ordered The number of pounds may contain a decimal place Each pound of coffee costs $13 When the user clicks the Calculate button, the button’s Click event procedure calculates and displays the total price of the order Figure 11-2 Interface for the Coffee Palace–Version 1 application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The Coffee Palace- Version 1 Application (Cont.) Figure 11-3 Click procedure in the Coffee Palace–Version 1 application Figure 11-6 Test data chart for the Coffee Palace–Version 1 application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The Coffee Palace- Version 2 Application Except for the title bar text, the interface is identical to the one in the Coffee Palace–Version 1 application In this version of the application, the number of pounds entered in the Pounds ordered text box must be an integer Each pound of coffee still costs $13 Figure 11-7 Interface for the Coffee Palace–Version 2 application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The Coffee Palace- Version 2 Application (Cont.) Figure 11-8 Click procedure in the Coffee Palace–Version 2 application Figure 11-9 Test data chart for the Coffee Palace–Version 2 application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The Coffee Palace- Version 2 Application (Cont.) Figure 11-10 Result of entering a non-integer in the text box The TryParse method in the btnCalc_Click procedure cannot convert the non-integer 4.5 to the Integer data type, so it assigns the number 0 to the intPounds variable When the intPounds variable contains the number 0, the total price will be 0 Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The Coffee Palace- Version 2 Application (Cont.) Stop! This Is a Restricted Area! The application expects the user to enter the number of pounds as an integer (no letters, periods, or special characters) Prevent the text box from accepting the character by coding the text box’s KeyPress event procedure A control’s KeyPress event occurs each time the user presses a key while the control has the focus Use the e parameter’s KeyChar property to determine the pressed key (KeyChar stands for “key character.”) Use the e parameter’s Handled property to cancel the pressed key if it is an inappropriate one Use the ControlChars.Back constant to refer to the Backspace key on a computer keyboard (necessary for editing the text box entry) Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The Coffee Palace- Version 2 Application (Cont.) Figure 11-11 Examples of preventing a text box from accepting certain characters Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The Shady Hollow Hotel Application The interface for the Shady Hollow Hotel application displays the daily rate for a room at the hotel The daily rate for a Standard room is $90 The daily rate for a Deluxe room is $115 The daily rate for a Suite is $130 Figure 11-13 Interface for the Shady Hollow Hotel application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The Shady Hollow Hotel Application (Cont.) Figure 11-14 btnGet_Click procedure in the Shady Hollow Hotel application Figure 11-15 Test data chart for the Shady Hollow Hotel application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The CD Shop Application The interface for the CD Shop application provides a text box for entering the number of CDs (compact discs) purchased Each CD costs $10 The CD Shop offers the discounts When the user clicks the Calculate button, the button’s Click event procedure calculates and displays the total due Figure 11-16 Interface for the CD Shop application Figure 11-17 CD Shop discounts Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The CD Shop Application (Cont.) Figure 11-21 Test data chart for the CD Shop application Figure 11-18 btnCalc_Click and txtCds_KeyPress procedures Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The Tiger Hotel Application The Tiger Hotel application’s interface provides a text box for entering the room type Standard Rooms cost $90 Deluxe Rooms cost $115 When the user clicks the Display Rate button, the button’s Click event procedure displays the daily rate in the Daily rate box Figure 11-22 Interface for the Tiger Hotel application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The Tiger Hotel Application (Cont.) Figure 11-23 btnDisplay_Click procedure in the Tiger Hotel application Figure 11-24 Test data chart for the Tiger Hotel application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The Tiger Hotel Application (Cont.) I Need to Tell You Something Create a message box using the MessageBox.Show method Text in the titleBarText argument appears in the form’s title bar The MessageBoxButtons.OK and MessageBoxIcon.Information arguments display an OK button and an Information icon Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The Tiger Hotel Application (Cont.) Figure 11-25 Basic syntax and an example of the MessageBox.Show method Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The Tiger Hotel Application (Cont.) Figure 11-27 Modified test data chart for the modified Tiger Hotel application Figure 11-26 Modified If…Then…Else statement Figure 11-28 Message Box Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The Tiger Hotel Application (Cont.) Just When You Thought It Was Safe No matter how thoroughly you test an application, it may still contain some errors, called bugs It’s almost impossible to create a set of test data that covers every possible scenario the application will encounter The number of bugs is directly related to the size and complexity of the application Figure 11-29 Result of including a space after the room type Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The Tiger Hotel Application (Cont.) Several ways to fix the bug in the application Change the txtType control’s MaxLength property to 1 - this limits the text box entry to one character Use the txtType control’s KeyPress event to prevent the text box from accepting the space character Use the Trim method to remove any spaces that appear before and after the room type Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The Tiger Hotel Application (Cont.) Figure 11-30 Basic syntax and examples of the Trim method Figure 11-31 Final test data chart for the Tiger Hotel application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
The Tiger Hotel Application (Cont.) Figure 11-32 Final btnDisplay_Click procedure Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
Summary Thoroughly test a program, using both valid and invalid data, before releasing the program to the user Use a test data chart to record an application’s test data and the expected results Prevent a text box from accepting a character by coding the text box’s KeyPress event procedure Use the e parameter’s KeyChar property to determine the pressed key Clearly Visual Basic: Programming with Microsoft Visual Basic 2012
Summary (Cont.) When there is a change to an application’s code, retest the application using the data listed in its test data chart Use the MessageBox.Show method to display a message while an application is running The Trim method makes a temporary copy of a string It then performs the necessary trimming on the copy only Clearly Visual Basic: Programming with Microsoft Visual Basic 2012