pytest夹具在请求输入/输出时工作不正常

2024-10-01 19:18:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在测试我构建的web scraper是否仍然从网站最新的HTML结构返回正确的数据。这需要发出一个出站HTTP请求(我不想模拟它),并删除响应

当我将刮取的HTML数据(转换为JSON)格式化并尝试访问它时,pytest抛出一个错误

AttributeError:“TestGoogleSearch”对象在我尝试从设备访问响应时没有属性“keys”。与:

obj=<;tests.test_google_search.TestGoogleSearch对象位于0x7fd073eae0d0>; target={'google\u query\u 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',…}

代码如下:

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



Tags: httpscomobjurltargetsearchpytestwww
1条回答
网友
1楼 · 发布于 2024-10-01 19:18:43

test_keys方法中的obj参数是TestGoogleSearch的实例,也就是self。尝试添加self

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

或者,只需删除该方法并除去该类

相关问题 更多 >

    热门问题