Login

View response's content in a browser while testing

Author:
ryankask1
Posted:
January 13, 2011
Language:
Python
Version:
1.2
Score:
0 (after 0 ratings)

I often insert pdb.set_trace() in my test cases to debug and examine behavior. When tests fail with assertions like assertContains(response, 'Some text'), it would be useful to see the response's contents in a browser window. This snippet does just that. Simply put this code in a python script on your PYTHONPATH and import/call the function when the debugger starts.

Only tested on Ubuntu and you might want to change URL_OPENER to whatever you want to open the URLs. Simple, but hopefully useful.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import os, tempfile, subprocess

URL_OPENER = 'xdg-open'

def open_response_content(response):
    """ Saves a response's content to a temporary file and opens it in a
    browser. """
    temp_file = tempfile.NamedTemporaryFile(delete=False)
    temp_file.write(response.content)
    dev_null = open(os.devnull, 'w')
    kwargs = {'stdout': dev_null, 'stderr': dev_null}
    subprocess.Popen([URL_OPENER, temp_file.name], **kwargs)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months, 1 week ago
  5. Help text hyperlinks by sa2812 12 months ago

Comments

Please login first before commenting.