Anybody using Test Driven Development with Purebasic

For everything that's not in any way related to PureBasic. General chat etc...
User avatar
AndrewM
User
User
Posts: 39
Joined: Wed Nov 06, 2013 3:55 am
Location: Australia

Re: Anybody using Test Driven Development with Purebasic

Post by AndrewM »

I found a unit testing framework that I like. The code is VBA but it would be easy to implement in PureBasic - only about 50 lines of it and it is procedural and tests procedural code. The original code is from http://monkeyfungus.blogspot.com.au/200 ... ccess.html. I will copy the code here just in case the original disappears.

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.
PureBasic does not have an immediate window and even in ms-access outputting test results into the immediate window is sub-optimal. I adjusted the code to save the output to a html file, which I view in my web browser.
User avatar
idle
Always Here
Always Here
Posts: 6027
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Anybody using Test Driven Development with Purebasic

Post by idle »

PB has a unit testing framework, look in the SDK folder.
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
AndrewM
User
User
Posts: 39
Joined: Wed Nov 06, 2013 3:55 am
Location: Australia

Re: Anybody using Test Driven Development with Purebasic

Post by AndrewM »

Thanks for that Idle. The unit testing framework looks really good. I would never have found it without your pointer. 8)
Post Reply