prefect
prefect copied to clipboard
prefect_test_harness fixture doesn't work when used under unittest.IsolatedAsyncioTestCase
Bug summary
When trying to use the harness fixture in combination with unittest.IsolatedAsyncioTestCase, the local server isn't recognized when trying to get it using get_client.
Minimal reproducible example: Note that you should make sure that you're logged out and not using an ephemeral server
prefect cloud logout
prefect config set PREFECT_SERVER_ALLOW_EPHEMERAL_MODE='False'
import unittest
from prefect import flow
import pytest
from prefect.client.orchestration import get_client
from prefect.testing.utilities import prefect_test_harness
@pytest.fixture(autouse=True, scope="session")
def prefect_test_fixture():
with prefect_test_harness():
yield
@flow
def my_favorite_flow():
return 42
# PASS
def test_my_favorite_flow():
client = get_client()
print(f"client: {client.api_url}")
assert my_favorite_flow() == 42
# PASS
class SyncTestCase(unittest.TestCase):
def test_my_favorite_flow(self):
client = get_client()
print(f"client: {client.api_url}")
assert my_favorite_flow() == 42
# FAIL! see the error below
class AsyncTestCase(unittest.IsolatedAsyncioTestCase):
def test_my_favorite_flow(self):
client = get_client()
print(f"client: {client.api_url}")
assert my_favorite_flow() == 42
# PASS
class AsyncTestCase2(unittest.IsolatedAsyncioTestCase):
def test_my_favorite_flow(self):
with prefect_test_harness():
client = get_client()
print(f"client: {client.api_url}")
assert my_favorite_flow() == 42
Error is:
if not api and PREFECT_SERVER_ALLOW_EPHEMERAL_MODE:
# create an ephemeral API if none was provided
from prefect.server.api.server import SubprocessASGIServer
server = SubprocessASGIServer()
server.start()
assert server.server_process is not None, "Server process did not start"
api = server.api_url
server_type = ServerType.EPHEMERAL
elif not api and not PREFECT_SERVER_ALLOW_EPHEMERAL_MODE:
> raise ValueError(
"No Prefect API URL provided. Please set PREFECT_API_URL to the address of a running Prefect server."
)
E ValueError: No Prefect API URL provided. Please set PREFECT_API_URL to the address of a running Prefect server.
Version info
Version: 3.0.10
API version: 0.8.4
Python version: 3.12.3
Git commit: 3aa2d893
Built: Tue, Oct 15, 2024 1:31 PM
OS/Arch: darwin/x86_64
Profile: default
Server type: unconfigured
Pydantic version: 2.9.2
Additional context
No response