Unit testing Web applications

Ryan Ginstrom software at ginstrom.com
Wed Mar 5 20:51:58 EST 2008


> On Behalf Of Monica Leko
> Does Python has some testing frameworks for testing Web 
> applications (like Cactus and HttpUnit for Java), generating 
> requests and checking if the response is correct?

I have got a lot of traction using mechanize [1] with nose [2]. Of course
that still leaves out testing JavaScript. For that, something like PAMIE [3]
is one way to go.

[1] http://wwwsearch.sourceforge.net/mechanize/
[2] http://somethingaboutorange.com/mrl/projects/nose/
[3] http://pamie.sourceforge.net/

Here's an example of how I use mechanize + nose:

# test_index.py
from mechanize import Browser

class TestPageLoads:

    def setup(self):
        self.mech = Browser()
        self.mech.set_handle_robots(False) # use thought and
consideration...
    
    def test_nonexistent(self):
        try:
            response =
self.mech.open("http://honyaku-archive.org/nonexistent/")
            assert False, "Should have thrown here"
        except Exception, e:
            assert "404" in str(e), e

    def test_index(self):
        response = self.mech.open("http://honyaku-archive.org/")
        assert response.code == 200, response.code

    def test_index_title(self):
        response = self.mech.open("http://honyaku-archive.org/")
        assert self.mech.title().strip() == "Honyaku Archive :: Home",
self.mech.title()

Regards,
Ryan Ginstrom




More information about the Python-list mailing list