본문 바로가기
파이썬

파이썬 What is actually assertEquals in Python?

by º기록 2021. 1. 12.
반응형

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',
        )

 

해결 방법

 

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 (contact) == 'John Smith'인지 확인하고 그렇지 않은 경우 같음이 실패하고 테스트가 실패하고 해당 줄에서 오류를 알려줍니다.

즉, assertEquals는 자동화 된 테스트를 위해 두 변수가 같은지 확인하는 함수입니다.

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

도움이 되었기를 바랍니다.

 

참조 페이지 https://stackoverflow.com/questions/17920625

 

 

반응형

댓글