对于scrapy的单元测试,官方文档并没有提到,只是说有一个Contract功能。但是相信我,这个东西真的不好用,甚至scrapy的作者在一个issue中都说到希望删去这个功能。

那么scrapy应该怎么测试呢?

首先我们要明白我们真正想测试的是什么:

  • 我们不是要测试爬虫是否能访问站点!这个应该在你编写爬虫的时候就做到;如果你的代码在运行突然不可以访问站点了,也应该使用sentry这种日志监控系统。
  • 我们要测试parse(), parse_xx()方法是否如预期返回想要的item和request
  • 我们要测试parse()返回的item中字段类型是否正确。尤其是你用了scrapy的processor系统之后

使用betamax进行单元测试

关于betamax的介绍,可以看我的这篇博客

我们实际要做的不仅是单元测试1,还是集成测试2。我们不想每次都重复进行真实的请求,我们不想使用啰嗦的mock

爬虫代码

下面是我们的爬虫代码,这是爬取一个ip代理网站,获取最新发布的ip:

# src/spider.py
import scrapy
from scrapy.loader import ItemLoader
from scrapy.loader.processors import TakeFirst, MapCompose, Join class IPItem(scrapy.Item):
ip = scrapy.Field(
input_processor=MapCompose(str, str.strip),
output_processor=TakeFirst()
)
port = scrapy.Field(
input_processor=MapCompose(str, str.strip),
output_processor=TakeFirst()
)
protocol = scrapy.Field(
input_processor=MapCompose(str, str.strip, str.lower),
output_processor=TakeFirst()
)
remark = scrapy.Field(
input_processor=MapCompose(str, str.strip),
output_processor=Join(separator=', ')
)
source = scrapy.Field(
input_processor=MapCompose(str, str.strip),
output_processor=TakeFirst()
) class IpData5uSpider(scrapy.Spider):
name = 'ip-data5u'
allowed_domains = ['data5u.com']
start_urls = [
'http://www.data5u.com/free/index.shtml',
'http://www.data5u.com/free/gngn/index.shtml',
]
custom_settings = {
'USER_AGENT': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
'DOWNLOAD_DELAY': 1
} def parse(self, response):
for row in response.css('div.wlist ul.l2'):
loader = ItemLoader(item=IPItem(), selector=row)
loader.add_value('source', 'data5u')
loader.add_css('ip', 'span:nth-child(1) li::text')
loader.add_css('port', 'span:nth-child(2) li::text')
loader.add_css('protocol', 'span:nth-child(4) li::text')
loader.add_css('remark', 'span:nth-child(5) li::text')
loader.add_css('remark', 'span:nth-child(5) li::text')
yield loader.load_item()

测试代码

我们使用pytest编写项目的单元测试,首先我们编写一些fixture函数:

# tests/conftest.py
import pathlib
import pytest
from scrapy.http import HtmlResponse, Request import betamax
from betamax.fixtures.pytest import _betamax_recorder # betamax配置,设置betamax录像带的存储位置
cassette_dir = pathlib.Path(__file__).parent / 'fixture' / 'cassettes'
cassette_dir.mkdir(parents=True, exist_ok=True)
with betamax.Betamax.configure() as config:
config.cassette_library_dir = cassette_dir.resolve()
config.preserve_exact_body_bytes = True @pytest.fixture
def betamax_recorder(request):
"""修改默认的betamax pytest fixtures
让它默认可用接口pytest.mark.parametrize装饰器,并且生产不同的录像带.
有些地方可能会用到
"""
return _betamax_recorder(request, parametrized=True) @pytest.fixture
def resource_get(betamax_session):
"""这是一个pytest fixture
返回一个http请求方法,相当于: with Betamax(session) as vcr:
vcr.use_use_cassette('这里是测试函数的qualname')
resp = session.get(url, *args, **kwargs)
# 将requests的Response,封装成scrapy的HtmlResponse
return HtmlResponse(body=resp.content)
"""
def get(url, *args, **kwargs):
request = kwargs.pop('request', None)
resp = betamax_session.get(url, *args, **kwargs)
selector = HtmlResponse(body=resp.content, url=url, request=request)
return selector return get

然后是测试函数:

# tests/test_spider/test_ip_spider.py
from src.spider import IpData5uSpider, IPItem def test_proxy_data5u_spider(resource_get):
spider = IpData5uSpider()
headers = {
'user-agent': spider.custom_settings['USER_AGENT']
} for urlr in spider.start_urls:
selector = resource_get(url, headers=headers, request=req) result = spider.parse(selector)
for item in result:
if isinstance(item, IPItem):
assert isinstance(item['port'], str)
assert item['ip']
assert item['protocol'] in ('http', 'https')
elif isinstance(item, Request):
assert item.url.startswith(req.url)
else:
raise ValueError('yield 输出了意料外的item')

然后我们运行它:

>>> pytest
...
Results (2.12s):
1 passed

我们可以看到fixture目录出现新的文件,类似xxx.tests.test_spiders.test_ip_spider.test_proxy_data5u_spider.json这样的文件名.

再运行一次:

>>> pytest
...
Results (0.56s):
1 passed

测试运行速度明显变快,这是因为这一次使用的是保存在fixture的文件,用它来代替进行真正的http request操作。

另外我们可以看一下fixture中json文件的内容:

