Setting and Checking a Keyword Test Result

Applies to TestComplete 15.45, last modified on December 01, 2022

You can return values from keyword tests to indicate the test result. These can be numbers, strings, objects, Boolean values and so on. If you call a keyword test from another test or script, you can check the result of the called test and implement some conditional logic depending on the test result.

You can specify the test result in several ways:

Use the Return operation

To return a value from a keyword test, you can use the Return operation. You typically add this operation at the end of the test, because it immediately exits the test. Tests without the Return operation have no return value.

To check a keyword test’s return value in the caller test, use the following approaches:

  • In you run the test from another keyword test, the return value of the called test is saved to the Last Operation Result value right after the test call. You can check this value using the If ... Then operation or save it to a variable. For more information, see Checking Operation Result.

  • If you run the test from a script using the KeywordTests.TestName.Run method, the method’s return value is the test’s return value. You can check this value using the if…then statement or save it to a script variable.

    For example, if a keyword test named Test1 returns True or False, you run the test and check its return value like this:

    JavaScript, JScript

    if (KeywordTests.Test1.Run())
      Log.Message("Test returned True");
    else
      Log.Message("Test returned False");

    Python

    if KeywordTests.Test1.Run():
      Log.Message("Test returned True")
    else:
      Log.Message("Test returned False")

    VBScript

    If KeywordTests.Test1.Run Then
      Log.Message "Test returned True"
    Else
      Log.Message "Test returned False"
    End If

    DelphiScript

    if KeywordTests.Test1.Run then
      Log.Message('Test returned True')
    else
      Log.Message('Test returned False');

    C++Script, C#Script

    if (KeywordTests["Test1"]["Run"]())
      Log["Message"]("Test returned True");
    else
      Log["Message"]("Test returned False");

Return values using project and project suite variables

The Return operation lets you return only one value from a keyword test. If you need to return two or more values, you can save them to project or project suite variables. These variables keep their values throughout the project (or project suite) run and let you exchange data between tests.

To assign a value to a project (or project suite) variable in keyword tests, use the Set Variable Value operation.

To get the variable value in other tests, you can use the Project.Variables.VariableName or ProjectSuite.Variables.VariableName syntax.

For example, suppose you have a boolean project variable named Result, and the Test1 keyword test writes True or False to this variable. If you run the Test1 test from a script, you can check the Result value like this:

JavaScript, JScript

function Main()
{
  KeywordTests.Test1.Run();

  if (Project.Variables.Result)
    Log.Message("Result is True");
  else
    Log.Message("Result is False");
}

Python

def Main():
  KeywordTests.Test1.Run()
  if Project.Variables.Result:
    Log.Message("Result is True")
  else:
    Log.Message("Result is False")
  

VBScript

Sub Main
  KeywordTests.Test1.Run

  If Project.Variables.Result Then
    Log.Message "Result is True"
  Else
    Log.Message "Result is False"
  End If
End Sub

DelphiScript

procedure Main;
begin
  KeywordTests.KeywordTest1.Run;

  if Project.Variables.Result then
    Log.Message('Result is True')
  else
    Log.Message('Result is False');
end;

C++Script, C#Script

function Main()
{
  KeywordTests["Test1"]["Run"]();

  if (Project["Variables"]["Result"])
    Log["Message"]("Result is True");
  else
    Log["Message"]("Result is False");
}

See Also

Keyword Tests
Keyword Tests
Running Keyword Tests
Keyword Test Variables

Highlight search results