我正在尝试使用 python doctests 来测试在函数中打印的错误消息。下面是我的代码
import sys
def get_values(vals=[]):
"""
Should split key:val and give values
>>> get_values(["abc"])
Error: could not get the values
"""
values = []
for val in vals:
try:
kv = val.split(':')
k = kv[0]
v = kv[1]
values.append(v)
except Exception:
print_msg("Error: could not get the values")
return values
def print_msg(msg):
print msg
sys.exit(1)
def main():
import doctest
try:
doctest.testmod()
except doctest.DocTestFailure, failure:
print 'DocTestFailure:'
sys.exit(1)
print "doctests complete"
if __name__ == "__main__":
main()
当我运行 doctest 时,我得到以下信息:
**********************************************************************
File "abc.py", line 7, in __main__.get_values
Failed example:
get_values(["abc"])
Exception raised:
Traceback (most recent call last):
File "/depot/python/lib/python2.7/doctest.py", line 1254, in __run
compileflags, 1) in test.globs
File "<doctest __main__.get_values[0]>", line 1, in <module>
get_values(["abc"])
File "abc.py", line 18, in get_values
print_msg("Error: could not get the values")
File "abc.py", line 23, in print_msg
sys.exit(1)
SystemExit: 1
**********************************************************************
1 items had failures:
1 of 1 in __main__.get_values
***Test Failed*** 1 failures.
doctests complete
任何人都可以帮助我在运行文档测试时如何处理 sys.exit(1) 吗?