On Friday 04 January 2013, Malcolm Box 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.
> 

No, you're mixing concerns.

> Is there a simple, clean way to solve this that I'm missing?
> 

class MyTestCase(TestCase):

        def check_response(self, response):
                self.assertContains(response, ....)

        def checked_get(self, *args, **kw):
                r = self.client.get(*args, **kw)
                self.check_response(r)
                return r

class SpecificTest(MyTestCase):

        def test():
                r = self.checked_get(...)
                ...

That's how I would do it.

Shai.

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

Reply via email to