Hi,

I was checking out the unittest module and try out the sample code. I save the code below in a file call "testrandom.py" and run it using IDLE (Python's default IDE), but I get the following in red. Obviously, the tests run fine as indicated by OK, but why did I get that traceback "error"?

Btw, I'm using Python 2.7.

...
----------------------------------------------------------------------
Ran 3 tests in 0.018s

OK

Traceback (most recent call last):
File "C:\python\testrandom.py", line 29, in <module>
unittest.main()
File "C:\PYTHON27\LIB\unittest\main.py", line 95, in __init__
self.runTests()
File "C:\PYTHON27\LIB\unittest\main.py", line 231, in runTests
sys.exit(not self.result.wasSuccessful())
SystemExit: False

import random
    import unittest

    class TestSequenceFunctions(unittest.TestCase):

        def setUp(self):
            self.seq = range(10)

        def test_shuffle(self):
            # make sure the shuffled sequence does not lose any elements
            random.shuffle(self.seq)
            self.seq.sort()
            self.assertEqual(self.seq, range(10))

            # should raise an exception for an immutable sequence
            self.assertRaises(TypeError, random.shuffle, (1,2,3))

        def test_choice(self):
            element = random.choice(self.seq)
            self.assertTrue(element in self.seq)

        def test_sample(self):
            with self.assertRaises(ValueError):
                random.sample(self.seq, 20)
            for element in random.sample(self.seq, 5):
                self.assertTrue(element in self.seq)

    if __name__ == '__main__':
        unittest.main()

In idle, sys.exit doesn't exit the process. Instead, the exception is caught and displayed

>>> import sys
>>> sys.exit(False)

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    sys.exit(False)
SystemExit: False

So how would I make it so it wouldn't display that when running my testrandom.py?

You would run the test from command window or you would double click the file in graphical file manager.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.