I really like these prompts; thanks for setting them up!
To answer “Unit testing tasks,” this is how I typically do it:
module.py
from prefect import task
@task
def divide_task(x, y):
return x / y
tests/test_module.py
import pytest
from module import divide_task
def test_divide_task():
assert divide_task.fn(1, 2) == 0.5 # fn calls underlying function
def test_divide_task_error():
with pytest.raises(ZeroDivisionError):
divide_task.fn(1, 0)
Then run in the command line.
pytest .
There’s another way of writing it, but I think this might be too verbose.
import pytest
from prefect import flow
from module import divide_task
def test_divide_task():
@flow
def test_flow():
return divide_task(1, 2)
task_state = test_flow().result()
actual = task_state.result()
assert actual == 0.5
Also, there’s docs here Testing - Prefect 2.0