In Python unittest, SystemExit: False printed even though the test ran “OK”
我正在写一个类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import datetime import dateutil.relativedelta import dateutil.rrule import dateutil.parser class RecurringInterval(object): def __init__(self, *args, **kwargs): self.period = kwargs.pop('period', None) assert isinstance(self.period, datetime.timedelta) or isinstance(self.period, dateutil.relativedelta.relativedelta) or (self.period is None) self.rrule = dateutil.rrule.rrule(*args, **kwargs) def __contains__(self, time): last_occurrence = self.rrule.before(time) return (last_occurrence <= time) and (time <= last_occurrence + self.period) if __name__ =="__main__": start = dateutil.parser.parse("Thu Nov 24 14:00 UTC 2016") recurring_interval = RecurringInterval(dateutil.rrule.DAILY, dtstart=start, count=5, period=datetime.timedelta(hours=2)) time = dateutil.parser.parse("Thu Nov 24 15:00 UTC 2016") print(time in recurring_interval) |
运行时,该程序按预期打印
接下来,我想将此脚本转换为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import datetime import dateutil.relativedelta import dateutil.rrule import dateutil.parser import unittest class RecurringInterval(object): def __init__(self, *args, **kwargs): self.period = kwargs.pop('period', None) assert isinstance(self.period, datetime.timedelta) or isinstance(self.period, dateutil.relativedelta.relativedelta) or (self.period is None) self.rrule = dateutil.rrule.rrule(*args, **kwargs) def __contains__(self, time): last_occurrence = self.rrule.before(time) return (last_occurrence <= time) and (time <= last_occurrence + self.period) class TestRecurringInterval(unittest.TestCase): def test_contains_method_for_primary_interval(self): start = dateutil.parser.parse("Thu Nov 24 14:00 UTC 2016") recurring_interval = RecurringInterval(dateutil.rrule.DAILY, dtstart=start, period=datetime.timedelta(hours=2)) time = dateutil.parser.parse("Thu Nov 25 15:00 UTC 2016") self.assertTrue(time in recurring_interval) if __name__ =="__main__": unittest.main() |
但是,当我运行它时,我得到一个例外:
1 2 3 4 5 6 7 8 9 10 11 12 | In [24]: exec(open('recurring_interval.py').read()) . ---------------------------------------------------------------------- Ran 1 test in 0.002s OK An exception has occurred, use %tb to see the full traceback. SystemExit: False /usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py:2889: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D. warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1) |
回溯内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | In [25]: %tb --------------------------------------------------------------------------- SystemExit Traceback (most recent call last) <ipython-input-24-ab8976376637> in <module>() ----> 1 exec(open('recurring_interval.py').read()) <string> in <module>() /usr/lib/python3.5/unittest/main.py in __init__(self, module, defaultTest, argv, testRunner, testLoader, exit, verbosity, failfast, catchbreak, buffer, warnings, tb_locals) 92 self.progName = os.path.basename(argv[0]) 93 self.parseArgs(argv) ---> 94 self.runTests() 95 96 def usageExit(self, msg=None): /usr/lib/python3.5/unittest/main.py in runTests(self) 255 self.result = testRunner.run(self.test) 256 if self.exit: --> 257 sys.exit(not self.result.wasSuccessful()) 258 259 main = TestProgram SystemExit: False |
导致此异常的原因是什么? 测试未成功运行吗?
在测试成功后,仍然得到回溯,回溯是从iPython运行脚本的结果,并不表示正在测试的代码有任何"实际"问题。
例如,如果我从命令行运行程序,我没有得到任何回溯:
1 2 3 4 5 6 | kurt@kurt-ThinkPad:~/dev/scratch/Furion_scheduler$ python3 recurring_interval.py . ---------------------------------------------------------------------- Ran 1 test in 0.001s OK |