On 5 January 2013 05:23, Malcolm Box <[email protected]> wrote:
>
> The general pattern I want to implement is have a test client that makes 
> assertions about all the requests made during a set of tests. For example, it 
> could check that every get() returned cache headers, or that content_type is 
> always specified in responses. Or that the response has certain HTML, uses 
> certain templates etc - ie all of the assertion testing goodness in 
> django.test.TestCase.
>
> My concrete use case is a JSONClient that adds a json_get / json_post methods 
> which makes sure to setup content_type etc on the call, and then validates 
> that what came back was valid JSON.
>
> The simple, wrong way is to do:
>
> def check_response(self, response):
>    self.assertContains(response, ....)
>    ....
>
> def test():
>    r = self.client.get(...)
>    self.check_response(r)
>
> but this is error prone, verbose etc etc.
>
> The right thing is that within a test suite, the get()/post() etc to do the 
> checks for me - and so it should be possible to create a testclient that does 
> this, and be able to use this testclient in various test suites.

I don't know that an appeal to authority is useful here, but I
disagree with your "wrong" and "right" assessments.  Have you heard of
the concept of the "four phase test"?
http://xunitpatterns.com/Four%20Phase%20Test.html

One of the things about tests is that they need to be understandable
at a glance.  Having the "exercise" and "verify" steps clearly
separated helps with this IMHO, and the approach you're proposing
doesn't really lend itself to this.

(If your local library or a friend or someone has the book "xUnit Test
Patterns", the first 150 pages or so are extremely well worth reading)

Cheers,
mwh

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

Reply via email to