Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 885
Travis Avatar asked Jan 01 '11 23:01

Travis


People also ask

What is Django test client?

The test client is a Python class that acts as a dummy web browser, allowing you to test your views and interact with your Django-powered application programmatically.

How does Django do testing?

The preferred way to write tests in Django is using the unittest module built-in to the Python standard library. This is covered in detail in the Writing and running tests document. You can also use any other Python test framework; Django provides an API and tools for that kind of integration.


1 Answers

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.

like image 200
Travis Avatar answered Oct 07 '22 10:10

Travis