如何禁用django迁移调试日志记录?

时间:2015-08-22 17:14:59

标签: python django nose django-migrations django-nose

lafagundes question about south migration debug logging非常相似,除了我没有使用南方 - 我使用普通的Django 1.7迁移。我也使用django-nose测试跑步者。

当我运行manage.py test时,没有捕获调试日志记录输出:

(codesy)lcrouch:codesy lcrouch$ ./manage.py test
nosetests --verbosity=1
Creating test database for alias 'default'...
......E...............................
======================================================================
ERROR: test_return_state (auctions.tests.utils_tests.IssueStateTest)
----------------------------------------------------------------------

当我运行单独的测试模块,例如./manage.py test auctions.tests.utils_tests时,调试日志记录输出包括Django迁移中涉及的所有django.db.backends.schema: DEBUG行:

(codesy)lcrouch:codesy lcrouch$ ./manage.py test auctions.tests.utils_tests
nosetests auctions.tests.utils_tests --verbosity=1
Creating test database for alias 'default'...
E
======================================================================
ERROR: test_return_state (auctions.tests.utils_tests.IssueStateTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/lcrouch/code/codesy/codesy/auctions/tests/utils_tests.py", line 13, in test_return_state
    fake_gh_client = fudge.Fake(github_client).returns_fake().provides('get_repo').returns_fake().provides('get_issue').returns_fake().has_attr(state='open')
  File "/Users/lcrouch/python/codesy/lib/python2.7/site-packages/fudge/__init__.py", line 1133, in returns_fake
    exp = self._get_current_call()
  File "/Users/lcrouch/python/codesy/lib/python2.7/site-packages/fudge/__init__.py", line 765, in _get_current_call
    "Call to a method that expects a predefined call but no such call exists.  "
FakeDeclarationError: Call to a method that expects a predefined call but no such call exists.  Maybe you forgot expects('method') or provides('method') ?
-------------------- >> begin captured logging << --------------------
django.db.backends.schema: DEBUG: CREATE TABLE "django_migrations" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "app" varchar(255) NOT NULL, "name" varchar(255) NOT NULL, "applied" datetime NOT NULL); (params [])
django.db.backends.schema: DEBUG: CREATE TABLE "django_content_type" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(100) NOT NULL, "app_label" varchar(100) NOT NULL, "model" varchar(100) NOT NULL); (params [])
django.db.backends.schema: DEBUG: CREATE TABLE "django_content_type__new" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(100) NOT NULL, "app_label" varchar(100) NOT NULL, "model" varchar(100) NOT NULL, UNIQUE ("app_label", "model")); (params [])
...
django.db.backends.schema: DEBUG: DROP TABLE "socialaccount_socialapp"; (params [])
django.db.backends.schema: DEBUG: ALTER TABLE "socialaccount_socialapp__new" RENAME TO "socialaccount_socialapp"; (params [])

这使得回到实际失败真的很难。

如何禁用此输出?在django或鼻子水平?

2 个答案:

答案 0 :(得分:4)

如果您不介意在测试中禁用迁移,可以通过在settings.py中添加以下内容来实现:

TESTING = 'test' in sys.argv

if TESTING:
    class DisableMigrations(object):
        def __contains__(self, item):
            return True

        def __getitem__(self, item):
            return "notmigrations"

    MIGRATION_MODULES = DisableMigrations() 

这将只是一次创建数据库,而不是逐步完成每次迁移。

如果您希望在测试中保持迁移,则可以通过更新日志记录设置来删除邮件。

TESTING = 'test' in sys.argv

if TESTING:
    LOGGING = {
        'version': 1,
        'handlers': {
            'file': {
                'level': 'DEBUG',
                'class': 'logging.FileHandler',
                'filename': '/tmp/codesy-debug.log',
            },
        },
        'loggers': {
            'django.db.backends.schema': {
                'handlers': ['file'],
                'propagate': True,
                'level': 'INFO',
            },
            '': {
                'handlers': ['file'],
                'level': 'DEBUG',
            }
        }
    }

答案 1 :(得分:2)

要快速删除日志消息,您可以使用以下命令运行:

./manage.py test --nologcapture

nose logcapture plugin正在捕获消息,其中包含用于微调的其他选项。但是,django-nose有old issue可能会阻止您使用命令行中的许多内容。它安排在milestone v1.4.2,但是这个月已经过期了。您可以使用.nosercnose.cfg文件解决问题 - 请参阅nose docs了解格式。