AttributeError:'模块'对象没有属性' TestSpider'

时间:2016-10-05 20:49:38

标签: python python-unittest

我正在尝试使用python中的unittest,但是我收到以下错误:AttributeError: 'module' object has no attribute 'TestSpider'我的测试代码如下

import unittest

from spiders.eshopsite import EshopsiteSpider

### Unit Test for the EshopsiteSpider class

class TestSpider(unittest.TestCase):

    def testExtractAnchor(self):
        testArray = [u'/test1', u'/test1/test2', u'test2']
        testObj = EshopsiteSpider()
        testOutputArray = testObjextractAnchors(testArray)
        self.assertEqual(len(testArray), len(testOutputArray))
        index = 0
        ### A loop is needed to compare each element in the
        ### arrays. To assert if they are equal
        while index < len(testOutputArray):
            varOne = testOutputArray[index]
            varTwo = testArray[index].encode('utf8')[1:]
            self.assertEqual(varOne, varTwo)
            index = index + 1



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

和我的文件夹结构:

enter image description here

完整的堆栈跟踪:

Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/runpy.py", line 170, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/__main__.py", line 18, in <module>
    main(module=None)
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/main.py", line 92, in __init__
    self.parseArgs(argv)
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/main.py", line 139, in parseArgs
    self.createTests()
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/main.py", line 146, in createTests
    self.module)
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/loader.py", line 146, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/loader.py", line 146, in <listcomp>
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/local/Cellar/python3/3.4.3/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/loader.py", line 114, in loadTestsFromName
    parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute 'TestSpider'

为了执行测试,我使用了以下命令:python3 -m unittest tests/TestSpider

根据其他测试,我无法弄清楚出了什么问题。即使我在测试函数中注释掉了所有内容,除了第一个语句之外,我也会得到同样的错误。

我希望有人可以指出我正确的方向或帮助解决方案。

\拉斯

0 个答案:

没有答案