Jump to content
Sign in to follow this  
bear

A very simple Test library

Recommended Posts

bear

Hi,

I made a very simple Test library for my project with AutoIt.

It may useful or useless for you, but I think I must feedback something to this great project.

Here is my code:

; Simple Test Case framework implementation.

#include "Array.au3"

#cs
@name Global variable section
#ce
; @{
Const $TEST_ERROR_CODE_PASS = 0
Const $TEST_ERROR_CODE_UNABLE_EVAL = 1
Const $TEST_ERROR_CODE_FAIL = 2

Local $__testCase = 0
Local $__tearDownFunc = 0
Local $__assertNum = 0
Local $__passNum = 0
Local $__failNum = 0
; @}

#cs
Start a test case and setup it.
@param testCaseName Test case name.
@param setupFunc Setup callback function.
@param tearDownFunc Tear down callback function.
#ce
Func StartTestCase($testCaseName, $setupFunc = "", $tearDownFunc = "")
 
 $__testCase = FileOpen($testCaseName & ".txt", 2)
 If $__testCase = -1 Then
  MsgBox(0 + 16, "Error", "Unable to open TEST CASE result file.")
  Exit
 EndIf 
 
 FileWriteLine($__testCase, "TEST CASE => " & $testCaseName)
 
 If $setupFunc <> "" Then
  Call($setupFunc)
 EndIf
 
 If $tearDownFunc <> "" Then
  $__tearDownFunc = $tearDownFunc
 EndIf
 
EndFunc

#cs
Assert.
@param actualExp Actual result expression, must be a valid script string
                 which returns boolean after evaluate it.
@param expectExp Expect result expression, must be a valid script string
                 which returns boolean after evaluate it.
#ce
Func Assert($actualExp, $expectExp)
 __Assert($actualExp, $expectExp, False)
EndFunc

#cs
Assert required.
@param actualExp Actual result expression, must be a valid script string
                 which returns boolean after evaluate it.
@param expectExp Expect result expression, must be a valid script string
                 which returns boolean after evaluate it.
#ce
Func AssertRequired($actualExp, $expectExp)
 __Assert($actualExp, $expectExp, True)
EndFunc

#cs
Assert implementation.
@param actualExp Actual result expression, must be a valid script string
                 which returns boolean after evaluate it.
@param expectExp Expect result expression, must be a valid script string
                 which returns boolean after evaluate it.
@param exitOnFailed Exit program while test failed.
#ce
Func __Assert($actualExp, $expectExp, $exitOnFailed = True)
 Local $result
 Local $unableEval = False
 Local $expression = $actualExp & " == " & $expectExp
 
 $__assertNum = $__assertNum + 1
 
 Local $lhs = Execute($actualExp)
 If @error <> 0 Then
  $unableEval = True
 EndIf
 
 Local $rhs = Execute($expectExp)
 If @error <> 0 Then
  $unableEval = True
 EndIf
 
 If $lhs = $rhs Then
  $result = True
 Else
  $result = False
 EndIf
 
 If $unableEval = True Then
  $__failNum = $__failNum + 1
  FileWriteLine($__testCase, _
   $TEST_ERROR_CODE_UNABLE_EVAL & ": Unable to evaluate expression => " & $expression)
  If $exitOnFailed = True Then
   FinishTestCase()
  EndIf
 ElseIf $result = False Then
  $__failNum = $__failNum + 1
  FileWriteLine($__testCase, _
   $TEST_ERROR_CODE_FAIL & ": Test failed => " & $expression)
  If $exitOnFailed = True Then
   FinishTestCase()
  EndIf
 Else 
  $__passNum = $__passNum + 1
  FileWriteLine($__testCase, _
   $TEST_ERROR_CODE_PASS & ": Test passed => " & $expression)
 EndIf
  
 Return 
EndFunc

#cs
Finish test case and tear down.
#ce
Func FinishTestCase()
 FileWriteLine($__testCase, "ASSERT: " & $__assertNum)
 FileWriteLine($__testCase, "PASS: " & $__passNum)
 FileWriteLine($__testCase, "FAILED: " & $__failNum)
 
 If $__tearDownFunc <> "" Then
  Call($__tearDownFunc)
 EndIf
 
 FileWriteLine($__testCase, "FINISH TEST CASE")
 FileClose($__testCase)
 
 Exit
EndFuncoÝ÷ ØLZ^jëh×6#include "TestCase.au3"

Func Setup()
 MsgBox(0, "Test Case", "Setup => setup your application here.")
EndFunc

Func TearDown()
 MsgBox(0, "Test Case", "TearDown => release some resource here.")
EndFunc

StartTestCase("TestExample", "Setup", "TearDown")

AssertRequired("1 + 1", "2") ; 1 + 1 = 2 ?

Run("notepad")
Sleep(500)

Assert("WinActive('Untitled -')", "1")

Send("!{F4}")
Sleep(500)

Assert("WinActive('Untitled -')", "0")

FinishTestCase()

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×