Razer
Razer

Reputation: 8211

Unittest example from doc raises AttributeError

I'm new to unittesting in python. I tried the unittest example from the documentation:

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

Running this code gives me following error on the commandline:

D:\src>python foo.py
 Traceback (most recent call last):
    File "foo.py", line 8, in <module>
  class TestSequenceFunctions(unittest.TestCase):
AttributeError: 'module' object has no attribute 'TestCase'

I'm using ActiveState Python 3.2. Why do I get an attribute error here?

Upvotes: 1

Views: 2853

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124738

Most likely you have a second unittest module or package in your python path.

If you created a unittest.py file or a unittest directory containing an __init__.py file, python could find that before it finds the normal module in the standard python library.

Naming a local module or package unittest is the equivalent of naming a local variable list or dict or map; you are masking the built-in name with a local redefinition.

Rename that module or package to something else to fix this.

Upvotes: 6

Related Questions