Roc*_*und 9 python django django-tests
我在django中有以下test.py文件.你能解释一下这段代码吗?
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',
)
Run Code Online (Sandbox Code Playgroud)
Hie*_*yen 18
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',
)
Run Code Online (Sandbox Code Playgroud)
基本上它会检查str(contact)=='John Smith',如果没有则断言相等失败并且测试失败并且它将通知你该行的错误.
换句话说,assertEquals是一个检查两个变量是否相等的函数,用于自动化测试:
def assertEquals(var1, var2):
if var1 == var2:
return True
else:
return False
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.