Skip to content

Instantly share code, notes, and snippets.

@Niccolum
Created September 26, 2019 06:54
Show Gist options
  • Save Niccolum/6bf713bf55c9aeef135d3653ca785752 to your computer and use it in GitHub Desktop.
Save Niccolum/6bf713bf55c9aeef135d3653ca785752 to your computer and use it in GitHub Desktop.
# start_func.py
async def start_func(testcls, first, **second):
second['new_arg'] = 'wow'
await testcls.send(first, **second)
if __name__ == '__main__':
from end_func import TestCls
import asyncio
testargs = {
'bar': 'bar',
'baz': 'baz'
}
cls = TestCls()
loop = asyncio.get_event_loop()
loop.run_until_complete(start_func(cls, 'foo', **testargs))
loop.close()
# end_func.py
class TestCls:
async def send(self, first, **second):
second['newer_arg'] = 'WOOOOOOOOOOOOOOOOO'
await self._send_request(first, **second)
async def _send_request(self, first, **second):
print('nothing')
# test_mock.py
import pytest
from unittest import mock
from start_func import start_func
from end_func import TestCls
import json
class HTTP_mock:
async def _send_request(self, arg, **kwargs):
print(json.dumps([arg, kwargs]))
@pytest.mark.asyncio
async def test_send_mock(capsys):
testargs = {
'bar': 'testbar',
'baz': 'testbaz'
}
cls = TestCls()
with mock.patch('end_func.TestCls._send_request', new=HTTP_mock._send_request) as http_mock:
first_arg = 'footest'
await start_func(cls, 'footest', **testargs)
captured = capsys.readouterr()
res = json.loads(captured.out)
print('captured', res)
assert first_arg == res[0]
assert all(item in res[1].items() for item in testargs.items())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment