Hello World Example
The canonical OpenHTF hello_world.py example explained — one phase, one measurement, a JSON output callback and a DUT ID prompt — with its console and JSON output.
Last updated · Verified with OpenHTF 1.6.1
The smallest useful OpenHTF test: one phase that logs a line and sets a string measurement, written to a JSON file named after the DUT. Based on examples/hello_world.py.
import os.path
import openhtf as htf
from openhtf.output.callbacks import json_factory
from openhtf.plugs import user_input
@htf.measures(htf.Measurement('hello_world_measurement'))
def hello_world(test):
"""A hello world test phase."""
# test.logger writes to the test record (and to the Operator UI).
test.logger.info('Hello World!')
# A measurement declared above must be set exactly once.
test.measurements.hello_world_measurement = 'Hello Again!'
def create_and_run_test(output_dir: str = '.'):
test = htf.Test(hello_world)
# {dut_id} is expanded from the record when the file is written.
test.add_output_callbacks(
json_factory.OutputToJSON(
os.path.join(output_dir, '{dut_id}.hello_world.json'), indent=2
)
)
# Ask the operator for the DUT ID; OpenHTF requires one per run.
test.execute(test_start=user_input.prompt_for_test_start())
if __name__ == '__main__':
create_and_run_test()$ python hello_world.py
Enter a DUT ID in order to start the test.
--> SN1234
======================= test: openhtf_test outcome: PASS ======================What it shows
@htf.measures(htf.Measurement('...'))Declares the measurement before the phase runs. With no validator, any value passes; an unset value fails. Measurements →
test.logger.info(...)The right way to log from a phase: the record's log_records and the Operator UI both receive it, unlike print(). Logger →
json_factory.OutputToJSON(pattern, indent=2)Output callback writing one file per run. The pattern accepts any record field. Output Callbacks →
user_input.prompt_for_test_start()A start-trigger phase that prompts for the DUT ID on the console (and in the Operator UI when one is running). Device Under Test →
The output file
SN1234.hello_world.json, trimmed:
{
"dut_id": "SN1234",
"station_id": "my-laptop",
"outcome": "PASS",
"phases": [
{
"name": "hello_world",
"outcome": "PASS",
"measurements": {
"hello_world_measurement": {
"name": "hello_world_measurement",
"outcome": "PASS",
"measured_value": "Hello Again!"
}
}
}
],
"log_records": [
{ "level": 20, "logger_name": "openhtf.test_record.<uid>.phase.hello_world", "message": "Hello World!" }
]
}The start trigger is itself a phase and appears first in phases[] as trigger_phase (omitted above). Every field is documented in the JSON format reference.
Variations
- Skip the prompt while developing:
test.execute(test_start=lambda: 'SN1234'). - Print failures to the console as well:
test.add_output_callbacks(console_summary.ConsoleSummary()). - Send the record to a database in addition to the file:
test.add_output_callbacks(upload())fromtofupilot.openhtf— Manufacturing Test Analytics.
Next
Overview
Runnable OpenHTF example scripts, one per feature — hello world, measurements, plugs, phase groups, branches, checkpoints, repeat, stop on first failure, custom JSON output, Operator UI — each explained line by line.
Measurements
The upstream measurements.py example explained — string and numeric measurements, inline kwargs declaration, validators, units, a multi-dimensional power time series converted to a pandas DataFrame, and marginal limits.