Stopping Tests on Timeout

Applies to TestComplete 12.60, last modified on September 17, 2018

You can set a timeout for automated tests to make sure the tests do not take longer than they should. TestComplete provides several ways to specify a test timeout:

Using a Timeout for Test Items

Test items define the test run order when you run the entire project or project suite. You can configure test items and their timeouts on the Test Items page of the project editor and project suite editor.

When a test item’s timeout elapses, TestComplete logs an error and raises the OnTimeout event. To stop the current test or all the tests, create a handler for this event.

Use one of the following event handler examples depending on what you want to do.

Note: Disable the project’s Stop on Error option. Otherwise, the test engine will stop the entire project running upon the timeout error and will not execute the instructions you specify in the OnTimeout event handler.
To stop the entire project (or project suite)

JavaScript, JScript

function GeneralEvents_OnTimeout(Sender, Params)
{
  Runner.Stop();
}

Python

def GeneralEvents_OnTimeout(Sender, Params):
  Runner.Stop()

VBScript

Sub GeneralEvents_OnTimeout(Sender, Params)
  Runner.Stop
End Sub

DelphiScript

procedure GeneralEvents_OnTimeout(Sender; Params);
begin
  Runner.Stop;
end;

C++Script, C#Script

function GeneralEvents_OnTimeout(Sender, Params)
{
  Runner["Stop"]();
}

To stop the current test item without stopping the entire project (or project suite)

JavaScript, JScript

function GeneralEvents_OnTimeout(Sender, Params)
{
  Runner.Stop(true);
}

Python

def GeneralEvents_OnTimeout(Sender, Params):
  Runner.Stop(True)

VBScript

Sub GeneralEvents_OnTimeout(Sender, Params)
  Runner.Stop True
End Sub

DelphiScript

procedure GeneralEvents_OnTimeout(Sender; Params);
begin
  Runner.Stop(true);
end;

C++Script, C#Script

function GeneralEvents_OnTimeout(Sender, Params)
{
  Runner["Stop"](true);
}

To wait for current test iteration to end, skip further iterations and move on to the next test item

JavaScript, JScript

function GeneralEvents_OnTimeout(Sender, Params)
{
  Params.Break = true;
}

Python

def GeneralEvents_OnTimeout(Sender, Params):
  Params.Break = True;

VBScript

Sub GeneralEvents_OnTimeout(Sender, Params)
  Params.Break = True
End Sub

DelphiScript

procedure GeneralEvents_OnTimeout(Sender; Params);
begin
  Params.Break := true;
end;

C++Script, C#Script

function GeneralEvents_OnTimeout(Sender, Params)
{
  Params["Break"] = true;
}

TestComplete ignores the test item timeout in the following cases:

  • The test is run not as a test item. For example, from the Project Explorer.

  • The test item has a timeout of 0.

  • The test project does not have the OnTimeout event handler.

  • The OnTimeout event handler sets Params.Break to False.

Using a Timeout Timer

When you run automated tests individually (for example, from the Project Explorer as opposed to running the entire test project), you can use a timer to stop the test on timeout. A timer runs some code at regular time intervals. You can set the timer interval equal to the test timeout and call the Runner.Stop method from the timer’s code to stop the test.

JavaScript, JScript

// Unit1

function Main()
{
  // Create a 30-second timeout timer
  Utils.Timers.Add(30000, "Unit1.TimeoutTimer", true);

  // This loop will be stopped after about the 30th iteration
  for (var i = 1; i <= 100; i++)
  {
    Log.Message(i);
    Delay(1000);
  }
}
 
function TimeoutTimer()
{
  Log.Message("The test timeout has been reached... Stopping the test.");
  Runner.Stop();
}

Python

# Unit1

def Main():
  # Create a 30-second timeout timer
  Utils.Timers.Add(30000, "Unit1.TimeoutTimer", True)

  # This loop will be stopped after about the 30th iteration
  for i in (1, 100):
    Log.Message(i)
    Delay(1000)

def TimeoutTimer():
  Log.Message("The test timeout has been reached... Stopping the test.")
  Runner.Stop()

VBScript

Sub Main
  ' Create a 30-second timeout timer
  Utils.Timers.Add 30000, "Unit1.TimeoutTimer", true

  ' This loop will be stopped after about the 30th iteration
  Dim i
  For i = 1 To 100
    Log.Message i
    Delay 1000
  Next
End Sub
 
Sub TimeoutTimer
  Log.Message "The test timeout has been reached... Stopping the test."
  Runner.Stop
End Sub

DelphiScript

procedure Main;
var i;
begin
  // Create a 30-second timeout timer
  Utils.Timers.Add(30000, 'Unit1.TimeoutTimer', true);

  // This loop will be stopped after about the 30th iteration
  for i := 1 to 100 do
  begin
    Log.Message(i);
    Delay(1000);
  end;
end;
 
procedure TimeoutTimer;
begin
  Log.Message('The test timeout has been reached... Stopping the test.');
  Runner.Stop;
end;

C++Script, C#Script

// Unit1

function Main()
{
  // Create a 30-second timeout timer
  Utils["Timers"]["Add"](30000, "Unit1.TimeoutTimer", true);

  // This loop will be stopped after about the 30th iteration
  for (var i = 1; i <= 100; i++)
  {
    Log["Message"](i);
    Delay(1000);
  }
}
 
function TimeoutTimer()
{
  Log["Message"]("The test timeout has been reached... Stopping the test.");
  Runner["Stop"]();
}

Specifying a Timeout in the Command Line

If you run automated tests from the command line, you can use the /Timeout argument to specify a timeout (in seconds) for your test. This timeout should account for both the test run time and the TestComplete startup time.

TestComplete.exe "C:\Tests\Test.pjs" /r /e /Timeout:600

Possible timeout values are 30 .. 4 294 967 seconds (49.7 days).

See Also

OnTimeout Event
Test Items Page (Project Editor)
Test Items Page (Project Suite Editor)
Timeout Property

Highlight search results