Skip to content
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

Support pytest_aiohttp #26

Closed
ikhlestov opened this issue Mar 4, 2019 · 6 comments · Fixed by #27
Closed

Support pytest_aiohttp #26

ikhlestov opened this issue Mar 4, 2019 · 6 comments · Fixed by #27

Comments

@ikhlestov
Copy link
Contributor

At the present pytest_aiohttp doesn't work correct with pytest_asyncio (pytest_aiohttp issue and pytest_asyncio issue ).
On the other hand aresponses doesn't require pytest_asyncio directly and can be executed without it. But aresponses depends on event_loop fixture here. So the main questions - can we use existing fixture event_loop from asyncio or loop from aiohttp on the fly for the aresponses fixture initialization?

"""Test script to show aresponses work without pytest_asyncio
Steps:
- install all required packages as `pip install pytest aiohttp pytest_aiohttp aresponses`
- run script as `pytest test_example.py -p no:asyncio
"""
import pytest
import aiohttp


# Simple server definition
async def handle(request):
    return aiohttp.web.Response(text="test response")


def create_app():
    app = aiohttp.web.Application()
    app.add_routes([aiohttp.web.get('/', handle)])
    return app


# Tests
@pytest.fixture
async def client(aiohttp_client):
    app = create_app()
    return await aiohttp_client(app)


async def test_root_url(client):
    resp = await client.get('/')
    assert resp.status == 200
    assert await resp.text() == "test response"


# this additional fixture required by aresponses
# by default aresponses depends on `event_loop` fixture defined at the pytest_asyncio
@pytest.fixture
def event_loop(loop):
    return loop


async def test_aresponses_without_aiohttp(aresponses):
    aresponses.add('foo.com', '/', 'get', 'hi there!!')
    url = 'http://foo.com'
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            text = await response.text()
            assert text == 'hi there!!'
@brycedrennan
Copy link
Collaborator

If you just define your own fixture in conftest.py will that work?

from aresponses import ResponsesMockServer

@pytest.fixture
async def aresponses(loop):
    async with ResponsesMockServer(loop=loop) as server:
        yield server

@ikhlestov
Copy link
Contributor Author

Yes, it works, thank you. I think it's a nice resolution. Should I create a PR with readme update with additional example for aresponses+aiohttp_client or there is no reason for it?

@brycedrennan
Copy link
Collaborator

That would be great thank you!

@XayOn
Copy link

XayOn commented Mar 10, 2019

Yet pytest-asyncio is included in the setup.py as installs_requires.
Thus provoking the aformentioned bug ( aio-libs/pytest-aiohttp#8 ).

@XayOn
Copy link

XayOn commented Mar 10, 2019

Also, latest pytest releases will await coroutines just fine without pytest-asyncio.

@brycedrennan
Copy link
Collaborator

@XayOn do you have some documentation that shows pytest now supports asyncio? I found this which implies it doesn’t.
pytest-dev/pytest#2175

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants