Kurt Peek
Kurt Peek

Reputation: 57511

In Python unittest, SystemExit: False printed even though the test ran "OK"

I'm writing a class RecurringInterval which is supposed to represent periodic intervals, and tried firstly to test it as follows:

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)

When run, this program prints True as expected.

Next, I'd like to convert this script into a unittest. I made the following adaptations to the code:

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

However, when I run this I get an exception:

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)

The traceback reads as follows:

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

What is causing this exception? Are the tests not running successfully?

Upvotes: 2

Views: 3987

Answers (1)

Kurt Peek
Kurt Peek

Reputation: 57511

Following the answer from Tests succeed, still get traceback, the traceback is a result of running the script from iPython, and does not signal any 'actual' problem with the code being tested.

For example, if I run the program from the command line, I don't get any traceback:

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

OK

Upvotes: 2

Related Questions