While `prefect_test_harness` provides an environme...
# prefect-community
g
While
prefect_test_harness
provides an environment for flows to be tested, how might I go about testing a task in Prefect 2.0? It seems like calling
.run()
on tasks has not made it over from 1.0 yet.
a
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
n
you can also check out the tests used in our collections, like
prefect-slack
(link)
πŸ‘ 1
g
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
n
you can use the
.fn()
method on tasks like
Copy code
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