<@ULVA73B9P>; why is my unit test failing? I have ...
# ask-marvin
p
@Marvin; why is my unit test failing? I have a testcase written like:
Copy code
from prefect.testing.utilities import prefect_test_harness
from my_project import flow_to_test

def test_flow():
    with prefect_test_harness():
        flow_to_test()
when I try testing this, it results in this error:
Copy code
E                   anyio._backends._asyncio.ExceptionGroup: 2 exceptions were raised in the task group:
E                   ----------------------------
E                   Traceback (most recent call last):
E                     File "/Users/psktam/Library/Caches/pypoetry/virtualenvs/project-RcQVLUoH-py3.10/lib/python3.10/site-packages/prefect/engine.py", line 1389, in create_task_run_then_submit
E                       task_run = await create_task_run(
E                     File "/Users/psktam/Library/Caches/pypoetry/virtualenvs/project-RcQVLUoH-py3.10/lib/python3.10/site-packages/prefect/engine.py", line 1434, in create_task_run
E                       task_run = await flow_run_context.client.create_task_run(
E                     File "/Users/psktam/Library/Caches/pypoetry/virtualenvs/project-RcQVLUoH-py3.10/lib/python3.10/site-packages/prefect/client/orchestration.py", line 1980, in create_task_run
E                       response = await <http://self._client.post|self._client.post>(
E                     File "/Users/psktam/Library/Caches/pypoetry/virtualenvs/project-RcQVLUoH-py3.10/lib/python3.10/site-packages/httpx/_client.py", line 1848, in post
E                       return await self.request(
E                     File "/Users/psktam/Library/Caches/pypoetry/virtualenvs/project-RcQVLUoH-py3.10/lib/python3.10/site-packages/httpx/_client.py", line 1530, in request
E                       return await self.send(request, auth=auth, follow_redirects=follow_redirects)
E                     File "/Users/psktam/Library/Caches/pypoetry/virtualenvs/project-RcQVLUoH-py3.10/lib/python3.10/site-packages/prefect/client/base.py", line 312, in send
E                       response.raise_for_status()
E                     File "/Users/psktam/Library/Caches/pypoetry/virtualenvs/project-RcQVLUoH-py3.10/lib/python3.10/site-packages/prefect/client/base.py", line 164, in raise_for_status
E                       raise PrefectHTTPStatusError.from_httpx_error(exc) from exc.__cause__
E                   prefect.exceptions.PrefectHTTPStatusError: Server error '500 Internal Server Error' for url '<http://ephemeral-prefect/api/task_runs/>'
E                   Response: {'exception_message': 'Internal Server Error'}
E                   For more information check: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500>
E                   ----------------------------
E                   Traceback (most recent call last):
E                     File "/Users/psktam/Library/Caches/pypoetry/virtualenvs/project-RcQVLUoH-py3.10/lib/python3.10/site-packages/prefect/engine.py", line 1389, in create_task_run_then_submit
E                       task_run = await create_task_run(
E                     File "/Users/psktam/Library/Caches/pypoetry/virtualenvs/project-RcQVLUoH-py3.10/lib/python3.10/site-packages/prefect/engine.py", line 1434, in create_task_run
E                       task_run = await flow_run_context.client.create_task_run(
E                     File "/Users/psktam/Library/Caches/pypoetry/virtualenvs/project-RcQVLUoH-py3.10/lib/python3.10/site-packages/prefect/client/orchestration.py", line 1980, in create_task_run
E                       response = await <http://self._client.post|self._client.post>(
E                     File "/Users/psktam/Library/Caches/pypoetry/virtualenvs/project-RcQVLUoH-py3.10/lib/python3.10/site-packages/httpx/_client.py", line 1848, in post
E                       return await self.request(
E                     File "/Users/psktam/Library/Caches/pypoetry/virtualenvs/project-RcQVLUoH-py3.10/lib/python3.10/site-packages/httpx/_client.py", line 1530, in request
E                       return await self.send(request, auth=auth, follow_redirects=follow_redirects)
E                     File "/Users/psktam/Library/Caches/pypoetry/virtualenvs/project-RcQVLUoH-py3.10/lib/python3.10/site-packages/prefect/client/base.py", line 312, in send
E                       response.raise_for_status()
E                     File "/Users/psktam/Library/Caches/pypoetry/virtualenvs/project-RcQVLUoH-py3.10/lib/python3.10/site-packages/prefect/client/base.py", line 164, in raise_for_status
E                       raise PrefectHTTPStatusError.from_httpx_error(exc) from exc.__cause__
E                   prefect.exceptions.PrefectHTTPStatusError: Server error '500 Internal Server Error' for url '<http://ephemeral-prefect/api/task_runs/>'
E                   Response: {'exception_message': 'Internal Server Error'}
E                   For more information check: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500>

../Library/Caches/pypoetry/virtualenvs/project-RcQVLUoH-py3.10/lib/python3.10/site-packages/anyio/_backends/_asyncio.py:595: ExceptionGroup
m
The error you're encountering seems to be related to the way you're calling your flow in the test. In Prefect 2.0, the way to define and run flows has changed. The
with Flow()
syntax and
flow.run()
are now obsolete. In your test case, you're using
prefect_test_harness()
which is also not the recommended way to test flows in Prefect 2.0. Instead, you should define your flows with the
@flow
decorator and tasks with the
@task
decorator. To run a flow, you just call the flow directly instead of using
my_flow.run()
. So, your test case should look something like this:
Copy code
from my_project import flow_to_test

def test_flow():
    flow_to_test()
This should resolve the issue you're facing. If you're still encountering problems, it might be helpful to check the Prefect documentation or community resources for more information.
p
I don't think that's correct? Looking at the documentation, it recommends to use the
prefect_test_harness()
command to test flows with: https://docs.prefect.io/2.14.4/guides/testing/
maybe one thing to note, however, is that my
flow_to_test
flow also invokes some subflows. Could that have something to do with this error?
when I try this out using just a very simple flow taht doesn't call any other flows (or even any other tasks) it works as advertised
do I need to start a local server in order to run this? I was hoping that I wouldn't have to do that
n
do I need to start a local server in order to run this? I was hoping that I wouldn't have to do that
no you dont, i would just have an autouse fixture like this
p
oh interesting
n
im not sure off the top why your example above isnt working tho 🧐
p
I was going to ask how this is different from using
prefect_test_harness()
with the
with
statement though?
n
it shouldnt be, it should be roughly the same thing
p
I see
from what I can tell it only seems to happen when I call flows that have subflows in them
and the
task_runner
for the parent flow uses
DaskTaskRunner
n
ahh - think dask might be the issue. is it important that your unit tests use dask?
id have to look more at how exactly that context manager works - i can try reproducing a minimal example later
p
it's not important that the unit tests use dask, although it'd definitely help speed it up
well, actually, I think we do need to use the dask task runner, given a quirk with the way one of our tasks are set up. the tl;dr is we need new processes to spawn for each task in that flow