Geoffrey Keating

05/05/2022, 4:08 PM
provides an environment for flows to be tested, how might I go about testing a task in Prefect 2.0? It seems like calling
on tasks has not made it over from 1.0 yet.

Anna Geller

05/05/2022, 4:11 PM
The easiest would be to look at the tests in the repo e.g. this one In Prefect 2.0 you can call arbitrary Python code in your flows so testing anything in Prefect 2.0 is quite similar to writing tests for anything in Python. What in particular would you like to test?
this Discourse topic may also help
I guess you probably meant this part that Michael addressed in this discussion:
"we’re planning to create a way to test tasks outside of flows"
πŸ‘ 1


05/05/2022, 4:30 PM
you can also check out the tests used in our collections, like
πŸ‘ 1

Geoffrey Keating

05/05/2022, 5:04 PM
Thanks Anna and Nate - ad-hoc flows for testing single classes will work, although it would be somewhat useful to be able to call tasks out of context.
πŸ‘ 1


05/05/2022, 6:02 PM
you can use the
method on tasks like
import pytest

from my_prefect_collection.tasks import my_task

async def test_successful_task(mock_successful_calls):
    my_task_result = await my_task.fn()

    assert type(trigger_sync_result) is whatever
to call tasks out of context
πŸ’― 2