-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to patch with decorator using unittest_run_loop #4695
Comments
This is the first time I see anybody using unittest and not pytest... Feel free to send a PR and we'll see what we can do. This looks like it may be a problem of |
Yes, I see that many people prefer pytest over unittest, but I like to keep things simple and not import new dependencies and using new frameworks when python has them built in. Changing the order of decorators does not help. What I discovered is that it is a problem of make_mocked_coro since I found out that from python 3.8 unittest is asyncio capable with testcases, mocks and so on. Maybe it can be added in the documentation that this package helps people that want to keep using unittest and do not have python 3.8 yet, describing how to use it. I could send a small example to show how to use it and if it fits a PR as well. I would also suggest subclassing asynctest.TestCase from the same library and adding TestServer and TestClient since it also comes with cleaner asyncio testing capabilities. I would be glad to improve the testing in this manner, but I also know, it is new library to add to the dependencies and that python3.8 is covered by python itself. Here is the example by the way. from functools import wraps
from typing import Any
from aiohttp.test_utils import AioHTTPTestCase, 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
from asynctest.mock import patch
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') 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')
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) |
Feel free to send a docs PR then. P.S. I personally recommend you to try out pytest anyway because it's easier to use and it's much more powerful in many ways. |
Sure, closing. |
🐞 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
💡 Expected behavior
I expect to be able to use decorators to patch or mock a module/function
📋 Logs/tracebacks
📋 Your version of the Python
📋 Your version of the aiohttp/yarl/multidict distributions
📋 Additional context
macOS catalina
aiohttp - server
The text was updated successfully, but these errors were encountered: