Search code examples
scriptingautomated-testsautoit

Can I make _Assert write to console instead of a message box?


I am writing a script in AutoIt to test a windows application, and I am using the _Assert function to verify certain actions.

In the documentation I found there is a parameter to say whether or not the script should end if an assertion fails, which is great because in some cases I would like the script to continue, but unfortunately it is still halted by a message box.

Can I override the _Assert function somehow to only print to the console when certain assertions fail, so the script can continue without user interaction?


Solution

  • Desired behavior can not be achieved using _Assert(). However, _Assert() can be adjusted to do so (replaced MsgBox() by ConsoleWrite()):

    Func _AssertCustom($sCondition, $bExit = True, $nCode = 0x7FFFFFFF, $sLine = @ScriptLineNumber, Const $iCurERR = @error, Const $iCurEXT = @extended)
        Local $bCondition = Execute($sCondition)
        If Not $bCondition Then
            ConsoleWrite("Assertion Failed (Line " & $sLine & "): " & $sCondition & @CRLF)
            If $bExit Then Exit $nCode
        EndIf
        Return SetError($iCurERR, $iCurEXT, $bCondition)
    EndFunc
    

    Best to declare as new function (not to change Debug.au3).