canonical-django team mailing list archive
-
canonical-django team
-
Mailing list archive
-
Message #00018
Django testing best practices
Hi,
We're starting some work on Djano projects within Linaro, and at our
sprint last week we had some discussion on how we wanted to approach
testing. I'd like to present a summary of that discussion here in an
attempt to start some discussion and get some ideas from more
experienced people. I'd like to know about approaches you take, how you
split things us, any extra projects you make use of, the types of tests
that you find valuable, and which are more pain than they are worth.
I've found from my searches that there isn't too much information about
the subject beyond the basics, such as how to use the test client, so if
anyone has some more detailed resources on best practices, then that
would be appreciated too. Also, pointers to Django projects that have
comprehensive, tasteful, test suites would help.
Testing Models
==============
First we spoke a bit about testing models.
We weren't sure about appropriate tests for the basics of models, such
as creating an object and checking that it has an attribute that was
defined via a field. This is hampered as in my testing some things about
the field, such as limits on the length of a string don't seem to be
enforced in a test environment (with the sqlite3 backend).
As we add more behaviour to models we will be testing that, and it was
agreed that this was where most of the logic should be pushed too, so
views are as simple as possible.
Testing Views
=============
Django has facilities for testing that a view rendered using a
particular template, and the paramaters that were passed to a template,
and we agreed that it would be worthwhile trying to take advantage of
that.
We also agreed that writing some integration tests that retrieve a page
and test some parts of the HTML that is returned would be
worthwhile. This would only test the simple cases though, and not try to
exhaustively test edge cases, due to the poor failure modes of this
test. In addition we will be avoiding string comparisons, as they are
far too brittle.
If there are lots of ideas then I will collate them in to a wiki page
that we can maintain we learn more.
Thanks,
James
Follow ups