{"http_interactions": [{"request": {"body": {"encoding": "utf-8", "base64_string": ""}, "headers": {"user-agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"], "Accept-Encoding": ["gzip, deflate"], "Accept": ["*/*"], "Connection": ["keep-alive"]}, "method": "GET", "uri": "http://www.data5u.com/free/index.shtml"}, "response": {"body": {"encoding": "UTF-8", "base64_string": "H4sIAAAAAAx..."}]}

可以看到这里保存了一个response的全部信息,通过这个response再构造一个request.Response也不是难事吧。这就是betamax的原理。

对scrapy进行单元测试 -- 使用betamax的更多相关文章

  1. Learning Scrapy笔记(三)- Scrapy基础

    摘要:本文介绍了Scrapy的基础爬取流程,也是最重要的部分 Scrapy的爬取流程 Scrapy的爬取流程可以概括为一个方程式:UR2IM,其含义如下图所示 URL:Scrapy的运行就从那个你想要 ...

  2. python爬虫scrapy项目详解(关注、持续更新)

    python爬虫scrapy项目(一) 爬取目标:腾讯招聘网站(起始url:https://hr.tencent.com/position.php?keywords=&tid=0&st ...

  3. scrapy将爬取到的数据存入elasticsearch

    pip安装 elasticsearch-dsl的包, 是elasticsearch提供给python 的接口 if __name__ == "__main__": 这个用来调试,还 ...

  4. scrapy爬取cnblogs文章列表

    scrapy爬取cnblogs文章 目标任务 安装爬虫 创建爬虫 编写 items.py 编写 spiders/cnblogs.py 编写 pipelines.py 编写 settings.py 运行 ...

  5. Scrapy框架爬虫初探——中关村在线手机参数数据爬取

    关于Scrapy如何安装部署的文章已经相当多了,但是网上实战的例子还不是很多,近来正好在学习该爬虫框架,就简单写了个Spider Demo来实践.作为硬件数码控,我选择了经常光顾的中关村在线的手机页面 ...

  6. Intellij idea添加单元测试工具

    1.idea 版本是14.0.0 ,默认带有Junit,但是不能自动生成单元测试,需要下载JunitGererator2.0插件 2.Settings -Plugins,下载 JunitGenerat ...

  7. Python的单元测试(二)

    title: Python的单元测试(二) date: 2015-03-04 19:08:20 categories: Python tags: [Python,单元测试] --- 在Python的单 ...

  8. Python的单元测试(一)

    title: Python的单元测试(一) author: 青南 date: 2015-02-27 22:50:47 categories: Python tags: [Python,单元测试] -- ...

  9. scrapy爬虫docker部署

    spider_docker 接我上篇博客,为爬虫引用创建container,包括的模块:scrapy, mongo, celery, rabbitmq,连接https://github.com/Liu ...

随机推荐

  1. PHP下查询游戏《Minecraft》多人游戏 服务器的人数。

    废话不多说,直接上图: 作为一个优雅的Minecraft服务器,肯定需要官网的嗯. 很多服务器的官网都有显示当前服务器在线人数,延迟,每一个子服在线人数,甚至出了个流量图. 我们不搞花里胡哨的查询人数 ...

  2. VMware中linux虚拟机无法全屏显示的解决方法(1080P分辨率调节)

    安装了linux(这里以Ubuntu为例)后,发现无法全屏,如下图 即使在设置里面也找不到适合我的显示器的分辨率(我的显示器为1080P) 网上很多人都说是因为没装显卡驱动,其实不是这样的.要解决这个 ...

  3. Acwing-201-可见的点(数学, 欧拉函数)

    链接: https://www.acwing.com/problem/content/description/203/ 题意: 在一个平面直角坐标系的第一象限内,如果一个点(x,y)与原点(0,0)的 ...

  4. 【转】分布式文件系统FastDFS架构剖析

    FastDFS是一款类Google FS的开源分布式文件系统,它用纯C语言实现,支持Linux.FreeBSD.AIX等UNIX系统.它只能通过专有API对文件进行存取访问,不支持POSIX接口方式, ...

  5. 题解 【USACO 4.2.1】草地排水

    [USACO 4.2.1]草地排水 Description 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫 ...

  6. Axure RP 8 破解汉化(实测可用)

    Axure的安装教程初学乍练,从安装应用程序开始1.下载 下载地址:https://www.axure.com.cn/3510/ 2.破解:注册码(源于博主“前端大白兔”,原博客网址:http://b ...

  7. 用jstl的if或when标签判断字符串是否为空

    在jsp页面用到jstl的if或when标签判断字符串不为空的时候,书写格式: <c:when test="${not empty paramName}"> </ ...

  8. BZOJ 3782: 上学路 Lucas+ExCRT+容斥+dp

    其实呢,扩展中国剩余定理还有一种理解方式:就是你有一坨东西,形如:$A[i]\equiv B[i](mod$ $P[i])$. 对于这个东西,你可以这么思考:如果最后能求出一个解,那么这个解的增量一定 ...

  9. Hibernate 5 的模块/包(modules/artifacts)

    Hibernate 的功能被拆分成一系列的模块/包(modules/artifacts),其目的是为了对依赖进行独立(模块化). 模块名称 说明 hibernate-core 这个是 Hibernat ...

  10. noi.ac #536 打地鼠

    题目链接:戳我 [问题描述] 小A在玩打地鼠游戏.有一个n×m的网格,每个位置上地鼠都会要么冒出头要么缩进去.地鼠很狡猾,每次小A选一个地鼠冒出头的格子(x,y)把它打下去,但同一行同一列的地鼠全都会 ...