Re: unittest.TestCase vs. django.test.TestCase in overview example

217 views
Skip to first unread message

Tim Graham

unread,
Mar 25, 2013, 2:08:54 PM3/25/13
to django-d...@googlegroups.com, Brian Schott
It seems like it could be a dangerous precedent to cater to people who don't take the time to fully read the docs, but in this case I'm a bit sympathetic. On the other hand, this example will probably be a bit more obvious when we drop support for Python 2.6 and no longer have django.utils.unittest. At the least, we could probably move the warning above the example so it's a bit more visible.

On Saturday, March 16, 2013 8:27:01 PM UTC-4, Lorin Hochstein wrote:
Hi there:

On the Django testing overview doc page <https://docs.djangoproject.com/en/dev/topics/testing/overview/>, the initial example uses unittest.TestCase. A Django developer who was looking for a quick reminder on how to write unit tests is likely to hit this page first. If that developer doesn't read the "warning" section below, they could mistakenly use unittest.TestCase when their unit tests change  the database. This very scenario happened to a colleague of mine.

I proposed changing this to django.test.TestCase <https://github.com/django/django/pull/903>, but that pull request with closed out by Aymeric Augustin, with reference to <https://code.djangoproject.com/ticket/15896>. I don't think ticket #15986 covers quite the same issue, despite its title. Django devs, can you reconsider this doc patch?

Take care,

Lorin

Lorin Hochstein

unread,
Mar 30, 2013, 4:53:11 PM3/30/13
to django-d...@googlegroups.com, Brian Schott
Will dropping support for Python 2.6 have any effect here? I assumed the example would just switch to using unittest.TestCase instead of django.utils.unittest.TestCase, and the usability issue would remain.

I think having the warning above the example would help, but I'd still prefer having the (slower but less likely to cause an error in a different context) django.test.TestCase used in the initial example.

Brian Schott

unread,
Mar 30, 2013, 11:30:44 PM3/30/13
to django-d...@googlegroups.com, Brian Schott
There is really a bigger question in my mind about the appropriateness of using django.utils.unittest in the testing overview section.  It is an optimization that the warning admits is fairly limited for real testing and is premature for the first example.  It would be better to break out the optimization as a Note "if your tests don't rely on database access, you can ... optimize with django.utils import...".  The testing overview section should import the class that works correctly when testing Animal.objects.create() or self.lion.save().

It's also not a case of catering.  Enclosed is a link to a screen shot that starts with "Writing tests" and ends with "For more details about unittest, see the Python documentation".
Nowhere on that first screen of documentation on a 20 inch monitor does it refer to django.test.TestCase and "For more details..." reads like the end of the section.  It's really easy to not see the warning and the first two examples of what someone glancing at the docs to manually create a tests.py file will read is "from django.utils import unittest". which causes a very non-obvious unit test failures in the most typical test cases.  

Thanks for reconsidering the patch!  Lorin's version is much clearer.
Brian

--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Lorin Hochstein

unread,
Mar 31, 2013, 4:44:06 PM3/31/13
to django-d...@googlegroups.com, Brian Schott
I created a Django Trac ticket for this: https://code.djangoproject.com/ticket/20165
Reply all
Reply to author
Forward
0 new messages