Is assertIs being misused in Tutorial Part 5?

20 views
Skip to first unread message

John Meyer

unread,
Oct 25, 2018, 7:42:00 PM10/25/18
to Django users
tests.py in the tutorial gives this example:

self.assertIs(future_question.was_published_recently(), False)
This implies that assertIs tests equality of two booleans.  The code works, but not for the right reason, as per the language documentation, the assertIs does not test equality but tests whether two object references point to the same object:

assertIs(firstsecondmsg=None)
assertIsNot(firstsecondmsg=None)
Test that first and second evaluate (or don’t evaluate) to the same object.
New in version 3.1.

This invites tests being written that are testing other kinds of equality and the results are not reliable.  There are a few posts on StackOverflow indicating this confusion is causing confusion in the community.  I want to update the tutorial to use assertEquals or assertTrue.   I'm not in charge of the documentation so I'd settle for posting a bug request for this in the right place, if the point is valid. 

Thoughts?

Andréas Kühne

unread,
Oct 25, 2018, 8:09:30 PM10/25/18
to django...@googlegroups.com
I think it actually is correct, because False is always False - so technically they are correct. However, that being said, I agree with you fully, as long as we change it to self.assertFalse(), which I think is the correct thing to do here.d

Regards, 

Andréas


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4a3852af-4809-45b9-bd34-bcc37ee57966%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Simon Charette

unread,
Oct 25, 2018, 8:23:41 PM10/25/18
to Django users
The reason why assertIs is used instead of assertFalse is that the latter actually tests for falsy values and can sometimes hide bugs.

For example, assertFalse(0) and assertFalse(None) will pass and assertTrue(42) will as well.

If you're testing a method that is supposed to return a boolean value you're better off using assertIs.

Cheers,
Simon

John Meyer

unread,
Oct 27, 2018, 12:20:39 AM10/27/18
to Django users
The reason why assertIs is used instead of assertFalse is that the latter actually tests for falsy values and can sometimes hide bugs.

I understand that if the return type is not pure boolean, it can still evaluate to a boolean value.  I tested the following and it possible to do the type check explicitly.  
      self.assertIs(future_question.was_published_recently(), bool)
self.assertEquals(future_question.was_published_recently(), True)
This would be self-documenting in the sense it would help new folks to avoid using assertIs as  a general equality check on non-boolean types. Would this be okay or would it break with a best practice?


John Meyer

unread,
Oct 27, 2018, 12:32:37 AM10/27/18
to Django users
Ohhhhhhhh, I see what you are saying; False is not really a bool.    When the function returns a false value, the code returns the following:


AssertionError: False is not <class 'bool'>


Reply all
Reply to author
Forward
0 new messages