Scrapy Unit Testing

前端 未结 10 1493
逝去的感伤
逝去的感伤 2020-11-30 18:18

I\'d like to implement some unit tests in a Scrapy (screen scraper/web crawler). Since a project is run through the \"scrapy crawl\" command I can run it through something

10条回答
  •  伪装坚强ぢ
    2020-11-30 18:23

    Similar to Hadrien's answer but for pytest: pytest-vcr.

    import requests
    import pytest
    from scrapy.http import HtmlResponse
    
    @pytest.mark.vcr()
    def test_parse(url, target):
        response = requests.get(url)
        scrapy_response = HtmlResponse(url, body=response.content)
        assert Spider().parse(scrapy_response) == target
    
    

提交回复
热议问题