Python: handle sys.exit() in doctests -
i trying use python doctests test error message being printed in function. below code
import sys def get_values(vals=[]): """ should split key:val , give values >>> get_values(["abc"]) error: not values """ values = [] val in vals: try: kv = val.split(':') k = kv[0] v = kv[1] values.append(v) except exception: print_msg("error: not 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()
when run doctest, getting below:
********************************************************************** 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: not 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
can me how handle sys.exit(1) while running doctests?
use mock library monkey patch sys.exit
.
Comments
Post a Comment