1

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?

2
  • 3
    Do you have a dir named unittest in `d:\src` ? Commented Jul 17, 2012 at 8:28
  • to debug, add a print(unittest) statement to your code and see if it is the stdlib's unittest module or your own file instead. Commented Jul 17, 2012 at 16:47

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, a directory. Thanks for answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.