Ying Ting Loo
09/13/2024, 5:21 AMMarvin
09/13/2024, 5:21 AMYing Ting Loo
09/13/2024, 5:21 AMMarvin
09/13/2024, 5:21 AMpython
from prefect.testing.utilities import prefect_test_harness
from pytest import fixture
@fixture(scope='session', autouse=True)
def prefect_harness(tmp_path_factory):
wd = tmp_path_factory.mktemp(".prefect")
with prefect_test_harness(path=str(wd)):
yield
source
### Deprecation Warning:
There is an issue regarding rewriting the event_loop
fixture in pytest-asyncio
, suggesting it might lead to future errors. Ensure you handle asyncio event loops appropriately if your tests involve them.
source
You should be able to define your fixture similarly to the example provided and adapt it for your needs. If you encounter issues or have specific constraints, feel free to ask!
Would this approach work for you?Ying Ting Loo
09/13/2024, 5:23 AMprefect_test_harness
starts a temporary database but how about the prefect serverMarvin
09/13/2024, 5:23 AMprefect_test_harness
setup. Prefect doesn't provide a specific helper for spinning up the server in a test environment, but you can use subprocesses or dedicated test container setups for such needs.
Here’s a general outline to get you started:
1. Combining prefect_test_harness
with manual server control:
- Start the Prefect server manually within your fixture.
- Ensure proper teardown to stop the server after tests complete.
python
import pytest
from prefect.testing.utilities import prefect_test_harness
import subprocess
import time
@pytest.fixture(scope="session", autouse=True)
def prefect_server():
# Start the Prefect server
server_process = subprocess.Popen(["prefect", "server", "start"])
time.sleep(20) # Give the server some time to start
yield # Run tests
# Terminate Prefect server
server_process.terminate()
@pytest.fixture(scope="session", autouse=True)
def prefect_harness(tmp_path_factory):
wd = tmp_path_factory.mktemp(".prefect")
with prefect_test_harness(path=str(wd)):
yield
### Key Points:
1. Manual Server Startup:
Use subprocess
to start Prefect server.
Adjust the waiting time based on server startup duration.
2. Proper Cleanup:
Ensure the server process is terminated after tests.
3. Integration with prefect_test_harness
:
Combines setting up a temporary database with the server.
### Resources:
- Prefect Test Harness: Prefect Testing Guide
- General Prefect Server Setup: Prefect Deployment Guide
Would this setup integrate well with your existing test workflow?