Rebooting a Remote Test Environment From a Test Script

Rebooting a Remote Test Environment From a Test Script

Sometimes we can’t launch our application from our test case, which is often the case if we perform tests on embedded devices, where the AUT is launched during device startup.

In such cases, we simply attach to the AUT at the beginning of each test. At the same time, we want to keep our test cases independent, so bringing back the AUT to the initial state is part of the cleanup of the previous test case.

Unfortunately, if something unexpected happens during execution, our AUT might get stuck in an unknown state or become frozen. The only way to bring it back to the desired point may be to restart the device.

In the following Python example, we’re going to show you the example implementation of this approach.

The rebooting will happen in the init() function. This way we are independent of the previously executed test outcome.
We will use the paramiko Python module to establish an SSH connection and execute a reboot command on the device. The paramiko module is not part of the Squish Python package, but can be easily installed with pip.

  1. Define the init() function that is called automatically before a test case main() execution:
def init():
	test.startSection("Test initialization")
	if is_initial():
		test.log("AUT is in the initial state. Continue test execution.")
		test.log("AUT is not in the initial state. Reboot test environment")

2. Define is_initial() function that checks if the AUT is in the initial state:

def is_initial(timeout_s=20):
    """ custom code that checks if your application is in the initial state """
        waitForObject(names.initial_screen, timeout_s*1000)
        return True
        return False

3. Define thereboot() function that connects to the embedded device over SSH and sends a reboot command:

import paramiko

def reboot():
    ssh = paramiko.SSHClient()
    ssh.connect(hostname=DEVICE_IP, username=DEVICE_USERNAME, password=DEVICE_PASSWORD)
    ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("sudo shutdown -r now")
    if > 0:
        raise RuntimeError("AUT reboot failed")

4. Define the waitForAUT() function that waits for the device to be rebooted and that the AUT is available for attaching again:

from datetime import datetime

def waitForAut(timeout_s=20):
    start =        
    while True:
        except RuntimeError as e:
            if ( - start).total_seconds() > timeout_s:
                raise RuntimeError("Can't attach to the AUT")

Please note that you can use a global cleanup() and init() to implement this solution.

0 Kommentare

Eine Antwort hinterlassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.