我有一个带有许多功能的模块。其中一个应该调用testmod
(或相应的doctest函数)来测试模块中的所有其他函数,只要满足某个条件。
这是我的意思的一个例子:
#!/usr/bin/python
# Let's call this file 'mymodule.py'
import sys
def to_be_tested():
'''\
>>> to_be_tested()
2
'''
return 1 # Would fail, but doesn't get tested
# ... more functions with doctest strings ...
def main():
if '--test' in sys.argv: # Test all functions in this file
import doctest
doctest.testmod(verbose=True) # Doesn't work with setuptools
exit(0)
if __name__ == '__main__':
main()
该示例在作为shell脚本运行时工作正常,例如为/home/confus/mymodule.py --test
,但未使用setup.py
中指定的入口点。在后一种情况下,例如由mymoduleapp --test
运行,仅测试main()
(并且每次都会传递,因为其文档字符串中未定义任何测试)。
# In 'setup.py'
from setuptools import setup
setup(
...,
entry_points='''
[console_scripts]
mymoduleapp=mymodule:main
)
'''