2013-03-17 19 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是相同的每一次都会解决你的问题。