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

running the first example, as a decorator - fixture 'mocked' not found #218

Open
rnusser opened this issue Aug 4, 2022 · 3 comments
Open

Comments

@rnusser
Copy link

rnusser commented Aug 4, 2022

If I run the first example, as it is, I get the error:

fixture 'mocked' not found

However if placed in a class it works:

import aiohttp
import asyncio
from aioresponses import aioresponses
from unittest import TestCase

class ClientTest(TestCase):
    @aioresponses()
    def test_request(self, mocked):
        loop = asyncio.get_event_loop()
        mocked.get('http://example.com', status=200, body='test')
        session = aiohttp.ClientSession()
        resp = loop.run_until_complete(session.get('http://example.com'))

        assert resp.status == 200

Python 3.10.4
aiohttp 3.8.1
aioresponses 0.7.3
pytest 7.1.2

@Pacheco95
Copy link

Same problem here:

Python 3.9.13
aiohttp 3.8.1
aioresponses 0.7.3
pytest 7.1.2

@marcinsulikowski
Copy link
Contributor

Pytest does a lot of magic things behind the scenes with test functions that take arguments. With pytest it'd be better to use Pytets fixtures instead of the @aioresponses decorator:

import asyncio

import aiohttp
import pytest
from aioresponses import aioresponses


@pytest.fixture
def mocked():
    with aioresponses() as m:
        yield m


def test_request(mocked):
    loop = asyncio.get_event_loop()
    mocked.get('http://example.com', status=200, body='test')
    session = aiohttp.ClientSession()
    resp = loop.run_until_complete(session.get('http://example.com'))
    assert resp.status == 200

Looks like the README should be updated.

@mxr
Copy link

mxr commented Oct 16, 2024

On my end I have to specify ClientSession(loop=loop) for the examples to work, otherwise this line crashes with RuntimeError: no running event loop. Is that expected? I can post more about my environment if not

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

No branches or pull requests

4 participants