2011-05-28 47 views
57

我不確定我是否正確地組織了我的包結構,或者在setup.py中使用了正確的選項,因爲我在嘗試運行單元測試時出錯。Python包結構,運行單元測試的setup.py

我有這樣的結構:

/project 
    /bin 
    /src 
     /pkgname   
      __init__.py 
      module1.py 
      module2.py 
    /tests 
     __init__.py 
     test1.py 
     test2.py 

我setup.py看起來是這樣的:

#!/usr/bin/env python                                   
from setuptools import setup, find_packages 

setup(version='0.1', 
     description='Trend following library', 
     author='Nate Reed', 
     author_email='[email protected]', 
     packages=find_packages(), 
     install_requires=['numpy'], 
     test_suite="tests",       
) 

當我運行 '蟒蛇setup.py測試' 我得到:

[email protected]:~/PycharmProjects/trendfollowing$ sudo python setup.py test 
running test 
running egg_info 
writing requirements to UNKNOWN.egg-info/requires.txt 
writing UNKNOWN.egg-info/PKG-INFO 
writing top-level names to UNKNOWN.egg-info/top_level.txt 
writing dependency_links to UNKNOWN.egg-info/dependency_links.txt 
reading manifest file 'UNKNOWN.egg-info/SOURCES.txt' 
writing manifest file 'UNKNOWN.egg-info/SOURCES.txt' 
running build_ext 
Traceback (most recent call last): 
    File "setup.py", line 11, in <module> 
    test_suite="tests", 
    File "/usr/lib/python2.6/distutils/core.py", line 152, in setup 
    dist.run_commands() 
    File "/usr/lib/python2.6/distutils/dist.py", line 975, in run_commands 
    self.run_command(cmd) 
    File "/usr/lib/python2.6/distutils/dist.py", line 995, in run_command 
    cmd_obj.run() 
    File "/usr/lib/python2.6/dist-packages/setuptools/command/test.py", line 137, in run 
    self.with_project_on_sys_path(self.run_tests) 
    File "/usr/lib/python2.6/dist-packages/setuptools/command/test.py", line 117, in with_project_on_sys_path 
    func() 
    File "/usr/lib/python2.6/dist-packages/setuptools/command/test.py", line 146, in run_tests 
    testLoader = loader_class() 
    File "/usr/lib/python2.6/unittest.py", line 816, in __init__ 
    self.parseArgs(argv) 
    File "/usr/lib/python2.6/unittest.py", line 843, in parseArgs 
    self.createTests() 
    File "/usr/lib/python2.6/unittest.py", line 849, in createTests 
    self.module) 
    File "/usr/lib/python2.6/unittest.py", line 613, in loadTestsFromNames 
    suites = [self.loadTestsFromName(name, module) for name in names] 
    File "/usr/lib/python2.6/unittest.py", line 587, in loadTestsFromName 
    return self.loadTestsFromModule(obj) 
    File "/usr/lib/python2.6/dist-packages/setuptools/command/test.py", line 34, in loadTestsFromModule 
    tests.append(self.loadTestsFromName(submodule)) 
    File "/usr/lib/python2.6/unittest.py", line 584, in loadTestsFromName 
    parent, obj = obj, getattr(obj, part) 
AttributeError: 'module' object has no attribute 'test1' 

測試名稱是否需要匹配模塊名稱?我的包裝結構中還需要遵守其他約定嗎?

回答

47

通過一些試驗和錯誤,我找到了這個問題的原因。測試名稱應匹配模塊名稱。如果有「foo_test.py」測試,則需要有一個相應的模塊foo.py。

我發現了一些guidelines on organizing package structure,這讓我重新安排我封裝成一個結構我是自信。