First Test

Learn how to install OpenHTF and create your first test script with step-by-step instructions to set up a simple test phase and execute it.

Last updated · Verified with OpenHTF 1.6.1

Let's create a first test script in under five minutes.

Install OpenHTF with pip, write a script with a single phase, run it from the terminal and get the outcome in the console and a JSON test record on disk.

  • pip install openhtfinstalled
  • main.pyone phase, one measurement
  • python main.py
  • test_record.json

Installation

OpenHTF requires Python 3.10 or newer. Install it with pip inside a virtual environment:

Terminal
python -m venv .venv
source .venv/bin/activate   # Windows: .venv\Scripts\activate
pip install openhtf

That is the whole install: the package pulls its own dependencies (tornado, protobuf, PyYAML, ...). Optional extras for USB and serial plugs are covered on the Installation page.

First run

Create a main.py file in your virtual environment and copy this code.

main.py
import openhtf as htf

def hello_world(test):              # Define "hello_world" phase
    print('Hello world!')
    return htf.PhaseResult.CONTINUE # Continue to next phase

def main():
    test = htf.Test(hello_world)    # Define test with hello_world
    test.execute(lambda: "SN1234")  # Execute with DUT serial

if __name__ == '__main__':
    main()                          # Run main function

Run it with:

Terminal
python main.py
Terminal
Hello world!

======================= test: openhtf_test  outcome: PASS ======================

This is a very simple OpenHTF test, running through the command line without the Operator UI yet. You'll learn to connect instruments, add measurements, and more in the next sections.

Adding a measurement

Most real tests validate a value against a limit. Add a measurement with @htf.measures():

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

@htf.measures(
    htf.Measurement("voltage")
    .in_range(3.0, 5.0)
    .with_units(units.VOLT)
)
def measure_voltage(test):
    test.measurements.voltage = 3.3  # Within range → PASS
    return htf.PhaseResult.CONTINUE

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

if __name__ == '__main__':
    main()
Terminal
======================= test: openhtf_test  outcome: PASS ======================

If you change the voltage value to 10.0, the test will fail because the value is outside the 3.0–5.0 range.

Saving results to JSON

Add an output callback to write the test record to a JSON file:

main.py
import openhtf as htf
from openhtf.output.callbacks import json_factory
from openhtf.util import units

@htf.measures(
    htf.Measurement("voltage")
    .in_range(3.0, 5.0)
    .with_units(units.VOLT)
)
def measure_voltage(test):
    test.measurements.voltage = 3.3
    return htf.PhaseResult.CONTINUE

def main():
    test = htf.Test(measure_voltage)
    test.add_output_callbacks(
        json_factory.OutputToJSON('./test_result.json', indent=2)
    )
    test.execute(lambda: "SN1234")

if __name__ == '__main__':
    main()

After running, open test_result.json to see the complete test record: DUT ID, outcome, phase results, measurements, and timing. The JSON format reference walks through every field.

This file is the hand-off point to any database or dashboard. TofuPilot's upload() callback sends the same record to a hosted workspace in one line — see Manufacturing Test Analytics.

What's next

From here, explore the core features:

  • Phases — organize your test into steps
  • Measurements — validate numeric, string, and boolean values
  • Plugs — abstract hardware interactions into reusable classes
  • Operator UI — add a web interface for production use
  • Tutorial — a full resistor test with a simulated multimeter and power supply
  • Examples — every upstream example, runnable and explained

On this page

First-pass yield
0%4.1
Track with TofuPilot