我是在 Django 中使用生菜进行 BDD 开发的新手,但是,我需要帮助来弄清楚如何为我的模型加载初始测试数据,以及如何在每次测试之前刷新它们。
问问题
449 次
2 回答
1
我认为最简单的起点是查看http://lettuce.it/tutorial/tables.html。对于“刷新”数据,我使用terrain.py
如下代码:
from django.db import transaction
@before.each_feature
def begin_transaction(feature):
#shouldn't strictly be needed, but I've gotten
#inconsistent results without it
transaction.rollback()
transaction.set_autocommit(False)
@after.each_feature
def end_transaction(feature):
transaction.rollback()
使用它可能更合适before.each_scenario
。使用对你有用的东西。
于 2014-03-06T03:10:31.827 回答
0
我有以下内容:
@before.each_scenario
def load_scenario_fixture(scenario):
call_command('loaddata', 'lettuce_global', interactive=False, verbosity=0)
fixture_path = os.path.join(scenario.feature.name.lower().replace(' ', '_'), scenario.name.lower().replace(' ', '_'))
logger.info("Loading fixture:")
logger.info(" " + fixture_path)
call_command('loaddata', fixture_path, interactive=False, verbosity=0)
@after.each_scenario
def flush_database(scenario):
logger.info("Flushing the test database ...")
call_command('flush', interactive=False, verbosity=0)
这在每个场景之前加载到全局测试夹具中,并且还加载到场景的特定夹具中。夹具文件路径格式为 {app}/fixtures/{feature name}/{fixture name} 场景完成后,我只需使用 Django flush 命令进行重置。
于 2014-04-28T11:16:34.077 回答