Examples

Ignore Early-Canceled Tests Example

The upstream ignore_early_canceled_tests.py example — subclass OutputToJSON to skip writing records for tests aborted (Ctrl-C) before an operator entered a DUT ID, using default_dut_id.

Last updated · Verified with OpenHTF 1.6.1

A station that waits for a barcode scan produces a useless record every time someone presses Ctrl-C at the prompt. This example filters those out in the output callback. Based on examples/ignore_early_canceled_tests.py.

ignore_early_canceled_tests.py
import openhtf as htf
from openhtf.core import test_record
from openhtf.output.callbacks import json_factory
from openhtf.plugs import user_input
from openhtf.util import console_output

DEFAULT_DUT_ID = '<UNSET_DUT_ID>'


class CustomOutputToJSON(json_factory.OutputToJSON):

    def __call__(self, record):
        if (record.outcome == test_record.Outcome.ABORTED and
                record.dut_id == DEFAULT_DUT_ID):
            console_output.cli_print('Test was aborted at test start. Skipping output to JSON.')
        else:
            console_output.cli_print('Outputting test record to JSON.')
            super().__call__(record)


@htf.plug(user=user_input.UserInput)
def HelloWorldPhase(test, user):
    test.logger.info('Hello World!')
    user.prompt('The DUT ID is `%s`. Press enter to continue.' % test.test_record.dut_id)


def main():
    test = htf.Test(HelloWorldPhase)
    test.configure(default_dut_id=DEFAULT_DUT_ID)
    test.add_output_callbacks(CustomOutputToJSON('./{dut_id}.hello_world.json', indent=2))
    test.execute(test_start=user_input.prompt_for_test_start())


if __name__ == '__main__':
    main()
Terminal
$ python ignore_early_canceled_tests.py
Enter a DUT ID in order to start the test.
--> ^C
Test was aborted at test start. Skipping output to JSON.

===================== test: openhtf_test  outcome: ABORTED =====================

Press Ctrl-C after entering a DUT ID and the record is written, because the abort then carries information about a real unit.

What it shows

Subclassing OutputToJSON

An output callback is any callable taking a TestRecord. Subclassing the built-in keeps its file-pattern handling and lets you add a condition in __call__. Custom output →

Outcome.ABORTED

Set when the run ends on KeyboardInterrupt (SIGINT). Other exceptions give ERROR and are kept — they may be worth debugging. Test outcomes →

test.configure(default_dut_id=...)

A recognisable sentinel instead of the default UNKNOWN_DUT, so the callback can tell "never scanned" from "scanned, then aborted". Test Options →

console_output.cli_print

OpenHTF's console helper; respects the CLI colour settings and does not interleave badly with the logger.

The same shape filters records for any reason — skip uploads for station_id == 'dev-bench', write FAIL records to a second directory, or add a retry around a network upload. TofuPilot's upload() callback can be wrapped the same way. Output Callbacks →

Next

On this page

First-pass yield
0%4.1
Track with TofuPilot