<@ULVA73B9P> how can I mock a flow `run_count` in ...
# ask-marvin
p
@Marvin how can I mock a flow
run_count
in unit tests? or another way of putting it is how can I have access to
retry_delay_seconds
and
retries
when running a flow?
m
Mocking the
run_count
of a flow or accessing
retry_delay_seconds
and
retries
in Prefect unit tests can be done programmatically, but the query did not return detailed instructions or specific code examples relevant to your question. However, based on common testing patterns in Python, I can provide some general guidance. To mock or access these values during a flow run, you would typically use mocking libraries such as
unittest.mock
in Python. Here's how you might approach this: 1. Mocking `run_count`: You could use a mocking library to replace the
run_count
value with a mocked one. This involves patching the object that tracks the
run_count
within Prefect's code so that when your test runs, it uses the mocked value instead. 2. Accessing
retry_delay_seconds
and `retries`:
During a flow run, these parameters can be specified as part of the flow or task configuration. When writing tests, you can manually set these parameters in the flow definition and inspect them as needed. Here's an outline of what the code in your unit tests might look like:
Copy code
python
from unittest.mock import patch
from prefect import Flow
import your_flow_module  # This should be the module where your flow is defined

# Mocking run_count example
with patch("your_flow_module.some_object.run_count", new_callable=PropertyMock) as mock_run_count:
    mock_run_count.return_value = 3  # Assuming you want to simulate 3 runs
    # Proceed to run your flow and tests here

# Setting and accessing retry_delay_seconds and retries example
with Flow("test-flow", retries=2, retry_delay_seconds=10) as flow:
    # Define tasks and flow structure here

# Now you can run your flow and check the retries and retry_delay_seconds
flow.run()
assert flow.retries == 2
assert flow.retry_delay_seconds == 10
For a more specific answer, I recommend checking the documentation or Prefect's testing guidelines to see if there are updated or recommended approaches for testing these scenarios in Prefect 2.x. Alternatively, I can perform a GitHub issue search in the PrefectHQ/prefect repository if you would like to see if there's any previously reported issue or discussion related to this topic. Would you like me to do so?
n
🙅
Copy code
In [1]: from prefect import flow

In [2]: @flow(log_prints=True, retries=1)
   ...: def foo():
   ...:     print(foo.retries, foo.retry_delay_seconds)
   ...:

In [3]: foo()
15:10:08.443 | INFO    | prefect.engine - Created flow run 'vivacious-seal' for flow 'foo'
15:10:08.899 | INFO    | Flow run 'vivacious-seal' - 1 0
15:10:09.869 | INFO    | Flow run 'vivacious-seal' - Finished in state Completed()
p
Thanks so Im thinking I need to patch flow module and then access retries and make it return lets say 20? What about the
with Flow
solution? Can I just override the original retry and retry_delay_seconds there when doing unit tests?
n
with Flow is prefect 1 syntax that is still too prevalent on the internet for marvin to forget apparently
what are you trying to test in your unit tests?
i typically define a flow/task object as a fixture and then say
my_flow.with_options(retries=42)(*args, **kwargs)
in my test
✅ 1
p
so I originally have retries=30 and retry_delay_second=10. What I am trying to do is to make them 0 so test raises error
Thanks that should work