<@ULVA73B9P> Is it possible to spawn a runner proc...
# ask-marvin
r
@Marvin Is it possible to spawn a runner process using an ephemeral API for unit testing?
m
Yes, it is possible to spawn a runner process using an ephemeral API for unit testing in Prefect 3.x. The ephemeral mode can be enabled by configuring the server settings. Here are some relevant settings: - Ephemeral Mode Timeout: You can set the startup timeout for the server in ephemeral mode using the environment variable
PREFECT_SERVER_EPHEMERAL_STARTUP_TIMEOUT_SECONDS
. - Event Streaming: The server can stream events to the API via WebSockets, controlled by
PREFECT_SERVER_EVENTS_STREAM_OUT_ENABLED
. For unit testing, Prefect relies on writing tests using frameworks like
pytest
, and it is recommended to write tests that fail before your changes and pass afterward to ensure robustness. For more details, you can refer to the Prefect documentation on ephemeral mode settings. If you need to explore more about Prefect's public API or specific modules, you can start with the top-level API which includes functions like
flow
,
task
,
get_client
, and more. You can explore further based on your specific needs. Let me know if there's anything else you need!
r
@Marvin Can you give me the example code for spawning a runner using an ephemeral API server? A pytest fixture would be preferable.
m
Here's an example of how you can create a pytest fixture to spawn a runner using an ephemeral API server in Prefect 3.x. This example uses the
prefect_test_harness
utility to set up a temporary Prefect database/server for testing:
Copy code
python
import pytest
from prefect.testing.utilities import prefect_test_harness
from prefect import flow, task

@pytest.fixture(autouse=True)
def prefect_db():
    with prefect_test_harness() as harness:
        """Sets up a temp sandbox Prefect database/server for running tests."""
        yield harness

@task
def say_hello(name: str) -> str:
    return f"Hello, {name}!"

@flow
def greet(name: str):
    return say_hello(name)

def test_greet(prefect_db):
    result = greet("World")
    assert result == "Hello, World!"
In this example: -
prefect_test_harness()
sets up a temporary Prefect environment for testing. - A simple
flow
and
task
are defined to demonstrate the functionality. - The
test_greet
function uses the fixture to ensure the Prefect environment is available during the test. For more details on Prefect testing utilities, you can refer to the Prefect testing documentation.