0
class BackgroundProcess:

    def background(f):
        from functools import wraps
        @wraps(f)
        def wrapped(*args, **kwargs):
            loop = asyncio.get_event_loop()
            if callable(f):
                return loop.run_in_executor(None, f, *args, **kwargs)
            else:
                raise TypeError('Task must be a callable')    
        return wrapped

class Monitoring

    @BackgroundProcess.background
    def post_monitoring_event_in_background() 
             return ''

How to write test cases for post_monitoring_event_in_background method by mocking @BackgroundProcess.background

quamrana
  • 37,849
  • 12
  • 53
  • 71
Vishnuraj
  • 11
  • 2
  • Are you asking how to test the decorator, or how to patch it out to test the decorated function synchronously? Note that the decorator is defined incorrectly because it’s an instance method with no `self` arg; you probably want a `@staticmethod` on there. – Samwise Jul 05 '23 at 16:33
  • Need to write test case for post_monitoring_event_in_background() but while calling post_monitoring_event_in_background() method in unittest ,it is going to decorator @BackgroundProcess.background method. – Vishnuraj Jul 05 '23 at 16:37
  • @Samwise: The decorator 'works' as intended due to python class/method access. (Not the usual way of using a class, but it works) – quamrana Jul 05 '23 at 16:49
  • as my paw-paw used to say, just because it works don’t make it right. – Samwise Jul 05 '23 at 16:58

0 Answers0