【问题标题】:Got an error creating the test database: Django unittest创建测试数据库时出错:Django unittest
【发布时间】:2017-11-24 04:00:41
【问题描述】:

当我运行python manage.py test 时,django 每次都会问一个奇怪的问题:

$ python manage.py test
Creating test database for alias 'default'...
Got an error creating the test database: (1044, "Access denied for user 'nyble'@'localhost' to database 'test_nybledb'")
Type 'yes' if you would like to try deleting the test database 'test_nybledb', or 'no' to cancel: 

我希望它只是删除并重新制作一个基本的 sqlite3 数据库,我不希望这种行为。

无论我说是或否,它都会退出测试:

Destroying old test database for alias 'default'...
Got an error recreating the test database: (1044, "Access denied for user 'nyble'@'localhost' to database 'test_nybledb'")

在我的设置中

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'nybledb',
        'USER': 'nyble',
        'PASSWORD': 'password',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

如何在运行测试期间停止它并使其使用正常的 sqlite3 db?

【问题讨论】:

    标签: python django django-unittest


    【解决方案1】:

    您可以在 settings.py 中进行设置。它说如果命令行输入中有'test',请使用sqlite作为数据库引擎。

    import sys
    if 'test' in sys.argv or 'test_coverage' in sys.argv: #Covers regular testing and django-coverage
        DATABASES['default']['ENGINE'] = 'django.db.backends.sqlite3'
    

    【讨论】:

    • 谢谢你这很有用。你刚刚救了我。上帝保佑你)))
    猜你喜欢
    • 2021-07-24
    • 2019-09-05
    • 2014-07-26
    • 2022-01-16
    • 2012-04-20
    • 2017-01-26
    • 2012-12-20
    • 2020-11-17
    • 1970-01-01
    相关资源
    最近更新 更多