https://prefect.io logo
Title
a

Andreas Tsangarides

05/25/2023, 9:20 AM
hi all! Trying to test tasks that make use of the run context with
Prefect 1
I could do this:
def test_my_task():
    with prefect.context(dict(flow_run_id="test_id")):
      my_task()
    # assertions
how c an this be done in
Prefect 2
?
Untitled.py
d

Deceivious

05/25/2023, 9:27 AM
I have issues with
get_run_context
being called from
task
.
I prefer explicitly using
from prefect.context import FlowRunContext
when extract flow run information.
a

Andreas Tsangarides

05/25/2023, 10:16 AM
@Deceivious have changed to
from prefect import runtime
# inside flow
flow_run_id = runtime.flow_run.id
i.e. use the context inside the flows and pass extracted variables to tasks this way the tasks can be tested independently (unit testing fashion) was looking for a neat way to test the task itself…