TypeError:测试加载程序返回了一个不可命名的对象

2024-09-28 17:00:55 发布

您现在位置:Python中文网/ 问答频道 /正文

当我尝试运行时,不确定Django中的这些测试错误是什么:

python manage.py test --settings=core.settings.test

每个django应用程序中的测试失败,原因是:

Error in shepard.core.tests.test_exampleapp
TypeError: Test loader returned an un-runnable object.  Is 
"shepard.core.tests.test_authorization" importable from your current location?  

Maybe you forgot an __init__.py in your directory?  Unrunnable object looks like: 
None of type <class 'NoneType'> with dir ['__bool__', '__class__', '__delattr__', 
'__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', 
'__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', 
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', 
'__subclasshook__']

更新

对于test.py中的良好配置,仍然会返回不可命名的对象错误。欢迎任何想法,我是Django noob:)

core/settings/test.py:

ENVIRONMENT = "test"
DATABASES = {
 "default": {
  ...
 }
}

AUTH.update(
    {"gateway": "DummyAuthorizationGateway", 
"permissions": "DummyPermissionsGateway"}
)
LOGGING["loggers"] = {}
REST_FRAMEWORK = {
"DATETIME_FORMAT": "%Y-%m-%dT%H:%M",
"DEFAULT_THROTTLE_CLASSES": (
    "rest_framework.throttling.AnonRateThrottle",
    "rest_framework.throttling.UserRateThrottle",
    "rest_framework.throttling.ScopedRateThrottle",
),
"DEFAULT_THROTTLE_RATES": {"anon": "10/minute", "user": 
"120/minute"},
"DEFAULT_RENDERER_CLASSES": 
("rest_framework.renderers.JSONRenderer",),
"DEFAULT_PERMISSION_CLASSES": 
("rest_framework.permissions.IsAuthenticated",),
"DEFAULT_AUTHENTICATION_CLASSES": (
    
"oauth2_provider.contrib.rest_framework.OAuth2Authentication",
),
"DEFAULT_PAGINATION_CLASS": "core.pagination.IntegerPagination",
"PAGE_SIZE": 10,
}

文件结构如下所示

    ├── shepard
    ├── clients
        ├── tests
            ├── __init__.py
            ├── test_file_1.py
    ├── events
    ├── core
        ├── settings
            ├── test.py
            ├── dev.py

    ├── manage.py

Tags: djangopycoretestrestdefaultsettingsmanage