Search code examples
pythonpython-requestspytestfixtures

pytest fixture not working properly with requests input/output


I am testing if a web scraper I built still returns the correct data from a website's most current HTML structure. This requires making an outbound HTTP request (I don't want to mock it), and scraping the response.

When I fixture-ize the scraped HTML data (converted to JSON), and try to access it, pytest throws an error saying

AttributeError: 'TestGoogleSearch' object has no attribute 'keys' when I try to access the response from the fixture. With:

obj = <tests.test_google_search.TestGoogleSearch object at 0x7fd073eae0d0> target = {'google_query_url': 'https://www.google.com/search?q=GUSTAV%27S+BARGARTEN+97303+google+maps&oq=%7B%27q%27%3A+%22GUSTAV%27S+BARGARTEN+97303+google+maps%22%7D&sourceid=chrome&ie=UTF-8', ...}

Code below:

url_1 = 'https://www.google.com/search?q=GUSTAV%27S+BARGARTEN+97303+google+maps&oq=%7B%27q%27%3A+%22GUSTAV%27S+BARGARTEN+97303+google+maps%22%7D&sourceid=chrome&ie=UTF-8'

google_search_json = make_google_search_request(url_1)

url_1_target = {
    'google_query_url': 'https://www.google.com/search?q=GUSTAV%27S+BARGARTEN+97303+google+maps&oq=%7B%27q%27%3A+%22GUSTAV%27S+BARGARTEN+97303+google+maps%22%7D&sourceid=chrome&ie=UTF-8'
     # ...
}

@pytest.fixture
def obj():
    yield google_search_json['google_search_results']


@pytest.fixture
def target():
    yield url_1_target


class TestGoogleSearch:

    def test_keys(obj, target):
        assert sorted(obj.keys()) == sorted(target.keys())

    # ...other tests



Solution

  • The obj parameter in the test_keys method is the instance of the TestGoogleSearch, aka self. Try adding self::

    def test_keys(self, obj, target):
        assert sorted(obj.keys()) == sorted(target.keys())
    

    Alternatively, just dedent the method and get rid of the class.