Functional doctest snippet of the day

I was looking for an easy way to get the current browser.contents in my real browser to easy debug what is happening in my doctest.

Here is the snippet:

Updated the 01/13/2009

>>> import tempfile as tmp
>>> def contents():
...    fd, fn = tmp.mkstemp(suffix=".html", prefix="testbrowser-")
...    file = open(fn, 'w')
...    file.write(browser.contents)
...    file.close()
...    print fn
>>> import pdb; pdb.set_trace()

So now you can just call contents() and copy paste the file path in your real browser and see what’s happening in your tests. May it can be added to testbrowser.Browser ?

Advertisements

5 Responses to “Functional doctest snippet of the day”

  1. davisagli Says:

    ZopeTestCase already contains a better way, assuming you’re working in Zope 2:

    from Testing.ZopeTestCase.utils import startZServer
    startZServer()

    Then it will print a port where you can access the same test site that the testbrowser is working with, in a real browser.

  2. merpattersonnet Says:

    If you happen to use emacs, it’s great browse-url.el package proides a browse-url-of-region function. I’ve configured the package to use my system browser, then I just do “print browser.contents” at a Pdb prompt, highlight it, and then invoke that function and it opens in my browser (sans CSS and JS, of course).

    Then there’s also Testing.ZopeTestCase.utils.startZServer:

    http://maurits.vanrees.org/weblog/archive/2009/10/testing-in-plone

  3. @davisagli
    Well I have tested the “startZServer”. It opens a port on localhost but you must replay every things to be in the context of the test.

    So it’s something different (something nice for sure) but not a better way to debug current test.

    @merpattersonnet
    I’m using eclipse + pydev + wtp.

  4. Interesting snippet!

    Unfortunately it don’t work for me. Can you please post a full example and how to use it.

    deeply grateful
    Gomez

  5. It seems that indentation is broken. I m trying here with a pre html tag

    >>> import tempfile as tmp
    >>> def contents():
    ...    fd, fn = tmp.mkstemp(suffix=".html", prefix="testbrowser-")
    ...    file = open(fn, 'w')
    ...    file.write(browser.contents)
    ...    file.close()
    ...    print fn
    >>> import pdb; pdb.set_trace()
    

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: