Examples

Checkpoints Example

The upstream checkpoints.py example — a failing measurement, a checkpoint, and a long burn-in phase that is skipped because of it, with ConsoleSummary showing the failure.

Last updated · Verified with OpenHTF 1.6.1

By default a failed measurement does not stop a test. A checkpoint does. Based on examples/checkpoints.py, with the phases it imported from measurements.py inlined.

checkpoints.py
import time

import openhtf as htf
from openhtf.output.callbacks import console_summary
from openhtf.output.callbacks import json_factory
from openhtf.util import checkpoints


@htf.measures(htf.Measurement('hello_world_measurement'))
def hello_phase(test):
    test.measurements.hello_world_measurement = 'Hello!'


@htf.measures(
    htf.Measurement('fixed_time')
    .in_range(0, 10)
    .doc('This is going to fail validation.')
    .with_units(htf.units.SECOND)
)
def failing_phase(test):
    test.measurements.fixed_time = 20         # FAIL: 20 is outside 0..10


@htf.measures('first_measurement', 'second_measurement')
def lots_of_measurements(test):
    test.measurements.first_measurement = 'First!'
    test.measurements['second_measurement'] = 'Second :('


def long_running_phase(test):
    # A burn-in you do not want to run on a unit that already failed.
    for _ in range(10):
        test.logger.info('Still running....')
        time.sleep(10)
    test.logger.info('Done with long_running_phase')


def main():
    test = htf.Test(
        hello_phase,
        failing_phase,
        lots_of_measurements,
        checkpoints.checkpoint(),          # STOP here if any phase above failed
        long_running_phase,
    )
    test.add_output_callbacks(console_summary.ConsoleSummary())
    test.add_output_callbacks(json_factory.OutputToJSON('./checkpoints.json', indent=2))
    test.execute(test_start=lambda: 'MyDutId')


if __name__ == '__main__':
    main()
Terminal
$ python checkpoints.py
openhtf_test:FAIL
failed phase: failing_phase [ran for 0.00 sec]
  failed_item: fixed_time (Outcome.FAIL)
    measured_value: 20
    validators:
      validator: 0 <= x <= 10

======================= test: openhtf_test  outcome: FAIL ======================

The test returns in well under a second: lots_of_measurements still ran (a failed measurement is not terminal), the checkpoint saw failing_phase's FAIL and returned STOP, and the 100-second long_running_phase never started.

What it shows

checkpoints.checkpoint(name='checkpoint')

Shorthand for PhaseFailureCheckpoint.all_previous(name): stop if any earlier phase in the test has outcome FAIL. Checkpoint types →

Placement

Put the checkpoint immediately before the expensive phase. Everything before it still runs, so you collect all cheap failures in one pass.

ConsoleSummary

Lists every failed measurement with value and validator, which the one-line banner does not. Console summary →

Not in the JSON

A checkpoint is not a phase: it leaves no phase record, and 1.6.1 does not serialize TestRecord.checkpoints to JSON. The effect is visible as the missing long_running_phase and the FAIL outcome. JSON format →

Variations

  • Stop on the very first failed measurement anywhere: test.configure(stop_on_first_failure=True)example.
  • Only check the immediately preceding phase: PhaseFailureCheckpoint.last('after_cal').
  • Inside a subtest, pass action=htf.PhaseResult.FAIL_SUBTEST to fail only that subtest instead of stopping the test.
  • Trigger on a diagnosis rather than a failure: htf.DiagnosisCheckpoint(name, DiagnosisCondition.on_any(...)).

Next

On this page

First-pass yield
0%4.1
Track with TofuPilot