1

该项目是一个部署在 heroku 上的 webapp:

这是我在 db_test_many_create.py 中的简单单元测试:

import unittest
from petalapp.database.models import User, Hospital, Data, ROLE_USER
from petalapp import db

class BuildDestroyTables(unittest.TestCase):

    user_test_1 = User("test_user_nickname","user_email",ROLE_USER)
    data_test_1 = Data(1)
    hospital_test_1 = Hospital("test_hospital_1")

    def setUP(self):
        db.drop_all()
        db.create_all()


    def tearDown(self):
        db.session.remove()
        db.drop_all()


    def test_user_setup(self):

        db.session.add(self.user_test_1)
        db.session.commit()


if __name__ == "__main__":
    unittest.main()

当我运行命令时:

heroku run python petalapp/database/db_test_many_create.py  --app warm-atoll-3630

为了针对我的数据库对其进行测试,我得到以下回溯:

Running `python petalapp/database/db_test_many_create.py` attached to terminal... up, run.6505
E
======================================================================
ERROR: test_user_setup (__main__.BuildDestroyTables)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "petalapp/database/db_test_many_create.py", line 30, in test_user_setup
    db.session.commit()
  File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 114, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 703, in commit
    self.transaction.commit()
  File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 361, in commit
    self._prepare_impl()
  File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 340, in _prepare_impl
    self.session.flush()
  File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1718, in flush
    self._flush(objects)
  File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1789, in _flush
    flush_context.execute()
  File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 331, in execute
    rec.execute(self)
  File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 475, in execute
    uow
  File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 64, in save_obj
    table, insert)
  File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 558, in _emit_insert_statements
    execute(statement, params)
  File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1449, in execute
    params)
  File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1584, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1698, in _execute_context
    context)
  File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1691, in _execute_context
    context)
  File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 331, in do_execute
    cursor.execute(statement, parameters)
ProgrammingError: (ProgrammingError) relation "user" does not exist
LINE 1: INSERT INTO "user" (nickname, email, role) VALUES (E'test_us...
                    ^
 'INSERT INTO "user" (nickname, email, role) VALUES (%(nickname)s, %(email)s, %(role)s) RETURNING "user".id' {'nickname': 'test_user_nickname', 'role': 0, 'email': 'user_email'}

----------------------------------------------------------------------
Ran 1 test in 0.349s

FAILED (errors=1)

但是,如果我在交互式会话中进行更改(我认为是在进行):

In [2]: from petalapp.database.models import User, Hospital, Data, ROLE_USER

In [3]: from petalapp import db

In [4]: db.drop_all()

In [5]: db.create_all()

In [6]: user_test_1 = User("test_user_nickname","user_email",ROLE_USER)

In [7]: db.session.add(user_test_1)

In [8]: db.session.commit()

In [9]: u = User.query.get(1)

In [10]: print(u)
<Name : u'test_user_nickname'>

一切都如我所料,这让我相信我没有正确使用 setUp 和 tearDown 。如何使用单元测试有效地测试我的数据库?

4

1 回答 1

2

我发现setUP()而不是setUp()在您的代码中。这可以解释为什么您的数据库似乎没有初始化,因为unittest.TestCase'ssetUp()没有被覆盖,因此没有运行。

于 2012-12-27T20:04:02.713 回答