Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is actually assertEquals in Python?

I have the following test.py file in django. can you please explain this code?

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',
        )
like image 294
Rockhound Avatar asked Jul 29 '13 09:07

Rockhound


3 Answers

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

Basically it will check if str(contact) == 'John Smith', if not then assert equal is failed and the test is failed and it will notify you the error at that line.

In other words, assertEquals is a function to check if two variables are equal, for purposes of automated testing:

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

Hope it helps.

like image 193
Hieu Nguyen Avatar answered Oct 20 '22 15:10

Hieu Nguyen


assertEquals is a (deprecated) alias for TestCase.assertEqual, which is a method on the unittest.TestCase class.

It forms a test assertion; where str(contact) must be equal to 'John Smith' for the test to pass.

The form with s has been marked as deprecated since 2010, but they've not actually been removed, and there is no concrete commitment to remove them at this point. If you run your tests with deprecation warnings enabled (as recommended in PEP 565) you'd see a warning:

test.py:42: DeprecationWarning: Please use assertEqual instead.
  self.assertEquals(
like image 38
Martijn Pieters Avatar answered Oct 20 '22 14:10

Martijn Pieters


assertEquals is deprecated since Python 3.2, you should use assertEqual (no s).

Or pytest.

like image 2
Boris Avatar answered Oct 20 '22 16:10

Boris