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

I execute this command:

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

and causes this error:

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

Below is my directory structure. I also added app1 to my configuration of installed applications.

 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

+84
python django python-unittest
Aug 29 '14 at 19:34
source share
10 answers

I finally realized that I was working on another problem. The problem was that my test could not find the import.

It looks like you got the above error if your test failed to import. This makes sense because the test suite cannot import the broken test. At least I think this is what happens because I fixed the import in my test file and, of course, started working.

To test your test case, just try importing the test file file into the python console.

Example:

 from project.apps.app1.tests import * 
+162
Aug 29 '14 at 20:22
source share

Using:

./manage.py shell

followed by

import myapp.tests

to find the nature of the import error.

+30
Mar 30 '15 at 13:34
source share

In my case, I need to create empty __init __. py in my app/tests folder

+12
May 03 '16 at 19:07
source share

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

Other types 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() 
+6
Apr 25 '15 at 2:17
source share

I had the same error as Chris. I deleted the old model and then ran test.py, but another file (views.py) was still trying to import the remote model.

When I took out the obsolete import instruction, the problem was resolved.

+4
Sep 25 '14 at 19:26
source share

Make sure that all the modules you use in the script are not broken. By this I mean spellchecking in your import statements.

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

You can test your modules by following the import instructions in the jjano 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 
+2
Jan 09 '17 at 10:03 on
source share

According to django doc. When you run your tests , the default behavior of the test utility is to find all test cases (that is, subclasses of unittest.TestCase ) in any file whose name begins with the test, automatically builds the test suite from these test cases and runs this package.

try this: python manage.py test tests.py

+1
Aug 29 '14 at 19:49
source share

Got the same error but checked the list of all the reasons without fixing my problem.

Finally, find out that the reason is that the name of one method that is imported but not yet in use is incorrect. Although this is a stupid mistake, this is happening.

+1
Oct 03 '16 at 8:07
source share

I fixed the "AttributeError: Utilities module does not have the" name_of_my_function "attribute by correcting the circular import link. Each of my manage.py and utils.py files contained an import statement pointing to each other.

+1
Sep 29 '18 at 0:22
source share

enter image description here

I tried all of the above solutions and could not. The test works in pycharm, but in .gitlab-ci.yml does not work, please help?

0
Jan 23 '19 at 17:44
source share



All Articles