Attachments

Learn how to attach files and artifacts collected during test execution to the OpenHTF test record with step-by-step examples.

Last updated · Verified with OpenHTF 1.6.1

Attach files collected during execution to the test record.

During a phase, test.attach stores in-memory data and test.attach_from_file stores a file from disk. Both are saved on the phase record with their MIME type and end up in the test record's attachments.

  • Test start
  • phase_inspection
  • test.attach("report.txt", data)from memory · text/plain · 2 KB
  • test.attach_from_file("qc.jpg")from disk · image/jpeg · 1.2 MB
  • Test end
TestRecord
phases[0].attachments

    Hardware tests often generate extra data, such as images, that can't be captured as measurements. OpenHTF allows you to attach these files to the test record.

    Files

    You can attach binary data or files generated during the test.

    main.py
    import openhtf as htf
    
    def phase_attachment(test):
        # Attach a binary log with the identifier "test_attachment"
        test.attach("test_attachment", "This is test log data.".encode("utf-8"))
        # Optional third argument: an explicit MIME type
        test.attach("config_snapshot", b'{"gain": 2}', mimetype="application/json")
        return htf.PhaseResult.CONTINUE
    
    def main():
        test = htf.Test(phase_attachment)
        test.execute(lambda: "PCB0001")
    
    if __name__ == "__main__":
        main()
    test.attach(name, binary_data, mimetype=INFER_MIMETYPE)

    Store binary_data (bytes or str) under name. The MIME type is guessed from name unless given.

    test.attach_from_file(filename, name=None, mimetype=INFER_MIMETYPE)

    Read filename and attach it. name defaults to the file's base name.

    test.get_attachment(name)

    Return the Attachment (.data, .mimetype, .sha1) during the test.

    Attachments are stored in memory until the test ends, then written by output callbacks. OutputToJSON inlines them base64-encoded by default (inline_attachments=True); pass inline_attachments=False to keep the JSON small when attaching images or long logs. The Operator UI lists attachments under each phase and lets the operator download them.

    Files by path

    You can attach larger or pre-existing files from disk. The attachment name defaults to the file name and the MIME type is inferred from the extension; both can be overridden.

    main.py
    import openhtf as htf
    
    def phase_file_attachment(test):
        # Name and MIME type inferred: "scope_capture.png", image/png
        test.attach_from_file("/tmp/scope_capture.png")
        # Explicit name and MIME type
        test.attach_from_file(
            "/var/log/station/uart.log",
            name="uart_log",
            mimetype="text/plain",
        )
        return htf.PhaseResult.CONTINUE
    
    def main():
        test = htf.Test(phase_file_attachment)
        test.execute(lambda: "PCB0001")
    
    if __name__ == "__main__":
        main()

    Advanced use cases

    Access attachments during execution

    You can load and access attached files during test execution.

    main.py
    import openhtf as htf
    
    def phase_attachment(test):
        # Attach some data
        test.attach("test_attachment", "Attachment data".encode("utf-8"))
    
        # Retrieve and print the attached data
        test_attachment = test.get_attachment("test_attachment")
        print(test_attachment.data)
    
        return htf.PhaseResult.CONTINUE
    
    def main():
        test = htf.Test(phase_attachment)
        test.execute(lambda: "PCB0001")
    
    if __name__ == "__main__":
        main()

    On this page

    First-pass yield
    0%4.1
    Track with TofuPilot