2

関数で出力されているエラーメッセージをテストするために 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

doctest の実行中に sys.exit(1) を処理する方法を教えてくれる人はいますか?

4

2 に答える 2