Hi everyone, I am trying to write functional test...
# prefect-community
d
Hi everyone, I am trying to write functional tests for my code [not prefect] and I want to bypass the prefect decorators [task and flow] so I can write tests for my codes only. Is there any easy way of doing so WITH support of
with_options
methods?
t
I have found a nice usage pattern of
Copy code
def my_func(a):
    return a*100

task_my_func = task(my_func)
This then lets me separate out my function for unit tests, and gives me a bit of a reminder when I am using the
task_my_func
in my prefect code that it is delayed and should be called as such. Otherwise, I believe that a
task
decorated function can still be access. It is something like
Copy code
task_my_func.fn
which might be useful for your unit tests
d
I dont think this works well with overwriting task params with the usages of
with_options
Copy code
from unittest import mock
import logging


def prefect_task_patcher():
    def wrapper(*_args, **_kwargs):
        class PrefectTaskPatcher:
            def __call__(self, *call_args, **call_kwargs):
                return self.fn(*call_args, **call_kwargs)

            def __init__(self, fn):
                self.fn = fn

            def with_options(self, *_options_args, **_options_kwargs):
                return self

        return PrefectTaskPatcher

    return wrapper


mock_task = mock.patch("prefect.task", new_callable=prefect_task_patcher)
mock_logs = mock.patch("prefect.get_run_logger", returns=logging)
I m trying this instead.