我有一个包含相当广泛的测试套件的软件包,我维护的变化频率非常低.有时我会忘记安装测试所需的组件,或者我的更改会破坏测试代码.而且经常发生这种情况时,setuptools会导致隐藏问题的真正原因.
这是一个例子:
$python setup.py test
running test
running egg_info
writing requirements to pwman3.egg-info/requires.txt
writing pwman3.egg-info/PKG-INFO
writing top-level names to pwman3.egg-info/top_level.txt
writing dependency_links to pwman3.egg-info/dependency_links.txt
writing entry points to pwman3.egg-info/entry_points.txt
reading manifest file 'pwman3.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'pwman3.egg-info/SOURCES.txt'
running build_ext
Traceback (most recent call last):
File "setup.py", line 361, in <module>
'console_scripts': ['pwman3 = pwman.ui.cli:main']
File "/usr/lib64/python2.7/distutils/core.py", line 151, in setup
dist.run_commands()
File "/usr/lib64/python2.7/distutils/dist.py", line 953, in run_commands
self.run_command(cmd)
File "/usr/lib64/python2.7/distutils/dist.py", line 972, in run_command
cmd_obj.run()
File "/usr/lib64/python2.7/site-packages/setuptools/command/test.py", line 146, in run
self.with_project_on_sys_path(self.run_tests)
File "/usr/lib64/python2.7/site-packages/setuptools/command/test.py", line 127, in with_project_on_sys_path
func()
File "/usr/lib64/python2.7/site-packages/setuptools/command/test.py", line 167, in run_tests
testRunner=self._resolve_as_ep(self.test_runner),
File "/usr/lib64/python2.7/unittest/main.py", line 94, in __init__
self.parseArgs(argv)
File "/usr/lib64/python2.7/unittest/main.py", line 149, in parseArgs
self.createTests()
File "/usr/lib64/python2.7/unittest/main.py", line 158, in createTests
self.module)
File "/usr/lib64/python2.7/unittest/loader.py", line 130, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "/usr/lib64/python2.7/unittest/loader.py", line 100, in loadTestsFromName
parent, obj = obj, getattr(obj, part)
AttributeError: 'module' object has no attribute 'test_pwman'
有时,尽管在测试目录中有一个名为test_pwman的python模块,但我花了几分钟才试图理解为什么会出现这个错误.
setup.py中的相关代码是:
from setuptools.command.install import install
setup(name=pwman.appname,
...
test_suite='tests.test_pwman.suite',
...
)
花了一些时间盯着屏幕后,我会记得我可以像这样运行测试套件:
$python -m tests.test_pwman
这揭示了例如测试未运行的真正原因:
Traceback (most recent call last):
File "/usr/lib64/python2.7/runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib64/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/home/ozn/Software/pwman3/tests/test_pwman.py", line 27, in <module>
from .test_postgresql import TestPostGresql
File "tests/test_postgresql.py", line 26, in <module>
import psycopg2 as pg
File "/home/ozn/.virtualenvs/pwman3/lib/python2.7/site-packages/psycopg2/__init__.py", line 50, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: libpq.so.5: cannot open shared object file: No such file or directory
安装Postgresql并支持Python后,我可以运行我的测试.所以我的问题是:
你如何使setuptools传播真正的异常?
我的代码是否从setup.py调用测试套件错误了?
解决方法:
见http://bugs.python.org/issue7559,这是unittest模块中的一个错误.