Unit tests are important, although most of the cases you don’t get paid by writing unit tests, but they are there to make sure that your application works. Recently, I have to test a COM application in a outer process fashion. Thus I decide to write some unit tests in VBScript so in this way, the VBScript will be hosted/executed via cscript.exe. The VBScript will CreateObject and test the COM application.
How do you capture the error when tests in VBScript fail? You could write to a file, but you would need to tell the hosted testing agent where to look for this log file (probably a temp file). The easiest way would be to write to console (standard output) in VBScript so the testing agent just need to capture the output before the script ends.
In VBScript, you could use msgbox to show a dialog but this is not OK for testing purposes. You could also use WScript.Echo but depending on environment, the WScript object may not be available, and also may behave differently. i.e. Using wscript.exe to run the scripts, the WScript.Echo actually pops up a dialog.
The best way is to Create the Scripting.FileSystemObject and write to its standard stream.
1 2 | Set Console = CreateObject("Scripting.FileSystemObject") Console.GetStandardStream(1).WriteLine "https://helloacm.com" |
Set Console = CreateObject("Scripting.FileSystemObject") Console.GetStandardStream(1).WriteLine "https://helloacm.com"
So, we can emulate the Assert function like this:
1 2 3 4 5 6 | Sub Assert(x, msg) If Not x Then ' alternatively, you can throw Errors using Err.Raise Console.GetStandardStream(1).WriteLine msg End If End Sub |
Sub Assert(x, msg) If Not x Then ' alternatively, you can throw Errors using Err.Raise Console.GetStandardStream(1).WriteLine msg End If End Sub
And you could run tests in VBScript like this:
1 2 3 4 | pl1 = MyCOM.GetData(ax, ay, 10)
pl2 = MyCOM.GetData(ax, ay - 25, 7)
Assert pl1 < pl2, "pl1 < pl2"
Assert Len(MyCOM.ErrorMessage & MyCOM.WarningMessage) = 0, MyCOM.ErrorMessage & MyCOM.WarningMessage |
pl1 = MyCOM.GetData(ax, ay, 10) pl2 = MyCOM.GetData(ax, ay - 25, 7) Assert pl1 < pl2, "pl1 < pl2" Assert Len(MyCOM.ErrorMessage & MyCOM.WarningMessage) = 0, MyCOM.ErrorMessage & MyCOM.WarningMessage
All you have to do is to capture all output from your VBScript and fail the tests if the output is not empty.
Alternatively, you can use Err.Raise to throw exception-like errors in VBScript, so the above could be rewritten using a much simpler method:
1 2 3 4 5 | Sub Assert(x, msg) If Not x Then Err.Raise 1, msg, msg End If End Sub |
Sub Assert(x, msg) If Not x Then Err.Raise 1, msg, msg End If End Sub
However, if you have the following statement:
1 | On Error Resume Next |
On Error Resume Next
Then all exceptions will be caught and ignored unless you manually test for the Error Object, like this:
1 2 3 4 | Assert False, "Generated Error" If Err.Source <> "" Then Msgbox "Error: " & Err.Source End If |
Assert False, "Generated Error" If Err.Source <> "" Then Msgbox "Error: " & Err.Source End If
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: How to Find Intersection of Two Arrays in C++?
Next Post: The USB Flash Benchmark Tool - FlashBench