# Phase Groups
URL: /phase-groups
Structure your OpenHTF tests with Phase Groups for guaranteed teardown execution, setup/main/teardown patterns, and nested group composition.
Group phases into setup, main, and teardown blocks so cleanup always runs.
Hardware tests often need guaranteed cleanup: close relays, power down devices, disconnect instruments. A `PhaseGroup` ensures teardown phases run even if setup or main phases fail, similar to a `try/finally` block.
A `PhaseGroup` takes three lists of phases: `setup`, `main`, and `teardown`. The rules are:
**Setup** phases run first. If any fails, the group is never entered — main and teardown are skipped entirely.
**Main** phases run after setup succeeds. If any fails, remaining main phases are skipped.
**Teardown** phases run after main, regardless of whether main phases passed or failed. They are guaranteed to execute once setup has succeeded.
All setup → all main → all teardown phases run in order.
Remaining setup skipped. Main and teardown are skipped entirely — the group was never entered.
Remaining main phases are skipped. Teardown still runs because the group was entered during setup.
Remaining teardown phases still run. Teardown errors are logged but do not prevent other teardown phases from executing.
You can use factory methods to create groups more concisely when you only need teardown or setup.
Returns a decorator that wraps main phases with guaranteed teardown.
Returns a decorator that runs setup phases before main phases.
Returns a decorator that wraps main phases with both setup and teardown.
Phase groups can be nested inside other groups. Each group independently guarantees its own teardown. If an inner group's main phase fails, the inner teardown runs, then the outer teardown runs.
When `inner_main` fails, execution order is: `inner_teardown` → `outer_teardown`. Both teardowns run because both groups were entered.