Search code examples
pythondjangoapidjango-testing

Can Django Test Client Be Used for API Calls in Production?


I'm building a Django app with an API built on Piston. For the sake of keeping everything as DRY as possible and the API complete, I'd like my internal applications to call the API rather than the models (kind of a proxy-view-controller a la https://github.com/raganwald/homoiconic/blob/master/2010/10/vc_without_m.md but all on one django install for now). So the basic setup is:

Model -> API -> Application -> User Client

I can overload some core Piston classes to create an internal client interface for the application, but I'm wondering if I could just use the Django Test Client to accomplish the same thing. So to create an article, rather than calling the model I would run:

from django.test.client import Client
c = Client()
article = c.post('/api/articles', {
  'title' : 'My Title',
  'content' : 'My Content'
})

Is there a reason I shouldn't use the test client to do this? (performance, for instance) Is there a better tool that's more tailored for this specific purpose?


Solution

  • After reviewing the code for TestClient, it doesn't appear to have any additional overhead related to testing. Rather, it just functions as a basic client for internal requests. I'll be using the test client as the internal client, and using Piston's DjangoEmitter to get model objects back from the API.

    Only testing will tell whether the internal request mechanism is too much of a performance hit.