Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE

In SilkTest, what is the recommended 4Test syntax for an "if" statement with multiple conditions ?

In SilkTest, what is the recommended 4Test syntax for an "if" statement with multiple conditions ?

If you are trying to test multiple conditions, then you should use a "select" or "switch" block. You could use nested "if..else" statements, but if you have more than two or three conditions, the levels of indentation will become cumbersome.

You should not use an "if..else if..else" block. Although "if..else if..else" will work, it will be difficult to troubleshoot exceptions that occur because the results file will always point to the first "if" statement even if it was actually a subsequent "if" statement that raised the exception.

For example, in the following testcase, the third string, "Not a date", will raise the exception:
*** Error: Incompatible types -- "Not a date" is not a valid date
The exception actually occurs in the lines containing:
GetDateTimePart ([DATETIME]sVal, DTP_YEAR) == 2002

For the nested "if..else" and the "select" blocks, the results file points to those lines as the sources of the exceptions. However, for the "if..else if..else" block, the results file points to the first "if" statement, in other words to the line:
[-] if IsNull (sVal)
even though that line clearly is not the source of the exception because it does not concern DATETIME values.

  [+] testcase IfElseIfElse ()
        [-] LIST OF STRING lsVals = {...}
            [ ] "2002-05-20"
            [ ] "2002-11-07"
            [ ] "Not a date"
        [ ] STRING sVal
        [ ] 
        [-] for each sVal in lsVals
            [-] do
                [-] if IsNull (sVal)
                    [ ] Print ("No date given")
                [-] else if sVal == FormatDateTime (GetDateTime (), "yyyy-mm-dd")
                    [ ] Print ("The date is today")
                [-] else if GetDateTimePart ([DATETIME]sVal, DTP_YEAR) == 2002
                    [ ] Print ("The year is this year")
                [-] else
                    [ ] Print ("Some other year")
            [-] except
                [ ] ExceptLog ()
            [ ] 
            [-] do
                [-] if IsNull (sVal)
                    [ ] Print ("No date given")
                [-] else
                    [-] if sVal == FormatDateTime (GetDateTime (), "yyyy-mm-dd")
                        [ ] Print ("The date is today")
                    [-] else
                        [-] if GetDateTimePart ([DATETIME]sVal, DTP_YEAR) == 2002
                            [ ] Print ("The year is this year")
                        [-] else
                            [ ] Print ("Some other year")
            [-] except
                [ ] ExceptLog ()
            [ ] 
            [-] do
                [-] select
                    [-] case IsNull (sVal)
                        [ ] Print ("No date given")
                    [-] case sVal == FormatDateTime (GetDateTime (), "yyyy-mm-dd")
                        [ ] Print ("The date is today")
                    [-] case GetDateTimePart ([DATETIME]sVal, DTP_YEAR) == 2002
                        [ ] Print ("The year is this year")
                    [-] default
                        [ ] Print ("Some other year")
            [-] except
                [ ] ExceptLog ()
        [ ] 

Old KB# 22307

DISCLAIMER:

Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
Top Contributors
Version history
Revision #:
1 of 1
Last update:
‎2013-02-15 19:30
Updated by:
 
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.