Alastair
02/17/2026, 2:59 PMMarvin
02/17/2026, 2:59 PMMarvin
02/17/2026, 3:07 PM# conftest.py
import pytest
# Register all Prefect testing fixtures
pytest_plugins = ("prefect.testing.fixtures",)
# Start the hosted API server once per test session
@pytest.fixture(scope="session", autouse=True)
def _prefect_server(hosted_api_server):
return hosted_api_server
# Point PREFECT_API_URL at the hosted server for every test
@pytest.fixture(autouse=True)
def _use_server(use_hosted_api_server):
# This fixture just needs to be depended on;
# it sets PREFECT_API_URL appropriately for each test
yield
- With this setup:
- The server starts once (session-scope) and is reused by all tests
- Each test uses the same API URL with ~0ms overhead
- If you have a few tests that should not use the hosted server, you can override with disable_hosted_api_server in those tests
Docs:
- How-to: Testing workflows
- Fixtures API: prefect.testing.fixtures
Option B — Run a single external server for the whole test run
- Start a server once (e.g., in CI) and point tests at it via PREFECT_API_URL:
# in a separate shell or CI job step
prefect server start &
# wait for it to be healthy, then:
export PREFECT_API_URL="<http://127.0.0.1:<port>/api>"
pytest -q
# when done:
prefect server stop
- To ensure nothing tries to auto-start an ephemeral server if PREFECT_API_URL isn’t set somewhere, disable ephemeral mode for tests:
export PREFECT_SERVER_EPHEMERAL_ENABLED=false
# (alias also supported: PREFECT_SERVER_ALLOW_EPHEMERAL_MODE=false)
Tip: Pure unit tests can bypass orchestration entirely
- For logic that doesn’t need orchestration, call the underlying function or flow.fn(...) directly in tests to avoid any API calls.
Which do you prefer?
- If you’re using pytest and are okay with a shared server across tests, Option A is usually the fastest and simplest.
- If you want full control (e.g., start once in CI and reuse across packages), Option B works well—just remember to set PREFECT_API_URL and disable ephemeral auto-start.
If you share how you’re running tests (pytest? CI runner?), I can tailor the exact conftest.py or CI step for your setup.