# Decorators
URL: /decorator
Learn how to use OpenHTF decorators to enhance your test phases with measurements, hardware plugins, and execution configuration.
Learn how to use OpenHTF decorators to enhance your test phases.
Python decorators modify function behavior without changing their code. In OpenHTF, decorators add functionality to test phases like declaring measurements, injecting hardware plugins, and configuring execution. They're applied using the `@` symbol before function definitions:
OpenHTF provides several essential decorators that make test writing more powerful and organized:
The `@htf.measures` decorator is used to declare what measurements a phase will capture and validate:
The `@htf.plug` decorator injects hardware or software plugins into your test phases:
The `@htf.PhaseOptions` decorator configures how a phase should execute:
OpenHTF decorators can be combined to create powerful test phases:
Let's enhance our first test to demonstrate decorator usage:
This enhanced version demonstrates:
**Measurement validation** using `@htf.measures`
**Phase timeout** using `@htf.PhaseOptions`
**Custom validation** with lambda functions
Understanding these decorators is crucial for building robust OpenHTF tests that can validate measurements, interact with hardware, and handle various execution scenarios.
Python decorators are callables, which means you can apply them at runtime instead of with the `@` syntax: `htf.measures(m)(phase)` returns the decorated phase. This is how you build phases whose limits come from a config file, a product database or operator input.
The same works for `@htf.plug`. A worked measurement example is under Callable decorators.
Beyond the three above, OpenHTF exposes:
Attach phase diagnosers that classify the phase's results.
Sample a value in a background thread while the phase runs — see Monitors.
Temporarily override configuration values for one phase.
Fill a plug's `__init__` arguments from configuration keys of the same name.
Not decorators but the same idea: return a copy of a phase with template arguments or plug placeholders filled in. Used in the with\_plugs example.