python - ProgrammingError : (1146, "Table ' test_<DB>.<TABLE >' doesn' t exist") 为 Django 运行单元测试时

标签 python django unit-testing

我正在使用 Django 框架运行单元测试并收到此错误。

运行实际代码没有这个问题,运行单元测试会即时创建一个测试数据库,所以我怀疑问题出在那里。

抛出错误的代码如下所示

member = Member.objects.get(email=email_address)

模型看起来像

class Member(models.Model):
    member_id = models.IntegerField(primary_key=True)
    created_on = models.DateTimeField(editable=False, default=datetime.datetime.utcnow())
    flags = models.IntegerField(default=0)
    email = models.CharField(max_length=150, blank=True)
    phone = models.CharField(max_length=150, blank=True)
    country_iso = models.CharField(max_length=6, blank=True)
    location_id = models.IntegerField(null=True, blank=True)
    facebook_uid = models.IntegerField(null=True, blank=True)
    utc_offset = models.IntegerField(null=True, blank=True)
    tokens = models.CharField(max_length=3000, blank=True)
    class Meta:
        db_table = u'member'

我看不出有什么奇怪的。

运行测试的用户与运行网站的用户对数据库服务器具有相同的权限

这是运行在 osx 上的 mariadb 上的 django 1.1

    MJ-2:mysite Marsh$ python manage.py test sitecoming
Creating test database...
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table djangodblog_errorbatch
Creating table djangodblog_error
Installing index for djangodblog.ErrorBatch model
Installing index for djangodblog.Error model
E
======================================================================
ERROR: test_index (mysite.sitecoming.tests.SiteComingTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/Marsh/Development/deal/src/mysite/sitecoming/tests.py", line 19, in test_index
    response = c.post('/submit', {'email':'marshall@offby3.com'})
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/test/client.py", line 313, in post
    response = self.request(**r)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/handlers/base.py", line 92, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/Users/Marsh/Development/deal/src/mysite/sitecoming/views.py", line 49, in submit
    member = Member.objects.get(email=email_address)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/manager.py", line 120, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/query.py", line 300, in get
    num = len(clone)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/query.py", line 81, in __len__
    self._result_cache = list(self.iterator())
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/query.py", line 238, in iterator
    for row in self.query.results_iter():
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/sql/query.py", line 287, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/sql/query.py", line 2369, in execute_sql
    cursor.execute(sql, params)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 84, in execute
    return self.cursor.execute(query, args)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.3-fat.egg/MySQLdb/cursors.py", line 173, in execute
    self.errorhandler(self, exc, value)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.3-fat.egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
ProgrammingError: (1146, "Table 'test_deal.member' doesn't exist")

----------------------------------------------------------------------
Ran 1 test in 0.447s

FAILED (errors=1)
Destroying test database...

我还能在哪里查看哪里出了问题,为什么没有创建此表?

更新 - 问题似乎是在运行单元测试时,用于生成测试数据库的模型来自应用程序内部而不是项目内部。这看起来很奇怪,并且违反了 DRY,因为为了让它工作,我需要在每个应用程序中复制模型文件,而不是在项目中集中复制。

谁能建议如何解决这个问题?

** 更新 2 ** - 项目结构如下所示:

项目结构如下:

/mysite (www.whatever.com)
    /application1 (facebook app, handles all urls beginning with /fb)
    /application2 (www app, handles everything else in the root dir of the site)

我想将 fb 功能与站点的其余部分分开,但它们共享同一个数据库。我做错了吗?

最佳答案

要纠正此问题,请生成在项目文件夹的 settings.py 文件中声明的所有表。

您可以在设置文件的 INSTALLED APPS block 中找到。为此运行此命令:

manage.py syncdbpython manage.py syncdb

如果这不起作用,则为 python 目录设置环境变量 PATH。

关于python - ProgrammingError : (1146, "Table ' test_<DB>.<TABLE >' doesn' t exist") 为 Django 运行单元测试时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2644749/

相关文章:

django - 在 Django 的 Debug模式下关闭缓存

python - 在哪里保存 Python 单元测试?

python - 在子解析器参数之后添加顶级 argparse 参数

python - 是时候举手 throw 了吗?

django - 使用 django + nginx + apache mod_wsgi 访问正在进行的上传文件

javascript - 无法在 vue-test-utils 中使用 $route 设置模拟

c# - 测试/模拟什么?

python - 我需要调用一个函数,直到它在 python 中返回 0

python - 如何使用 Selenium 提取 src 属性的值

python - 通过base64上传文件