Skip to content

Unable to patch with decorator using unittest_run_loop #4695

Closed
@WisdomPill

Description

🐞 Describe the bug

patch using decorators is not possible while using unittest_run_loop,
to overcome this issue patching with context manager is needed, it can be a real pain to do it multiple times in the same test case. The arguments are not passed to the unittest test case method.

💡 To Reproduce

from unittest.mock import patch

from aiohttp.test_utils import AioHTTPTestCase, make_mocked_coro, unittest_run_loop
from aiohttp.web_app import Application
from aiohttp.web_request import Request
from aiohttp.web_response import Response
from aiohttp.web_routedef import get


async def do_something():
    print('something')


async def ping(request: Request) -> Response:
    await do_something()
    return Response(text='pong')


class TestApplication(AioHTTPTestCase):
    def get_app(self) -> Application:
        app = Application()
        app.router.add_routes([
            get('/ping/', ping)
        ])

        return app

    @unittest_run_loop
    async def test_ping(self):
        resp = await self.client.get('/ping/')

        self.assertEqual(resp.status, 200)
        self.assertEqual(await resp.text(), 'pong')

    @unittest_run_loop
    async def test_ping_mocked_do_something(self):
        with patch('test_bug.do_something', make_mocked_coro()) as do_something_patch:
            resp = await self.client.get('/ping/')

            self.assertEqual(resp.status, 200)
            self.assertEqual(await resp.text(), 'pong')

            self.assertTrue(do_something_patch.called)

    @unittest_run_loop
    @patch('test_bug.do_something', make_mocked_coro())
    async def test_ping_mocked_do_something_decorated(self, do_something_patch):
        resp = await self.client.get('/ping/')

        self.assertEqual(resp.status, 200)
        self.assertEqual(await resp.text(), 'pong')

        self.assertTrue(do_something_patch.called)

💡 Expected behavior

I expect to be able to use decorators to patch or mock a module/function

📋 Logs/tracebacks

something


Ran 3 tests in 0.080s

FAILED (errors=1)

Error
Traceback (most recent call last):
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 628, in run
    testMethod()
  File "/Users/anas/.envs/timing_router/lib/python3.7/site-packages/aiohttp/test_utils.py", line 488, in new_func
    func(self, *inner_args, **inner_kwargs))
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 1255, in patched
    return func(*args, **keywargs)
TypeError: test_ping_mocked_do_something_decorated() missing 1 required positional argument: 'do_something_patch'

📋 Your version of the Python

$ python --version
Python 3.7.6

📋 Your version of the aiohttp/yarl/multidict distributions

$ python -m pip show aiohttp
Name: aiohttp
Version: 3.6.2
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author: Nikolay Kim
Author-email: fafhrd91@gmail.com
License: Apache 2
Location: /Users/anas/.envs/timing_router/lib/python3.7/site-packages
Requires: chardet, yarl, attrs, multidict, async-timeout
Required-by: 
$ python -m pip show multidict
Name: multidict
Version: 4.7.5
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache 2
Location: /Users/anas/.envs/timing_router/lib/python3.7/site-packages
Requires: 
Required-by: yarl, aiohttp
$ python -m pip show yarl
Name: yarl
Version: 1.4.2
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl/
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache 2
Location: /Users/anas/.envs/timing_router/lib/python3.7/site-packages
Requires: idna, multidict
Required-by: aiohttp

📋 Additional context

macOS catalina
aiohttp - server

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions