How can I count the number of testcases in a Testplan using SilkTest?
Treat the Testplan as an external text file and use SilkTest to search for a substring target within each line. This piece of code can easily be adapted to search for other specific items within any text file.
To use this piece of code to count the number of testcases in a Testplan, you will need to edit the information within square brackets before running it, here is an example of what you will need to change;
[ ] sFileName="C:\Program Files\Segue\SilkTest\Projects\Test1\Test1.pln" [ ] sSubString="testcase:"
Note that for the substring we are using the word testcase followed by a colon ":", this means that it will only search for an instance of testcase as a Testplan Keyword.
[-] testcase CountAllTestcases() appstate none [ ] //Name the variables [ ] HFILE hFile //Handle to the file [ ] STRING sFileName //Filename and path [ ] STRING sLine //Any line within the file [ ] STRING sSubString //Substring to search for within the line [ ] FILEMODE fmMode //FileOpen mode [ ] FILESHARE fShare //Sharing behaviour [ ] INTEGER iExist //If greater than zero then the target is found [ ] INTEGER iTotal //Total number of substring instances [ ] [ ] //Set the constants [ ] sFileName="[path and filename]" [ ] sSubString="[information to search for]" [ ] fmMode=FM_READ //Make the file read-only [ ] fShare=FS_EXCLUSIVE //Deny access to anyone else [ ] iTotal=0 [ ] iExist=0 [ ] [ ] //Loop through the target file searching for instances of the substring [ ] hFile=FileOpen(sFileName,fmMode,fShare) [-] while (FileReadLine (hFile,sLine)) [ ] iExist=0 [ ] iExist=StrPos(sSubString,sLine) [-] if iExist>0 [ ] iTotal++ [ ] FileClose(hFile) [ ] print("The total number of [search targets] is: "+ str( iTotal))
If you cannot be certain that the substring target you are searching for will be unique within a line, then you may modify the search loop to look for two or more substrings as follows:-
[ ] //Modified variant to search for two or more unique items within a line [ ] //Loop through the target file searching for instances of the substring [ ] //When it is found, search the line again for the second substring target [ ] //Nest as many inner checks as needed to ensure the correct target is hit [ ] hFile=FileOpen(sFileName,fmMode,fShare) [-] while (FileReadLine (hFile,sLine)) [ ] iExist=0 [ ] iExist=StrPos(sSubString1,sLine) [-] if iExist>0 [ ] iExist=StrPos(sSubString2,sLine) [-] if iExist>0 [ ] iTotal++ [ ] FileClose(hFile) [ ] print("The total number of [search targets] is: "+ str( iTotal))