Scrapy:测试内联请求的有效方法

inf*_*234 5 python unit-testing scrapy

我使用scrapy-inline-requests库编写了一个蜘蛛。所以我的蜘蛛中的 parse 方法看起来像这样:

@inline_requests
def parse(self, response1):
    item = MyItem()
    loader = ItemLoader(item=item, response=response1)

    #extracting some data from the response1

    try:
        response 2 = yield Request(some_url)
        #extracting some other data from response2
    except Exception:
            self.logger.warning("Failed request to: %s", some_url)

    yield loader.load_item()
Run Code Online (Sandbox Code Playgroud)

我想有效地测试这种方法。我可以轻松编写一个测试,在其中创建一个假模拟 response1 并将其传递给函数。但是,我不知道如何模拟 response2 并使用来自虚假响应的数据获取完整项目。你有什么建议吗?

小智 0

可能有点晚了,但请查看 scrapy-inline-requests 的 github 存储库中的测试: https: //github.com/rmax/scrapy-inline-requests/blob/master/tests/test_inline_requests.py

基本上,按照回调将产生这些响应的请求的顺序向回调提供响应对象列表。