<@ULVA73B9P> in my unit tests, when I submit a flo...
# ask-marvin
a
@Marvin in my unit tests, when I submit a flow for testing an in-process prefect server is started up and then torn down for every single test. this is causing our test runtime to balloon out of control - any suggestions on how we can resolve this? I'm thinking that we spin up a new server just for tests and connect to that
m
thought for 490.3 seconds
You’re right: starting an in‑process (ephemeral) server per test will crush your runtime. Two good approaches to fix it: Option A — Use Prefect’s pytest fixtures to run one hosted server per test session - Prefect 3.x ships pytest fixtures that start a single subprocess server once (session-scoped) and point all tests at it. - Add this to your conftest.py:
Copy code
# 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:
Copy code
# 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:
Copy code
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.