我正在测试我构建的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
test_keys
方法中的obj
参数是TestGoogleSearch
的实例,也就是self
。尝试添加self
:或者,只需删除该方法并除去该类
相关问题 更多 >
编程相关推荐