Passing Arguments To a Script

You can pass arguments to a script. For example, you might want to pass in the number of iterations to perform or the name of a data file. All functions and test cases in the script have access to the arguments.

How to pass arguments to a script

All arguments are passed in as strings, separated by spaces, such as: Bob Emily Craig

If an argument is more than one word, enclose it with quotation marks. For example, the following passes in three arguments: "Bob H" "Emily M" "Craig J"

You can pass arguments to a script using the following methods:

  • Specify the arguments in the Arguments field in the Runtime Options dialog box. To open the dialog box, click Options > Runtime in the menu bar.
  • The Arguments field in the Run Test Case dialog box is used to pass arguments to a test case, not to an entire script.
  • Specify the arguments in a suite file after a script name, for example find.t arg1 arg2.
  • Provide arguments when you invoke Silk Test Classic from the command line.
  • If you pass arguments in the command line, the arguments provided in the command line are used and any arguments specified in the currently loaded options set are not used. To use the arguments in the currently loaded options set, do not specify arguments in the command line.

Processing arguments passed into a test script

You use the GetArgs function to process arguments passed into a script. GetArgs returns a list of strings with each string being one of the passed arguments. Any test case or function in a script can call GetArgs to access the arguments.

Example: Passed arguments

The following test case prints a list of all the passed arguments:

testcase ProcessArgs ( )
LIST OF STRING lsArgs 
lsArgs = GetArgs ( ) 
ListPrint (lsArgs) 

//You can also process the arguments individually. The following test case prints the second argument passed:
testcase ProcessSecondArg ( )
LIST OF STRING lsArgs 
lsArgs = GetArgs ( ) 
Print (lsArgs[2]) 

//The following testcase adds the first two arguments:
testcase AddArgs ()
LIST OF STRING lsArgs 
lsArgs = GetArgs ( ) 
NUMBER nArgSum 
 
nArgSum = Val (lsArgs[1]) + Val (lsArgs[2]) 
Print (nArgSum) 

You can use the Val function to convert the arguments, which are always passed as strings, into numbers.

When the arguments script 10 20 30 are passed to the scr_args.t script, the test result is:

Script scr_args.t (10, 20, 30) - Passed
Passed:  1 test (100%)
Failed:  0 tests (0%)
Totals:  1 test, 0 errors, 0 warnings

Testcase AddArgs - Passed 

30