我想使用自定义的sqlite数据库进行代码的单元测试。question的答案是使用playhouse.test_utils的test_database。
但是,这在那里是不可用的。
我可以用什么来替换它?
发布于 2019-02-08 05:09:15
您可以使用Database.bind()或Database.bind_ctx()方法,这两种方法都有说明:
http://docs.peewee-orm.com/en/latest/peewee/api.html#Database.bind_ctx
文档中包含的场景正是这样的:
MODELS = (User, Account, Note)
# Bind the given models to the db for the duration of wrapped block.
def use_test_database(fn):
@wraps(fn)
def inner(self):
with test_db.bind_ctx(MODELS):
test_db.create_tables(MODELS)
try:
fn(self)
finally:
test_db.drop_tables(MODELS)
return inner
class TestSomething(TestCase):
@use_test_database
def test_something(self):
# ... models are bound to test database ...
passhttps://stackoverflow.com/questions/54580385
复制相似问题