2014-02-23 22 views

回答

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。使用適合你的東西。

+0

transaction.set_autocommit在Django 1.5上不可用。 –

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命令進行重置。

相關問題