小编典典

Scrapy Unit的单元测试

scrapy

我想在Scrapy(屏幕抓取工具/网络抓取工具)中实现一些单元测试。由于项目是通过“ scrapy crawl”命令运行的,所以我可以通过诸如“鼻子”之类的东西来运行它。由于scrapy建立在扭曲之上,我可以使用其单元测试框架Trial吗?如果是这样,怎么办?否则,我想获得的鼻子工作。

更新:

我一直在谈论Scrapy-Users,我想我应该“在测试代码中构建Response,然后使用该响应调用该方法并断言[I]在输出中获得了预期的项目/请求”。我似乎无法使它正常工作。

我可以在测试中构建单元测试类:

创建一个响应对象
尝试使用响应对象调用我的Spider的parse方法
但是,最终会生成此回溯。为什么有任何见解?


阅读 781

收藏
2020-04-08

共1个答案

小编典典

我这样做的方法是创建虚假响应,这样你就可以脱机测试解析函数。但是你可以通过使用真实的HTML获得真实的情况。

这种方法的问题是你的本地HTML文件可能无法反映在线的最新状态。因此,如果HTML在线更改,你可能会遇到一个大错误,但是你的测试用例仍会通过。因此,这可能不是测试此方法的最佳方法。

我当前的工作流程是,每当出现错误时,我都会使用url向管理员发送电子邮件。然后针对该特定错误,创建一个html文件,其中包含引起错误的内容。然后,我为此创建一个单元测试。

这是我用来创建示例Scrapy http响应以从本地html文件进行测试的代码:

# scrapyproject/tests/responses/__init__.py

import os

from scrapy.http import Response, Request

def fake_response_from_file(file_name, url=None):
    """
    Create a Scrapy fake HTTP response from a HTML file
    @param file_name: The relative filename from the responses directory,
                      but absolute paths are also accepted.
    @param url: The URL of the response.
    returns: A scrapy HTTP response which can be used for unittesting.
    """
    if not url:
        url = 'http://www.example.com'

    request = Request(url=url)
    if not file_name[0] == '/':
        responses_dir = os.path.dirname(os.path.realpath(__file__))
        file_path = os.path.join(responses_dir, file_name)
    else:
        file_path = file_name

    file_content = open(file_path, 'r').read()

    response = Response(url=url,
        request=request,
        body=file_content)
    response.encoding = 'utf-8'
    return response

样本html文件位于scrapyproject / tests / responses / osdir / sample.html中

然后,测试用例可能如下所示:测试用例的位置为scrapyproject / tests / test_osdir.py

import unittest
from scrapyproject.spiders import osdir_spider
from responses import fake_response_from_file

class OsdirSpiderTest(unittest.TestCase):

    def setUp(self):
        self.spider = osdir_spider.DirectorySpider()

    def _test_item_results(self, results, expected_length):
        count = 0
        permalinks = set()
        for item in results:
            self.assertIsNotNone(item['content'])
            self.assertIsNotNone(item['title'])
        self.assertEqual(count, expected_length)

    def test_parse(self):
        results = self.spider.parse(fake_response_from_file('osdir/sample.html'))
        self._test_item_results(results, 10)

基本上,这就是我测试解析方法的方式,但不仅限于解析方法。

2020-04-08