19

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',
        )
Joe
  • 46,419
  • 33
  • 155
  • 245
Rockhound
  • 407
  • 1
  • 3
  • 9

6 Answers6

33
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.

garrboo
  • 15
  • 4
Hieu Nguyen
  • 8,563
  • 2
  • 36
  • 43
  • 4
    This is true, though you could rewrite any if statement like that (immediately returning either `True` or `False` more succinctly by doing `return var1 == var2`. – Max Goodridge Feb 21 '17 at 14:55
20

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(
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • you linked to `assertEqual` not `assertEquals` – Boris Verkhovskiy Nov 11 '19 at 03:31
  • @Boris: indeed, and I probably didn't even realise that at the time. I've updated this with a note on the fact that it is an alias for `assertEqual` and the fact that it is officially deprecated. – Martijn Pieters Nov 11 '19 at 11:35
  • Is there any convention for the argument order? `assertEqual("expected", calculated)` or `assertEqual(calculated, "expected")`? – Eric Duminil Jun 27 '21 at 20:29
  • 1
    @EricDuminil: not that I am aware of. I personally always use `calculated, "expected"`, but that's not set in stone. Free you must feel, [Yoda assertions](https://en.wikipedia.org/wiki/Yoda_conditions) you can use! – Martijn Pieters Jul 02 '21 at 17:08
2

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

Or pytest.

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
0

The assertEquals set your test as passed if the __str__ of your contact object returns 'John Smith`. This is part of unit tests, you should check the official documentation

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
0

Syntax: assertEqual(first, second, msg=None)

Test that first and second are equal. If the values do not compare equal, the test will fail.In addition it will also check if first and second are the exact same type and one of list, tuple, dict, set, frozenset or unicode.

in your case it will check will check if str(contact) == 'John Smith', if not then assert equal is failed.

Ami Patel
  • 296
  • 1
  • 13
0

assertEquals tests whether two variables are equal to each other.

Philpot
  • 216
  • 5
  • 17