Scott
01/08/2025, 3:34 PMMarvin
01/08/2025, 3:35 PMprefect_test_harness
is a utility provided by Prefect for setting up a temporary sandbox Prefect database/server to run tests against. You can use this utility in conjunction with pytest
to create a test environment where you can mock variables.
Here's a basic example of how you might set up such a test using `pytest`:
python
import pytest
from prefect.testing.utilities import prefect_test_harness
@pytest.fixture(autouse=True)
def prefect_db():
with prefect_test_harness() as harness:
yield harness
This setup will automatically apply the test harness to all tests in the test suite, allowing you to mock and manipulate Prefect variables within this controlled environment.
For more details, you can refer to the Prefect documentation on variables and explore the Prefect GitHub repository for more examples.Scott
01/08/2025, 3:37 PMScott
01/08/2025, 3:38 PMMarvin
01/08/2025, 3:38 PMconfig_file.yaml
using the test harness, you can leverage the Prefect Python SDK to set the variable within your test environment. Here's a basic approach using pytest
and the `prefect_test_harness`:
1. Use the prefect_test_harness
to set up the test environment.
2. Use the Prefect SDK to set the variable within this environment.
Here's an example code snippet:
python
import pytest
from prefect.testing.utilities import prefect_test_harness
from prefect.client import get_client
@pytest.fixture(autouse=True)
def prefect_db():
with prefect_test_harness() as harness:
# Create a mock variable
client = get_client()
client.set_variable(name="config_file.yaml", value="/path/to/mock/config_file.yaml")
yield harness
This setup will create a mock Prefect variable named config_file.yaml
pointing to /path/to/mock/config_file.yaml
in your test harness environment. You can then run your tests with this mocked configuration in place.
For more details on managing variables, you can refer to the Prefect documentation on variables.