2013-03-17 17 views
0

我剛剛將所有AJAX驗證代碼移到了Django Forms中。我需要一些幫助來更新我的測試。我基本上有一些測試數據,聲明爲常量,用於所有套件。我在整個測試過程中都會重複這些數據。Django測試使用新ID創建表格

作爲安裝的一部分,我創建一些用戶和登錄我需要的用戶:

def setUp(self): 
    self.client = Client() 
    create_user(username='staff', email='[email protected]', 
       password='staff', staff=True) 
    create_user(username='agent', email='[email protected]', 
       password='agent', staff=False) 
    ShiftType.objects.create(type_id='SI', description='Sales Inbox') 
    self.client.login(username='staff', password='staff') 

推倒刪除該數據(或者它使用的):

def tearDown(self): 
    # Clean up the DB 
    self.client.logout() 
    ShiftType.objects.all().delete() 
    User.objects.all().delete() 
    Event.objects.all().delete() 
    RecurrentEvent.objects.all().delete() 

這是工作正常,但現在表單不會驗證,因爲用戶給出的表單值id每次都會增加。例如:

ERROR: <ul class="errorlist"><li>employee_id<ul class="errorlist"><li>Select a valid choice. That choice is not one of the available choices.</li></ul></li></ul> 

打印表格讓我看到每次都增加了id。

即使我正在刪除所有員工,爲什麼會發生這種情況?

回答

0

我會研究使用文本夾具,而不是每次創建和刪除數據。這在Django中很容易實現。在您的tests.py它會是這個樣子

class BlogTest(TestCase): 
    fixtures = ['core/fixtures/test.json'] 

當你這樣做的Django將爲你創造一個測試數據庫,夾具加載到它,運行測試,然後炸燬了數據庫之後的測試完成。如果你想要使用不同的數據庫引擎(我們這樣做是爲了讓我們的測試使用sqlite,因爲它的速度很快),你可以在你的settings.py中設置這樣的東西.py

這將使得它的ID是相同的每一次都會解決你的問題。