2013-07-29 56 views
1

我在django中有以下test.py文件。你能解釋一下這段代碼嗎?Python中的實際assertEquals是什麼?

from contacts.models import Contact 
... 
class ContactTests(TestCase): 
    """Contact model tests.""" 

    def test_str(self): 

     contact = Contact(first_name='John', last_name='Smith') 

     self.assertEquals(
      str(contact), 
      'John Smith', 
     ) 
+1

它會檢查str(聯繫人)=='John Smith',如果不是那麼斷言相等是失敗 –

+0

你問過寫這個的人嗎? –

+1

你可以請定義什麼assertEquals是什麼? – Rockhound

回答

7
from contacts.models import Contact # import model Contact 
... 
class ContactTests(TestCase): # start a test case 
    """Contact model tests.""" 

    def test_str(self): # start one test 

     contact = Contact(first_name='John', last_name='Smith') # create a Contact object with 2 params like that 

     self.assertEquals( # check if str(contact) == 'John Smith' 
      str(contact), 
      'John Smith', 
     ) 

基本上,它會檢查是否STR(接觸)==「約翰·史密斯」,如果沒有則斷言等於失敗和測試失敗,它會通知你在該行的錯誤。

換句話說,是的assertEquals功能檢查,如果兩個變量是相等的,用於自動化測試的目的:

def assertEquals(var1, var2): 
    if var1 == var2: 
     return True 
    else: 
     return False 

希望它能幫助。

+0

這是真的,儘管你可以重寫任何if語句(通過執行'return var1 == var2'來更簡潔地返回'True'或'False'。 –

-1

的assertEquals將設置你的測試,如果你contact對象返回「約翰Smith`的__str__爲通過。這是單元測試的一部分,你應該檢查the official documentation

-1

語法:assertEqual(first, second, msg=None)

測試該第一和第二相等。如果值不相等,則測試將失敗。此外,它還將檢查第一個和第二個是否與列表,元組,字典,集合,凍結集或unicode完全相同。

在你的情況下,它將檢查將檢查if str(contact) == 'John Smith',如果不是,那麼assert平等是失敗的。

-1

assertEquals測試兩個變量是否彼此相等。

相關問題