On Thu, Mar 15, 2012 at 10:47 AM, msbuck <mbuckn...@usgs.gov> wrote:
> I have used class based views in my latest project. Now I'm trying to write
> tests. I can write tests like the ones I wrote for function based views but
> these seem to me to be functional tests. Does anyone do unit testing on a
> class based view methods? I'm not sure how to go about instantiating a view
> to use in a unit test. And I'm wondering if they are really useful and/or
> worth the effort.



typically, in my projects I try to put most of the functionality in
the models, in most cases the views do little more than selecting the
appropriate model object and show with a template, or just do a little
verifications and call some model's methods.

with that in mind, my full process looks like this in relation to tests:

- first i design the data structure that supports the (yet
unimplemented) functionality.  for this i simply write the models.py
files, repeatedly calling the graph_models command so i have an
auto-refreshing graphical view of the structure.  here i spend some
time thinking what belongs where, splitting and merging apps several
times until they look fine, both in code and in graph.

- when the models diagram fits my vision, i start writing tests for
the basic functionality.  usually at this stage i write them in the
same models.py, and interact only with the models layer.  in a typical
TDD methodology, i write tests that exercise the concepts and then
write the methods to implement them.  sometimes this grows to the
point where i have to split models.py into a subdirectory.

- when the tests do in python most of what i want the user be able to
do via web, i start to write the views, urls and templates.  sometimes
i found i have to write some extra data handling code.  if i can't
push it down to the models, then i try to factor it out to a
'utils.py'.  there i should (and sometimes do) first write tests (now
in tests.py, not in models.py).  still, the tests are kind of 'unit
tests' trying to test the code out of the web environment (but usually
don't bother to write mock objects to please the Unit test idealists)

- finally, when the views are mostly working, i write some tests that
use the test client to do fake web requests.  these tests are not TDD,
and have more 'integration test' flavor.  typically they're there to
ensure user restrictions and to refuse to do some actions or to show
some data when not appropriate.  I use lots of 'assertRedirects' and
'assertNotContains'.


-- 
Javier

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to