Gabriel Lespérance
07/24/2024, 9:57 PMMarvin
07/24/2024, 9:58 PM.fn
attribute of the task and the prefect_test_harness
for unit tests.
Here’s a brief guide:
1. Unit Testing a Task:
- You access the original function of the task using .fn
.
- Disable logging if your task uses a logger to avoid RuntimeError
from a missing flow context.
python
from prefect import task
from prefect.logging import disable_run_logger
@task
def my_favorite_task():
return 42
def test_my_favorite_task():
with disable_run_logger():
assert my_favorite_task.fn() == 42
2. Unit Testing a Flow:
- Use prefect_test_harness
to run flows against a temporary local SQLite database.
python
from prefect import flow
from prefect.testing.utilities import prefect_test_harness
@flow
def my_favorite_flow():
return 42
def test_my_favorite_flow():
with prefect_test_harness():
assert my_favorite_flow() == 42
3. Extensive Testing using pytest
:
- Leverage prefect_test_harness
as a fixture in your unit testing framework like pytest
.
For more details, refer to the Prefect documentation on testing:
Testing in Prefect
For code examples, you can check out:
Prefect Code Examples on GitHub
This should help you get started with setting up and executing tests for your Prefect tasks and flows.