aioresponses icon indicating copy to clipboard operation
aioresponses copied to clipboard

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

Open rnusser opened this issue 2 years ago • 3 comments

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

rnusser avatar Aug 04 '22 11:08 rnusser

Same problem here:

Python 3.9.13
aiohttp 3.8.1
aioresponses 0.7.3
pytest 7.1.2

Pacheco95 avatar Aug 29 '22 11:08 Pacheco95

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.

marcinsulikowski avatar Oct 19 '22 19:10 marcinsulikowski