[docs] Problems with a unittest example and Python 3.3

Pierazzo Mara info at trattoriaallascala.com
Sun Sep 30 10:04:04 CEST 2012


I tried the very first example I found on the Python documentation at this address: http://docs.python.org/py3k/library/unittest.html#module-unittest

    import random
    import unittest
    
    class TestSequenceFunctions(unittest.TestCase):
    
        def setUp(self):
            self.seq = list(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, list(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()


On Python 2.7.1 this example used to work, as I was getting back this result:


> Ran 3 tests in 0.001s
> 
> OK


but when I run this code on Python 3.3 I'm getting this error:


    Traceback (most recent call last):
      File "/Users/[USERNAME]/Desktop/prova.py", line 29, in <module>
        unittest.main()
      File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/unittest/main.py", line 125, in __init__
        self.runTests()
      File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/unittest/main.py", line 263, in runTests
        sys.exit(not self.result.wasSuccessful())
    SystemExit: False


More information about the docs mailing list