How come unittest.TestCase doesn't exist for me?

Traceback (most recent call last):
  File "D:\My Projects\TAGEngine\TAGEngine\trunk\unittest.py", line 1, in <module>
    import unittest
  File "D:\My Projects\TAGEngine\TAGEngine\trunk\unittest.py", line 4, in <module>
    class CheckStructureAcceptance(unittest.TestCase):
AttributeError: 'module' object has no attribute 'TestCase'

Dani AI

Generated

This error usually happens when Python is importing a local module named the same as a standard library module. The import system searches sys.path in order, and the script’s directory is first, so a file or package named unittest in your project will shadow the real standard library unittest, which is why TestCase is missing.

To confirm what is actually being imported, check the module’s file path:

python -c "import unittest; print(unittest.__file__)"

If that prints a path inside your project, you are shadowing the stdlib. After you correct the naming conflict, clear any cached bytecode that might still cause Python to load the wrong module:

  • For Python 3: delete __pycache__/unittest.*.pyc
  • For Python 2: delete unittest.pyc
  • Also ensure there is no folder named unittest/ in your project or on sys.path

A quick sanity check after cleanup:

python -c "import unittest; print(hasattr(unittest, 'TestCase'))"

When it returns True, you are importing the real module again. For background on how imports are resolved, see the Python docs on the import system (reference/import.html). The official unittest API, including TestCase, is documented at docs.python.org/3/library/unittest.html.

Recommended Answers

All 6 Replies

???

Does yours outputs like this mate?

>>> import unittest
>>> dir (unittest)
['FunctionTestCase', 'TestCase', 'TestLoader', 'TestProgram', 'TestResult', 'TestSuite', 'TextTestRunner', '_CmpToKey', '_TextTestResult', '_WritelnDecorator', '__all__', '__author__', '__builtins__', '__doc__', '__email__', '__file__', '__metaclass__', '__name__', '__package__', '__unittest', '__version__', '_makeLoader', '_strclass', 'defaultTestLoader', 'findTestCases', 'getTestCaseNames', 'main', 'makeSuite', 'os', 'sys', 'time', 'traceback', 'types']
>>> print unittest.TestCase
<class 'unittest.TestCase'>

EDIT:

Mate change your script filename, it's replacing the module.

Cheers and Happy coding

nope. dir(unittest) lookse like this:

I shoulda posted that on the first post.

Mate change your script filename, it's replacing the module.

Your script named , it's replacing the module file.

Cheers and Happy coding

ahh. thanks lol

Already had similar problems, we try to keep the names neat, but as it adds the script path to the syspath, it replaces the modules if we have files with the same name.

It can be cool depending of the situations. :D

Cheers and Happy coding

Hi,
you can not define you filename as unittest which is LIB name...
this will make program search unittest.TestCase form you own file.
so if you rename your file.It's will be ok.

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.