I want to use asyncio together with pytest.
here is what I want to do:
- run a server while my test is running - stop the server when it is finished
- in a perfect world I would implement the server as a fixture (using yield)
I like to write test code like this:
def test_add(svr_fixture):
await asyncio.sleep(100)
assert m.add(1, 2) == 3 # I like the readability of this and want to restore it
I tried to write the fixture with pytest-asyncio (https://pypi.python.org/pypi/pytest-asyncio) but could not figure out how to do this.
What I came up with it this test (works but it looks clumsy and disguises the intention of the test):
def test_add():
async def do_it():
await asyncio.sleep(100)
return m.add(1, 2)
loop = asyncio.get_event_loop()
coro = loop.create_server(server.ServerProtocol, '127.0.0.1', 8023)
asyncio.async(coro)
res = loop.run_until_complete(do_it())
assert res == 3
Any help on how to extract the server code into a fixture like a link to docs or a sample would be much appreciated.
I do not think the complete server code is necessary (but it is here: https://stackoverflow.com/a/48277838/570293)