在Python unittest中,SystemExit:False打印即使测试运行了" OK"

时间:2016-11-24 14:25:41

标签: python unit-testing

我正在编写一个应该代表周期性间隔的类RecurringInterval,并首先尝试按如下方式测试它:

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)

运行时,此程序会按预期打印True

接下来,我想将此脚本转换为unittest。我对代码进行了以下调整:

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()

然而,当我运行这个时,我得到一个例外:

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)

回溯内容如下:

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

导致此异常的原因是什么?测试没有成功运行吗?

1 个答案:

答案 0 :(得分:1)

根据Tests succeed, still get traceback的回答,回溯是从iPython运行脚本的结果,并不表示正在测试的代码存在任何“实际”问题。

例如,如果我从命令行运行程序,我不会得到任何回溯:

kurt@kurt-ThinkPad:~/dev/scratch/Furion_scheduler$ python3 recurring_interval.py 
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK