110

I'm running this command:

python manage.py test project.apps.app1.tests

and it causes this error:

AttributeError: 'module' object has no attribute 'tests'

Below is my directory structure. I've also added app1 to my installed apps config.

Traceback (most recent call last):
    File "manage.py", line 10, in <module> execute_from_command_line(sys.argv)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 71, in execute
    super(Command, self).execute(*args, **options)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 88, in handle
    failures = test_runner.run_tests(test_labels)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/test/runner.py", line 146, in run_tests
    suite = self.build_suite(test_labels, extra_tests)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/test/runner.py", line 66, in build_suite
    tests = self.test_loader.loadTestsFromName(label)
    File "/usr/lib/python2.7/unittest/loader.py", line 100, in loadTestsFromName
    parent, obj = obj, getattr(obj, part)
    AttributeError: 'module' object has no attribute 'tests'

Directory structure:

enter image description here

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Chris
  • 4,643
  • 6
  • 31
  • 49

12 Answers12

196

I finally figured it out working on another problem. The problem was that my test couldn't find an import.

It looks like you get the above error if your test fails to import. This makes sense because the test suite can't import a broken test. At least I think this is what is going on because I fixed the import within my test file and sure enough it started working.

To validate your test case just try import the test case file in python console.

Example:

from project.apps.app1.tests import *
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Chris
  • 4,643
  • 6
  • 31
  • 49
  • Bad assumption in that message. – AdamC Jan 25 '16 at 22:10
  • 1
    This can also be done from the command line, eliminating the need to open a Python shell: `python -c "from project.apps.app1.tests import *"` – Hakan B. Sep 16 '17 at 04:28
  • 1
    +1 saved me from confusing error message. The only problem with your suggestion is that you imported **death star** . OK in console for experiments, but in code, always import just the names needed. – Peter M. - stands for Monica Jan 17 '18 at 19:54
  • Instead of fearing use of the "**death star**" can't you juse use the `__all__` variable in each file? And specify a list of class names, functions, and variables to export when using `from package_name.module import *`? I've had good luck with this pattern. I understand it takes a bit more time to write up the `__all__` bit at the top of each file. But importing using "**death star**" seems to work nicely. – MikeyE Jan 10 '20 at 23:04
  • I just had this issue and was really confused. Thanks for the answer. Python is not doing the right thing here. It needs a clearer message. That being said, I assume your "...import *" is just to check the tests, not meant to be part of the test runner... I think the latter would be inappropriate. – rfportilla Apr 20 '20 at 15:33
  • This answer can't be accepted although it works. Never import in python with star – RaphWork Jan 19 '21 at 00:21
  • Yep the star was just an example. – Chris Jan 20 '21 at 01:18
39

Use:

./manage.py shell

followed by

import myapp.tests

to find the nature of the import error.

Steve Bradshaw
  • 827
  • 7
  • 10
  • I appreciate the effort, and it seems like this worked for many people. But, when I opened a python interactive shell using `./manage.py shell` then did either `import myapp.tests` or `from myapp.tests import *` both worked without error. But, I still get the error described by the OP. – MikeyE Jan 10 '20 at 23:09
23

For my case, I need to create an empty __init__.py in my app/tests folder

tmin
  • 1,313
  • 13
  • 15
6

Steve Bradshaw's example above works for import errors (thanks Steve).

Other type of errors (e.g. ValueError) may also cause

AttributeError: 'module' object has no attribute 'tests'

to see what these errors are

./manage.py shell
from myapp.tests import SomeTestCase
t = SomeTestCase()
lukeaus
  • 11,465
  • 7
  • 50
  • 60
5

I had the same error as Chris. I had deleted an old model, then run tests.py, but another file (views.py) was still trying to import the deleted model.

When I took out the now-obsolete import statement, problem solved.

Doug Murphy
  • 453
  • 1
  • 5
  • 9
3

Make sure that all modules that you are using in your script are not broken. By this I mean check spelling in your import statements.

# invalid import
from app.model.notification import Notification
# valid import
from app.models.notification import Notification

You can test yours modules by executing imports statements in djano's interactive console.

$root@13faefes8: python manage.py shell
Type "help", "copyright", "credits" or "license" for more information (InteractiveConsole)
>>> from app.model.notification import Notification
Traceback (most recent call last): 
   File "<console>", line 1, in <module>
ImportError: No module named model.notification
Lukasz Dynowski
  • 11,169
  • 9
  • 81
  • 124
2

I resolved the error "AttributeError: module 'utils' has no attribute 'name_of_my_function' " by fixing a circular import reference. My files manage.py and utils.py each had an import statement pointing at each other.

rudyt
  • 21
  • 3
2

I had the same error. It turned out to be because I named my module common.py, yet there already was some other common.py module. All I had to do was to rename my module.

Davide Andrea
  • 1,357
  • 2
  • 15
  • 39
1

According to django document When you run your tests, the default behavior of the test utility is to find all the test cases (that is, subclasses of unittest.TestCase) in any file whose name begins with test, automatically build a test suite out of those test cases, and run that suite.

so try this : python manage.py test tests.py

Mazdak
  • 105,000
  • 18
  • 159
  • 188
1

Got the same error, but checked all the reasons list here, did not fix my problem.

Finally figure it out that, the reason is that the name of one method that imported but not used yet is not correct. Though it is a stupid error, it happens.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
zhihong
  • 1,808
  • 2
  • 24
  • 34
0

I had a similar error while writing a unittest.TestCase. When I re-typed the same method definition as-is, it seemed to work !

The only change I noticed on PyCharm was the 'override' icon pop-up the 2nd time, as the setup(self) method needs to override the original method defined in TestCase.

enter image description here

dpsahoo
  • 27
  • 7
0

My solution was just renaming my module.

bobdilan
  • 21
  • 6
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 04 '21 at 08:31
  • You mean like in [this answer](https://stackoverflow.com/a/54932529/2227743)? – Eric Aya Nov 04 '21 at 11:02