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.
Arguments in
4Test are declared in the following two ways:
- Explicitly, as a list of arguments for a test case. Only the test case has access to an explicit argument, not the entire
script. For example:
testcase MyTest1(STRING arg1, STRING arg2)
Print("{arg1} {arg2}")
- Implicitly, by using the
GetArgs method inside a function or test case.
GetArgs returns a list of strings with each string being one of the passed arguments. All functions and test cases in the script
have access to these implicit arguments by calling
GetArgs. For example:
testcase MyTest2()
LIST OF STRING args = GetArgs()
ListPrint(args)
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 specify arguments explicitly in one of the following ways:
- In the
Arguments field in the
Run Test Case dialog box.
- In
Silk Central. Select the
Properties tab of a
Silk Test Classic test and specify the arguments by adding the
Test data property with the arguments to the
Test Properties section.
- When you invoke
Silk Test Classic from the command line.
You can specify arguments implicitly in one of the following ways:
- In the
Arguments field in the
Runtime Options dialog box. To open the dialog box, click
in the menu bar.
- After a script name in a suite file, for example
find.t arg1 arg2.
- When you invoke
Silk Test Classic from the command line.
Note: 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.
Example: Implicitly passed arguments
The following test case prints a list of all the implicitly 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