Creating A VB Test Harness

Level:
Level1

Written By TheVBProgramer.

This tutorial explains how you can create a simple test harness that you can use when you are trying different tests. Many of the tutorials on this site use this test harness to demonstrate things. It is recommended that you save this sample program in its own folder (for example, you might call the folder where you save the sample project below "VBTestHarness"). You can then copy this folder over to another folder for each subsequent sample program. When you open the new, copied folder to do the new example, the basic structure for the program will be there, and you can concentrate on modifying the code behind the "Try It" command button.

 

  • Start a new "Standard EXE" project in VB

 

  • Set the following properties of the form:

Property Value

(Name) frmTest

Caption Test

AutoRedraw True

Font Courier New

 

  • Place a command button on the form and set the following properties:

Property Value

(Name) cmdTryIt

Caption Try It

 

  • Place a second command button on the form and set the following properties:

Property Value

(Name) cmdClear

Caption Clear

 

  • Place a third command button on the form and set the following properties:

Property Value

(Name) cmdExit

Caption Exit

 

  • Place the command buttons toward the bottom of the form, center them horizontally on the form and set their horizontal spacing equal. Your form should look like this:

 

 

 

  • Double-click the cmdClear command button and place the single statement Cls in the cmdClear_Click event:

 

Private Sub cmdClear_Click()

 

Cls

End Sub

 

 

  • Double-click the cmdExit command button and place the single statement End in the cmdExit_Click event:

 

Private Sub cmdExit_Click()

 

End

End Sub

 

  • Double-click the cmdTryIt command button and place the following code in the cmdTryIt_Click event:

 

Private Sub cmdTryIt_Click()

 

Dim strName As String

strName = InputBox("Enter your name:", "Input Test")

Print "Hello there, "; strName

End Sub

 

  • Run the program and click the "Try It" command button. The input box will appear. Type your name in the data entry area and click OK:

 

 

  • The output (as a result of the Print statement) will be displayed on the form:

 


 

  • Pressing "Try It" repeatedly will cause the output to be repeated on successive lines. Pressing the "Clear" button will clear all the output. Pressing the "Exit" button will exit the program and return you to the design-time environment.

 

  • If desired, save the program (both the project file and the form file) to the folder of your choice.

 

Download the VB project files for the example above here.

If you enjoyed this post, subscribe for updates (it's free)

Programming in VB 2008

I am trying to write a program using multiple forms. I also have to write a program to open text files and display the contents, and allow the program to open up at least 5 text files at once. I must also use the open and save dialog boxec to specify which files are to be opened. Can you Help? This is crunch time for me.

MsgBox

You could also use a message box

the code would look like this:

Private Sub cmdtryit_Click()
Dim strName As String
strName = InputBox("Enter Your Name:", "Input Test")
MsgBox "Hello there, " & strName
End Sub

You can also remove the "clear" command and shrink the size of the form

VB IDE

VB6 is no longer available from Microsoft, but if you go to Books-A-Million you are likely to find it included in a "Learn Visual Basic 6 in 21 Days" type of book. There are still lots of those books on shelves waiting to be bought at a pretty good discount. You could also try Amazon.Com. The version included with those books is the "Working Model Edition" which is complete in just about every respect except that you don't get the compiler. If you absolutely must have a compiled project you can search ebay for the commercial editions, but the going prices are pretty steep these days. The only free version I have found on the internet is VB5 CCE (Control Creation Edition) which, although it comes with a compiler, is suitable only for creating ActiveX controls and NOT standard exe's. Try searching at "thevbzone" to see if they still have it available.

VB IDE

Anybody here who knows where I can download VB IDE for free? The link suggested above doesn't work anymore. I need to prepare for a test and would like to try actual programming in VB6. Last time I tried this was a long time ago. I am currently using VB.Net. Please somebody help me. Thanks!!!

this app

This is supposed to be for beginners, yet it starts off with things i do not understand. Im sitting in front of visual basic express and it tells me for instance:

set the following properties for the form, how the heck do i do that without basic instrucion for a first timer, which is what this is suposed to be for, telling me how to do so....

Help

To anonymous:
I understand your problem.
You are using vb express right? You should use VB6. You can download for free over here "http://www.megaupload.com/?d=6I0ZPYWR"
In vb6, the properties are in the right side of your screen.
The other stuff just follow instructions don't worry about the code.
cls = clear
end = exit
inputbox = box where you input stuff

the properties are usually

the properties are usually on the right side of the screen it is the property box, just make sure the object you want to edit is selected and then start changing the properties. some of the commands like "Csl" means clear screen or at least that's what i perceived it to mean, "End" just ends the program, an input box is where you can place data that can either be placed into the project or that can be referenced, the second area of the code for that where is says ,"input box test" is the title for the inbut box, before that in the quotes is what you want the text within the input box to say like a question or statement. Hope that this info clears things up for you, people often don't explain stuff addiquetly enough anymore so you have to be willing to do a lot of research when you are a learning programmer or you're not gunna make it.

Not VB6

The problem is you are looking at Visual Basic Express - this is VB.NET not VB6. Sorry for the confusion - you can check out my review of VB.NET tutorials on the left navigation area.

Kazumal

Well its preety good one for beginners hope alot of people will learn for this example...
But i need a more deeper or a little harder programs or datas..

"Try it"

very simple and worked as described. Good stuff for the beginner.

Thanks for the post.

FF

Simply, a beginner's

Simply, a beginner's debugger that can be used as an extra form with a text control. Things are printed as they are executed. Test harness is an odd word.

Using End to exit from app

Why did you choose to use End to exit from your app?

its the best way to end the

its the best way to end the program. other methods like cls and exit don't end the program but simply closes the forms but the program continues running in the background

Why did you use end?

He used end because it is the fastest way to create an exit button and actually be able to exit the program. :)

yes

application useful to me

How is this a test harness?

How is this a test harness? A test harness should allow for reusable testing practices.

See http://en.wikipedia.org/wiki/Test_harness

This project doesn't seem to be anything more than a beginner-level sample project.

test harness?

That's what it was purported to be; for a beginner.

Reply to 'How is this a test harness'

Hello, I am the owner of The VB Programmer website and author of this project. Through mutual agreement, Matt is using several of my tutorials on this site. The term "test harness" is Matt's term, not mine, but basically the idea is that it is simply a "skeleton" program that can be copied over and over to test very rudimentary programming concepts (such as calculations and loops) that do not need a GUI, as when a person is first learning to program. All of these elementary exercises use InputBox to get the input data, a command button labelled "Try It" where the main code of the exercise goes, a "Clear" button, and an "Exit" button. All the output for these initial exercises use the "Print" statement to print to the form, in a manner that simulates the old days of DOS BASIC/QBASIC.