Framework for unit tests

Scripts for common tasks

Framework for unit tests

Postby Mike on Wed May 05, 2010 2:45 pm

We had a question about the best way to have a script going on with the execution of the next unit test if the previous one has failed.

In this simple example, we explain how it can be done.
Main.src
Code: Select all
'We use Run function to execute independantly the different testcase scripts.
'This function runs the unit test as specified in scriptname$ (so one script name for one unit test)
'and logs the results in the logfile specified in logfile$
function run_case(scriptname$)
run(scriptname$+" "+chr$(34)+logfile$+chr$(34))
endfunction

'Create the log file
logfile$="D:\test\testnotepad"+Year$()+month$()+day$()+hour$()+min$()+".log"
create(logfile$)

'Launch the different unit cases
'IMPORTANT : as we run here the .ROB, any modification in the testcase scripts (src) must be saved and COMPILED before running again main.src
run_case("testcase1")
run_case("testcase2")


This script contains all the functions used by the different unit tests, testcase1.src, testcase2.src
The script below just gives some examples of such functions.
Test_functions.src
Code: Select all
Function check_text(Check_name$,window_name$,text$)
local ret, a$, pos

a$=capture$(window_name$,0,1)
pos = instr(a$, text$)
if pos > 0 then
   Comment("Check : "+Check_name$+" OK")
else
   Comment("Check : "+Check_name$+" KO")
endif

EndFunction
'--------------------------------------
Function check_window(Check_name$,window_name$)
local ret

ret=existW(window_name$)
if ret=1 then
   Comment("Check : "+Check_name$+" OK")
else
   Comment("Check : "+Check_name$+" KO")
endif
endfunction
'--------------------------------------
Function check_box(Check_name$, window_name$, etat$)
local ret

ret=CheckedW(name$)
if etat$="checked" then
   if ret=1 then
      comment("Check : "+check_name$+" OK")
   else
      comment("Check : "+check_name$+" KO")
   endif
else
   if ret=0 then
      comment("Check : "+check_name$+" OK")
   else
      comment("Check : "+check_name$+" KO")
   endif
endif
EndFunction
'--------------------------------------
function exist_item(Check_name$,nom_liste$, nom_item$)
local ret, i, a$
repeat
   a$=listitem$(nom_liste$, i)
   if a$="Italique" then
      ret=1
   else
      i=i+1
   endif
until ret=1 or a$=""   
if ret=0 then
   Comment("Check : "+Check_name$+" KO")
else
   Comment("Check : "+Check_name$+" OK")
endif

EndFunction

The test cases scripts:
Testcase1.src
Code: Select all
'Include the functions valid for any script.
Include "test_functions.src"

'**********************************************************************
'   OnAction Error definition - it applies only for this script
'**********************************************************************
Sub errorproc()
'If an error occurs, this proc is launched and it returns one level up so in main script.
'kill the application that this script has launched, so the desktop is clean for the next test case script to run
KillApp("notepad.exe",1)
'write in the log a comment
comment("Error in script: "+#ErrorScript$)
EndSub

OnAction Error
   DoSub errorproc
EndAction

'Retrieve the log filename from the call in main.src
logfile$=command$(1)

'Log initialization
logfile(logfile$, 0, 0)
'write a sentence in the log
comment("FIND Test")


'launch the application
shell("Notepad")

'Uncomment one of those 2 lines below for a case which fails or which is successful
'UseWindow("NOTEPAD.EXE|Edit|Untitled - Notepad|1",1)
'UseWindow("NOTEPAD.EXE|Bla|Untitled - Notepad|1",1)
   SendKeys("bla bla<Enter>")


'Call one of the test functions which are in test_functions.src
check_text("Check if the text typed is there", "NOTEPAD.EXE|Edit|Untitled - Notepad|1", "bla bla")


'Close the application
KillApp("notepad.exe", 1)


Testcase2 script
Code: Select all
'Include the functions valid for any script.
Include "test_functions.src"

'**********************************************************************
'   OnAction Error definition - it applies only for this script
'**********************************************************************
Sub errorproc()
'I kill the application that this script has launched, so the desktop is clean for the next test case script to run
KillApp("notepad.exe",1)
'I write in the log a comment
comment("Error in script: "+#ErrorScript$)
EndSub

OnAction Error
   DoSub errorproc
EndAction

'Retrieve the log filename from the call in main.src
logfile$=command$(1)

'Log initialization
logfile(logfile$, 0, 0)
'write a sentence in the log
comment("About Test")

'launch the application
shell("Notepad")

'Call the Help/About menu in Notepad
UseWindow("NOTEPAD.EXE|Notepad|Untitled - Notepad",1)
   ChooseMenu(Normal,"&Help|&About Notepad")

'Is the window named "NOTEPAD.EXE|#32770|About Notepad" there
check_window("Is About window there: ","NOTEPAD.EXE|#32770|About Notepad")


'Close the application
KillApp("notepad.exe", 1)

Mike
 
Posts: 5
Joined: Fri Dec 04, 2009 3:24 pm

Return to Sample Scripts and Tips

Who is online

Users browsing this forum: No registered users and 1 guest