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

后端 未结 11 1703
自闭症患者
自闭症患者 2020-12-04 14:43

I\'m running this command:

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

and it causes this error:

AttributeError: \

相关标签:
11条回答
  • 2020-12-04 15:26

    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()
    
    0 讨论(0)
  • 2020-12-04 15:27

    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

    0 讨论(0)
  • 2020-12-04 15:28

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

    0 讨论(0)
  • 2020-12-04 15:28

    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.

    0 讨论(0)
  • 2020-12-04 15:35

    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
    
    0 讨论(0)
  • 2020-12-04 15:35

    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.

    0 讨论(0)
提交回复
热议问题