2015-07-21 7 views
1

Я пытаюсь использовать доктрины python для проверки сообщения об ошибке, которое печатается в функции. Ниже мой кодPython: handle sys.exit() в 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) во время работы doctests?

+0

Вместо 'sys.exit' вы можете сделать' raise KeyboardInterrupt'. – refi64

+0

Спасибо за ваш ответ. – allDoubts

ответ

0

Mock библиотека до monkey patchsys.exit.

+0

Спасибо, но есть ли способ сделать это без использования внешних библиотек? – allDoubts