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