Vincent Chéry
07/04/2025, 3:21 PMprefect_test_harness
context, by default, flows executions return the return value of the flow. How can I get the flow execution to return a State
object instead ?Marvin
07/04/2025, 3:21 PMMarvin
07/04/2025, 3:21 PMprefect_test_harness
context, you can get the flow execution to return a State
object by setting the return_state=True
parameter when calling the flow. Here's an example:
python
from prefect import flow
@flow
def my_test_flow():
return "Hello"
with prefect_test_harness():
state = my_test_flow(return_state=True) # Returns State object instead of "Hello"
assert state.type == "COMPLETED"
assert state.result() == "Hello" # Access the actual return value
When return_state=True
is set, you'll get back a State
object that contains metadata about the flow run, including its status and result. You can then:
- Check the state type using state.type
- Access the actual flow return value using state.result()
- Inspect other state attributes like timestamps and run IDs
This is particularly useful when you need to test not just the flow's output, but also its execution state and metadata.Vincent Chéry
07/04/2025, 3:26 PMMarvin
07/04/2025, 3:27 PMMarvin
07/04/2025, 3:29 PMpython
from prefect import flow, task
from prefect.states import get_state_result
@task
def task_1():
return "Hello"
@task
def task_2():
return "World"
@flow
def my_flow():
# Store task states in a dictionary
task_states = {}
task_states["task_1"] = task_1(return_state=True)
task_states["task_2"] = task_2(return_state=True)
return task_states
# Run the flow and get states
with prefect_test_harness():
flow_state = my_flow(return_state=True)
task_states = flow_state.result()
# Access individual task results
task_1_result = get_state_result(task_states["task_1"])
task_2_result = get_state_result(task_states["task_2"])
# Or use the state's result() method directly
task_1_result = task_states["task_1"].result()
task_2_result = task_states["task_2"].result()
A few important points:
1. You need to call tasks with return_state=True
to get their State objects
2. You can access task results either using the get_state_result()
function or the state's result()
method
3. The State object contains additional metadata about the task execution, such as:
- `state.type`: The type of state (e.g., "COMPLETED", "FAILED")
- `state.timestamp`: When the state was created
- `state.name`: The name of the state
This approach is particularly useful for testing scenarios where you need to verify both the results and the execution state of individual tasks within a flow.Vincent Chéry
07/04/2025, 3:30 PMMarvin
07/04/2025, 3:31 PMMarvin
07/04/2025, 3:32 PM