2015-11-02 24 views
0

我想尝试一些方法在我的蜘蛛。 例如在我的项目,我有这样的模式:如何测试我的scrapy方法与unitest类

toto/ 
├── __init__.py 
├── items.py 
├── pipelines.py 
├── settings.py 
├── spiders 
│ ├── __init__.py 
│ └── mySpider.py 
└── Unitest 
    └── unitest.py 

unitest.py样子说:

# -*- coding: utf-8 -*-               
import re 
import weakref 
import six 
import unittest 
from scrapy.selector import Selector 
from scrapy.crawler import Crawler 
from scrapy.utils.project import get_project_settings 
from unittest.case import TestCase 
from toto.spiders import runSpider 

class SelectorTestCase(unittest.TestCase): 

    sscls = Selector 

    def test_demo(self): 
    print "test" 

if __name__ == '__main__': 
    unittest.main() 

和我mySpider.py,看起来像:

import scrapy 

class runSpider(scrapy.Spider): 
    name = 'blogspider' 
    start_urls = ['http://blog.scrapinghub.com'] 

    def parse(self, response): 
     for url in response.css('ul li a::attr("href")').re(r'.*/\d\d\d\d/\d\d/$'): 
      yield scrapy.Request(response.urljoin(url), self.parse_titles) 

    def parse_titles(self, response): 
     for post_title in response.css('div.entries > ul > li a::text').extract(): 
      yield {'title': post_title} 

在我UNITEST .py文件,我怎么能打电话给我的蜘蛛? 我想在我的unitest.py文件中添加from toto.spiders import runSpider,但是却没有...... 我得到这个错误:

Traceback (most recent call last): File "unitest.py", line 10, in from toto.spiders import runSpider ImportError: No module named toto.spiders

我怎样才能解决这个问题?

+0

* “但却[原文]它不......” * - 什么?请更具体地了解正在发生的事情。请注意,测试目录通常会在您的包的顶层之外*参见http://www.jeffknupp.com/blog/2013/08/16/open-sourcing-a-python-project-the-right-way/ – jonrsharpe

+0

runSpider在哪里? – Harsha

+0

我编辑我的quatsion –

回答

1

尝试:

import sys 
import os 
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), '../..')) #2 folder back from current file 

from spiders.mySpider import runSpider 
+0

在运行时攻击'sys.path'表明你做错了什么。 – jonrsharpe

+0

那么最新的选择? – Harsha

+0

实际上*在开发模式下安装*包,如果它仍然是实践中的工作,那么您可以测试它,因为它会出现在CI /客户端安装上,而不依赖于相对目录位置。 – jonrsharpe