उपयोग करने के लिए किए गए दस्तावेज़ों के अनुसार:Django जब टियरडाउन विधि
एक testcase, दूसरे हाथ पर, टेबल काट-छांट नहीं करता है और एक परीक्षण की शुरुआत में प्रारंभिक डेटा लोड करें। इसके बजाय, यह एक डेटाबेस लेनदेन है कि परीक्षण के अंत में वापस लुढ़का हुआ है में परीक्षण कोड encloses। यह भी किसी भी प्रतिबद्ध जारी करने या डेटाबेस पर रोलबैक संचालन, यह सुनिश्चित करें कि परीक्षण के अंत में रोलबैक अपनी प्रारंभिक अवस्था में डेटाबेस पुनर्स्थापित करता है से परीक्षण के अंतर्गत कोड को रोकता है। में यह गारंटी देने के लिए कि सभी टेस्टकेस कोड एक स्वच्छ डेटाबेस से शुरू होते हैं, Django परीक्षण धावक पहले सभी टेस्टकेस परीक्षण चलाता है, किसी भी अन्य परीक्षण (उदा। सिद्धांत) से पहले डेटाबेस को इसके मूल स्थिति में बहाल कर सकता है।
तो अगर मैं एक परीक्षण है कि इस तरह दिखता है:
class GeneralUserCreateTest(TestCase):
def setUp(self):
create_roletypes()
create_permissiontypes()
self.client = Client()
self.event = create_event()
def test_create(self):
create_url = reverse('event_user_signup', args=[self.event.slug])
post_data = {
'signup-account-email': '[email protected]',
'signup-account-password': 'foobar',
'signup-account-password2': 'foobar',
'signup-account-first_name': 'Foo',
'signup-account-last_name': 'Bar',
}
response = self.client.post(create_url, data=post_data)
self.assertEqual(response.status_code, 302)
# check creation of user object
self.assertEqual(User.objects.filter(email=post_data['signup-account-email']).count(), 1)
user = User.objects.get(username=post_data['signup-account-email'])
# user and profile objects created
self.assertEqual(User.objects.all().count(), 1)
self.assertEqual(Profile.objects.all().count(), 1)
# get the first user and profile object to test against submitted field
user = User.objects.all()[0]
profile = Profile.objects.all()[0]
role = Role.objects.filter(event=self.event, profiles=profile)[0]
self.assertEqual(role.roletype.name, 'General')
self.assertEqual(user.username, post_data['signup-account-email'])
self.assertEqual(user.email, post_data['signup-account-email'])
self.assertEqual(profile.first_name, post_data['signup-account-first_name'])
self.assertEqual(profile.last_name, post_data['signup-account-last_name'])
यह अभी भी एक teardown
विधि को चलाने के लिए आवश्यक है या TestCase
वर्ग यह की देखभाल करता है? यदि हां, तो जब एक teardown
विधि TestCase
वर्ग की उपलब्धता को देखते हुए इस्तेमाल करना चाहिए?