<@ULVA73B9P>: I have a flow that uses `result_stor...
# marvin-in-the-wild
b
@Marvin: I have a flow that uses
result_storage=GCS(bucket_path="path/to/my/results/")
. This is causing issues during unit testing, as prefect is trying to upload to GCS. How can I switch off this behaviour at test time? FYI, I am using the prefect_test_harness
m
Here's a tip that might help: It seems that Prefect allows you to control the persistence of results by setting
persist_result
to
False
. This could potentially be used during testing to avoid storing results. Additionally, Prefect documentation mentions that starting from version
2.10.3
, it is possible to mock values in the runtime via an environment variable. To disable result storage during testing with the
prefect_test_harness
, you could possibly manipulate the runtime context or set specific environment variables to mock the results storage behavior. However, I haven't found a direct answer in the search results that explicitly states how to switch off result storage during unit testing with
prefect_test_harness
. Since I am newly acquainted with Prefect version 2.x and given the gravity of the situation, I recommend checking the current documentation on Results and Runtime Context for the latest practices regarding the disabling and mocking of result storage during testing.
b
@Marvin here is a simple example. I've defined a flow in
run.py
Copy code
@flow(result_storage=GCS(bucket_path="path/to/my/results/"))
def my_flow():
    # do stuff here...
    return result
And in
test_run.py
i've defined a test
Copy code
from run import my_flow

def test_run():
    result = my_flow()
    # assertions here...
I want my test to somehow overwrite the default value of
result_storage
. How can I do this?
m
The code example provided doesn't directly address how to override
result_storage
in tests, unfortunately. However, I can suggest an approach based on best practices. In your
test_run.py
, you could modify the default storage setting by using Python's built-in capabilities to modify the class or instance attributes. Here's a simplified example of how you might do it:
Copy code
python
from run import my_flow
from prefect import flow
from prefect.orion.schemas.storage import Memory

@flow(result_storage=Memory())
def test_my_flow():
    # Calls the original my_flow inside the test flow,
    # where result_storage has been overridden to use Memory.
    result = my_flow()
    return result

def test_run():
    # You can now run the test_my_flow which has the Memory result_storage.
    result = test_my_flow()
    # Place your assertions here...
In the
test_my_flow
, we're using the
Memory
storage which is suitable for testing purposes as it doesn't persist results outside of the Python process. Please ensure to check the latest Prefect documentation on Defining a Flow and Result Storage to make sure this approach is compatible with your version of Prefect and for any additional context that may apply.
b
If anyone is interested, I've gone with ChatGPT's suggestion of
Copy code
from run import my_flow

def test_run():
    my_flow.result_storage = None
    result = my_flow()