Passing Test Parameters via Command Line

Applies to TestComplete 15.46, last modified on January 09, 2023

If you have tests that take parameters, for example, input user data or the URL of your tested web application or the name of the tested application configuration, you may want to pass those parameters to your tests via command line:

  1. In your test project, create a parsing routine that will extract your custom parameters from the command line and send those parameters to your tests.

  2. To extract parameters from the command line, use the BuiltIn.ParamCount and BuiltIn.ParamsStr methods.

    • The BuiltIn.ParamCount method returns the number of command-line parameters passed to TestComplete via command line.

    • The BuiltIn.ParamStr method returns the TestComplete command-line argument by its index.

  3. When running TestComplete from the command line, run the parsing routine. You can do it, for example, by using the /unit and /routine command line parameters or create a test item that will run the parsing routine and then run that test item by running the entire project.

Note: As an alternative, you can use project and project suite variables to send parameters to your tests from the command line. See Passing Variables via Command Line.

Example

Suppose we have a test that takes the config parameter that specifies the tested application configuration: debug or release. The parameter value is passed via command line:

TestComplete.exe "C:\Work\My Projects\MySuite.pjs" /r /p:MyProj /u:Unit1 /rt:Main /config=debug

If you run your tests via the SessionCreator utility, use its command-line argument (/arg) to pass the parameter to TestComplete:

SessionCreator.exe "C:\Work\My Projects\MySuite.pjs" /r /p:MyProj /u:Unit1 /rt:Main /arg:config=debug

To pass this parameter to the test when running TestComplete from the command line, we will create the following routines:

  • The processCommandLineArguments routine will process all the parameters passed to TestComplete via command line one by one and for each of them it will call the processCommandLineArgument routine.

  • The processCommandLineArgument routine will parse each parameter to get the parameter name and the parameter value. The routine assumes that the name and the value are separated with the = character. When the routine encounters the config parameter, it will pass its value to the Test routine.

  • The Test routine takes the config parameter. Depending on the parameter value (debug or release), it performs various testing actions.

JavaScript, JScript

function Main()
{
  processCommandLineArguments();
}

function processCommandLineArguments()
{
  for (var i = 0; i < BuiltIn.ParamCount(); i++)
  {
    processCommandLineArgument(BuiltIn.ParamStr(i))
  }
}

function processCommandLineArgument(arg)
{
  var items = arg.split("=");

  if (items.length != 2)
  {
    return;
  }

  var item = aqString.ToLower(aqString.Replace(aqString.Trim(items[0]), "/", ""));
  if (item == "config")
  {
    // Run the Test routine with the parameter value
    Test(items[1]);
  }
}

function Test(config)
{
  switch (aqString.ToLower(aqString.Trim(config)))
  {
    case "debug":
      Log.Message("Running tests for the debug configuration");
      // Simulate a user action over the debug version of the application
      …
      break;
    case "release":
      Log.Message("Running tests for the release configuration");
      // Simulate a user action over the debug version of the application
      …
      break;
  }
}

Python

def Main():
  processCommandLineArguments()


def processCommandLineArguments():
  for i in range (0, BuiltIn.ParamCount()):
    processCommandLineArgument(BuiltIn.ParamStr(i))

def processCommandLineArgument(arg):
  items = arg.split("=")

  if (len(items) != 2):
    return

  item = aqString.ToLower(aqString.Replace(aqString.Trim(items[0]), "/", ""))
  if (item == "config"):
    # Run the Test routine with the parameter value 
    Test(items[1])

def Test(config):
  value = aqString.ToLower(aqString.Trim(config))
  if value == 'debug':
    Log.Message("Running tests for the debug configuration")
    # Simulate user action over the debug version of the application
    # ...
  elif value == 'release':
    Log.Message("Running tests for the release configuration");
    # Simulate user action over the debug version of the application
    # ...

VBScript

Sub Main()
  processCommandLineArguments()
End Sub

Sub processCommandLineArguments()
  For i = 0 To BuiltIn.ParamCount() - 1
    processCommandLineArgument(BuiltIn.ParamStr(i))
  Next
End Sub

Sub processCommandLineArgument(arg)
  items = Split(arg, "=")

  If UBound(items) > 1 Then
    Exit Sub
  End If

  item = aqString.ToLower(aqString.Replace(aqString.Trim(items(0)), "/", ""))
  If item = "config" Then
  ' Run the Test routine with the parameter value
    Test(items(1))
  End If
End Sub

Sub Test(config)
  Select Case (aqString.ToLower(aqString.Trim(config)))
    case "debug"
      Log.Message("Running tests for the debug configuration")
      ' Simulate a user action over the debug version of the application
      …
    case "release":
      Log.Message("Running tests for the release configuration")
      ' Simulate a user action over the debug version of the application
      …
  End Select
End Sub

DelphiScript

procedure Test(config);
begin
  case aqString.ToLower(aqString.Trim(config)) of
  'debug':
    Log.Message('Running tests for the debug configuration');
    // Simulate a user action over the debug version of the application
    …
  'release':
    Log.Message('Running tests for the release configuration');
    // Simulate a user action over the debug version of the application
  …
  end;
end;

procedure processCommandLineArgument(arg);
var item;
begin
  aqString.ListSeparator := '=';

  if (aqString.GetListLength(arg) <> 2) then
    exit;

  item := aqString.ToLower(aqString.Replace(aqString.Trim(aqString.GetListItem(arg, 0)), '/', ''));
  if item = 'config' then
    // Run the Test routine with the parameter value
    Test(aqString.GetListItem(arg, 1));
end;

procedure processCommandLineArguments();
var i;
begin
  for i := 0 to BuiltIn.ParamCount() - 1 do
    processCommandLineArgument(BuiltIn.ParamStr(i));
end;

procedure Main();
begin
  processCommandLineArguments();
end;

C#Script

function Main()
{
  processCommandLineArguments();
}

function processCommandLineArguments()
{
  for (var i = 0; i < BuiltIn["ParamCount"](); i++)
  {
    processCommandLineArgument(BuiltIn["ParamStr"](i))
  }
}

function processCommandLineArgument(arg)
{
  var items = arg["split"]("=");

  if (items["length"] != 2)
  {
    return;
  }

  var item = aqString["ToLower"](aqString["Replace"](aqString["Trim"](items[0]), "/", ""));
  if (item == "config")
  {
    // Run the Test routine with the parameter value
    Test(items[1]);
  }
}

function Test(config)
{
  switch (aqString["ToLower"](aqString["Trim"](config)))
  {
    case "debug":
      Log["Message"]("Running tests for the debug configuration");
      // Simulate a user action over the debug version of the application
      …
      break;
    case "release":
      Log["Message"]("Running tests for the release configuration");
      // Simulate a user action over the debug version of the application
      …
      break;
  }
}

See Also

TestComplete Command Line
ParamCount Method
ParamStr Method
Passing Variables via Command Line

Highlight search results