Hey community, Does anyone have a nice way to tes...
# ask-community
b
Hey community, Does anyone have a nice way to test functions that are decorated as a
task
with arguments? I want to test the functions logic but unfortunately when
pytest
imports the function it is calling the decorators logic for the
result=
argument, which calls an external dependancy. I have tried to mock the task decorator but I cant seem to crack it.
a
Try
result = mytask.run()
b
Yep, I have been doing that. That invokes the decorator before running the class
Where the whole issue is....
k
Hey, I’ll check this some members today about all the testing questions
👍 1
z
Hey Ben, could you show me what your decorator looks like? Mocking the task decorator doesn't seem like best practice here. You should be mocking the external dependency your result type depends on.
b
My decorator just is
@task(result=Backup.bucket()
where Backup.bucket just returns an S3Result object. I have tried mocking that out, but from my investigation it appears as though decorators mocking works differently and I couldn't manage to get it working.
Within that bucket method there's a call to S3.
The problem is its wrapped at function definition time. https://stackoverflow.com/a/7667621/10959670
I did get around this by separating out all of my tasks as functions and re wrapping them in another file with the
@task
decorator. At first it didn't seem like the best approach, but it has the added benefit of now the common tasks can be shared between flows more easily @Zanie
z
Hey Ben, glad that is working out for you. I think that's the best way to do it. For the mocking, you could likely include mock patch directly in your conftest.py which should be applied before the import of the function in your test file itself.
I'm a bit confused about the task decorator mocking bit still because here your external call is to S3 and you'll want to just mock the boto client or your
Backup.bucket()
implementation
b
Yeah I tried that, unfortunately with no success.