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.
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(
- [Django]-Django :How to integrate Django Rest framework in an existing application?
- [Django]-Django proxy model and ForeignKey
- [Django]-How do you dynamically hide form fields in Django?
2👍
assertEquals
is deprecated since Python 3.2, you should use assertEqual
(no s
).
Or pytest
.
- [Django]-Django multiple template inheritance – is this the right style?
- [Django]-How does django handle multiple memcached servers?
- [Django]-Django dynamic forms – on-the-fly field population?
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
- [Django]-Name '_' is not defined
- [Django]-Django Footer and header on each page with {% extends }
- [Django]-Unittest Django: Mock external API, what is proper way?
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.
- [Django]-Adding css class to field on validation error in django
- [Django]-Alowing 'fuzzy' translations in django pages?
- [Django]-How to serve media files on Django production environment?
- [Django]-Django composite unique on multiple model fields
- [Django]-Github issues api 401, why? (django)
- [Django]-Python (and Django) best import practices