Configuration

Explore how to manage test settings with OpenHTF's configuration module using in-code definitions or external configuration files.

Last updated · Verified with OpenHTF 1.6.1

Control test execution parameters with configuration variables or files.

A configuration key is declared in code with a default value. A YAML file loaded at startup overrides the default, and a command-line flag overrides the file. Phases read the final value through the conf module.

  • conf.declare("voltage_limit")default_value=5.05.0
  • conf.load_from_filename(...)config.yaml: voltage_limit: 5.5
  • --config-value voltage_limit=6.0command line
  • phase_voltageconf.voltage_limit

Sometimes you need to change script parameters without modifying the script itself, like updating firmware versions or adjusting instrument ports between stations. OpenHTF simplifies this by using configuration files to manage these parameters.

File

You can load configurations from an external file written in YAML or JSON format. Passing --config-file config.yaml on the command line does the same without code (see CLI flags). For example:

config.yaml
firmware_path: 'path/to/firmware_v1.0.0.bin'

Load the configuration file during execution script:

main.py
import openhtf as htf
from openhtf.util import configuration

# Declare the firmware_path key
configuration.CONF.declare("firmware_path")

# Load configuration from an external YAML file
with open("config.yaml", "r") as yamlfile:
    configuration.CONF.load_from_file(yamlfile)

def flash_firmware(test):
    firmware_path = configuration.CONF.firmware_path
    print(f"Flashing firmware from: {firmware_path}")

def main():
    test = htf.Test(flash_firmware)
    test.execute(test_start=lambda: "SN1234")

if __name__ == "__main__":
    main()

Built-in keys

OpenHTF declares a few keys itself. Set them in the same file or with --config-value key=value:

station_idstr

Name of the test station written to every record. Default: the machine's hostname.

station_server_portint

Port for the Operator UI. Default 0 picks a free port.

stop_on_first_failurebool

Same switch as test.configure(stop_on_first_failure=True) — see Test Options.

allow_unset_measurementsbool

If True, a declared measurement that is never set does not fail the phase. Default False.

capture_sourcebool

Store the source code of phases in the record's code_info. Default False.

frontend_throttle_sfloat

Minimum seconds between Operator UI updates. Default 0.15.

station_discovery_address / _port / _ttl

Multicast settings for the multi-station dashboard.

Run any test with --config-help to print every declared key with its description and default.

Advanced use cases

Variables

You can set configuration variables directly in the script.

main.py
import openhtf as htf
from openhtf.util import configuration

# Define firmware_path configuration
configuration.CONF.declare(
    "firmware_path",
    default_value="path/to/firmware_v1.0.0.bin",
    description="Path to the firmware file",
)

def flash_firmware(test):
    firmware_path = configuration.CONF.firmware_path
    print(f"Flashing firmware from: {firmware_path}")

def main():
    test = htf.Test(flash_firmware)
    test.execute(test_start=lambda: "SN1234")

if __name__ == "__main__":
    main()

You can set the following parameters for each configuration variable in OpenHTF:

.namestr

The unique identifier for the configuration variable (must be in snake_case).

.default_value

An optional value that serves as the default if no other value is provided.

.descriptionstr

An optional description explaining the purpose of the configuration variable.

Decorator

You can use the save_and_restore decorator to temporarily change a value during the function call, which is then restored. In this example, you can also configure the variable after its declaration.

main.py
import openhtf as htf
from openhtf.util import configuration

configuration.CONF.declare("phase_variance")
configuration.CONF.load(phase_variance=4)

@configuration.CONF.save_and_restore(phase_variance=55) # Decorator
def phase_one(test):
    phase_variance_to_print = configuration.CONF.phase_variance
    print(f"Value during phase one: {phase_variance_to_print}")

def phase_two(test):
    phase_variance_to_print = configuration.CONF.phase_variance
    print(f"Value during phase two: {phase_variance_to_print}")

def main():
    test = htf.Test(phase_one, phase_two)
    test.execute(test_start=lambda: "SN1234")

if __name__ == "__main__":
    main()

On this page

Run it on your stations

$ tofupilot run .
✓ operator UI ready
$ git push
✓ stations updated
✓ runs uploaded

Same OpenHTF script, no code changes.

How it works