Code: Select all
'
' AUNIT
'
' Unit testing framework for MS Access
'
' Matt Kubilus 2005
'
' This code comes with no warranty or gaurantees.
' Anyone may use or modify this code.
'
Option Compare Database
Global badtests As Integer
Global alltests As Integer
Global startTime As Date
Global endTime As Date
Public Function startTest()
badtests = 0
alltests = 0
Debug.Print
Debug.Print
Debug.Print "-------------------------------"
Debug.Print "Testing " & Date & " " & Time
Debug.Print
startTime = Time
End Function
Public Function stopTest()
endTime = Time
Debug.Print
Debug.Print "Elapsed Time: " & _
Format((endTime - startTime), "hh:mm:ss")
Debug.Print
If badtests = 0 Then
Debug.Print ("OK, All " & alltests & " tests passed.")
Else
Debug.Print ("FAIL, " & badtests & _
" tests failed out of " & alltests & " tests.")
End If
End Function
Public Sub assert(assertion As Boolean, caller As String, message As String)
Dim output As String
output = "Testing " & caller
alltests = alltests + 1
If assertion = False Then
output = output + " . . . FAIL " & message
badtests = badtests + 1
Else
output = output + " . . . OK"
End If
Debug.Print (output)
End Sub
Now save this into a module called aunit, or whatever else you want. Now we can create some tests like so:
Option Compare Database
Global foo As Integer
Global bar As Integer
Public Sub testFoo()
aunit.assert foo = 5, "testAunit.testFoo", "Foo != 5"
End Sub
Public Sub testBar()
aunit.assert bar = 9, "testAunit.testBar", "Bar != 9"
End Sub
Public Sub testAll()
foo = 5
bar = 9
startTest
testFoo
testBar
stopTest
End Sub
Open the immediates window in access to see the output and run testAll. This is the output I get:
-------------------------------
Testing 8/19/2005 2:36:14 PM
Testing testAunit.testFoo . . . OK
Testing testAunit.testBar . . . OK
Elapsed Time: 00:00:00
OK, All 2 tests passed.

