Python
Run Python functions as test phases with full framework access.
You can run any Python function using the python parameter.
Let's start by creating a Python file in a new phases/ directory:
Then add a new phase referencing this module:
main:
- name: Test Functionality
python: phases.check_devicedef check_device():
# Your code hereTofuPilot will manage your Python Environment, run your function, and provide access to framework features through Function Parameters.
File
You can specify which Python file to execute using dot notation. Paths are resolved relative to your procedure.yaml file.
For example, for this file tree:
You can reference these files with:
main:
- name: Foo
python: phases.foo
- name: Bar
python: phases.nested.bar
- name: Baz
python: shared.bazFunction
You can specify which function to call using colon syntax: module:function.
TofuPilot defaults to the last component of the path as the function name.
main:
- name: Calibration
python: calibrate # Calls calibrate() function by default
- name: Custom Function
python: calibrate:run_calibration # Calls run_calibration() insteaddef calibrate():
# Default function - called by first phase
def run_calibration():
# Custom function - called by second phaseEnvironment
Overview
TofuPilot automatically manages Python virtual environments and runs your code in isolation, ensuring consistent behavior across development and production.
As soon as you add a Python phase to your procedure, TofuPilot automatically:
- Looks for an existing virtual environment (
.venvorvenv) in your procedure directory - If found: Uses it as-is
- If not found:
- Creates a
pyproject.tomlfile if one doesn't exist (Python's standard project manifest defining Python version and dependencies) - Creates a new
.venvdirectory with the specified Python version (defaults to 3.11) - Installs all dependencies listed in
pyproject.toml
- Creates a
- Syncs dependencies if
pyproject.tomlchanges
Dependencies
Edit pyproject.toml in your procedure directory to manage dependencies:
[project]
name = "my-procedure"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = [
"numpy>=1.24.0",
"pandas>=2.0.0",
]TofuPilot will automatically install these dependencies when you run your procedure.
Under the hood, TofuPilot uses UV, a Python package manager faster than pip, with full support for the PEP 508 dependency specification.
For advanced use cases, you can configure private registries, local packages, Git dependencies, and platform-specific requirements directly in your pyproject.toml:
Function Parameters
TofuPilot automatically injects parameters based on your function signature. You can refer to each framework feature's documentation for Python function parameters usage.
Phase Result
Use result to set phase outcome
Measurements
Use measurements to record test data
Logs
Use log to output messages
Operator UI
Use ui to update interface components
Attachments
Use attach to attach files and data to reports
Plugs
Use your plug methods from any phase
Unit Under Test
Use unit to set UUT metadata
Run
Use run to access the full test context
How is this guide?