Login

View response's content in a browser while testing

Author:
ryankask1
Posted:
January 13, 2011
Language:
Python
Version:
1.2
Tags:
debug test response
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. Load response.content in browser (for debugging) by tin_nqn 3 years, 3 months ago
  2. Simple Exception Response for AJAX debugging by newmaniese 7 years, 5 months ago
  3. "Zoom in" on rendered HTML that the test client returns by peterbe 6 years, 4 months ago
  4. XhtmlDegraderMiddleware by dmh 8 years, 1 month ago
  5. Variable._resolve_lookup monkeypatch by showell 5 years, 9 months ago

Comments

Please login first before commenting.