https://prefect.io logo
#prefect-community
Title
# prefect-community
a

Avi A

05/21/2020, 12:17 PM
is there a clean way to invoke tasks as functions? e.g. for unit testing outside a prefect flow? The cleanest way I could think of is to define the function regularly (without the
task
decorator) and treat them as tasks only inside the flow. i.e.:
Copy code
def f():
    pass

# regular call
foo = f()

# inside a flow
with Flow() as flow:
    foo = task(f)()
any nicer suggestions?
j

Jeremiah

05/21/2020, 12:18 PM
Hi @Avi A, you can call task.run() to invoke the task as a function. All the “Prefect stuff” happens in the
TaskRunner
class to preserve easy testing of tasks!
👏 1
Copy code
@task
def f(x, y):
    return x + y

f.run(1,2) == 3
hattip 1
l

Laura Lorenz (she/her)

05/21/2020, 12:21 PM
For some extra inspo while you are working on testing, you might be interested in these two pages in the docs too! https://docs.prefect.io/core/idioms/testing-flows.html#testing-prefect-flows-and-tasks https://docs.prefect.io/core/advanced_tutorials/local-debugging.html
upvote 1
👏 1
a

Avi A

05/21/2020, 12:22 PM
Thanks guys, I adore your work!
👍 1
🤗 1
j

Jeremiah

05/21/2020, 12:25 PM
@Marvin archive “Is there an easy way to test a task as a function?”
😍 1