Test workflows
Learn about writing tests for Prefect flows and tasks.
Prefect provides a simple context manager for unit tests that allows you to run flows and tasks against a temporary local SQLite database.
For more extensive testing, use prefect_test_harness
as a fixture in your unit testing framework. For example, when using pytest
:
Session scoped fixture
In this example, the fixture is scoped to run once for the entire test session. In most cases, you do not need a clean database for each test. Just isolate your test runs to a test database. Creating a new test database per test creates significant overhead, so we recommend scoping the fixture to the session. If you need to isolate some tests fully, use the test harness again to create a fresh database.
Unit testing tasks
To test an individual task, access the original function using .fn
:
Disable logger
If your task uses a logger, you can disable the logger to avoid the RuntimeError
raised from a missing flow context.
Was this page helpful?