Tuesday, May 3, 2011

Simple webserver or web testing framework

Need to testcase a complex webapp which does some interacting with a remote 3rd party cgi based webservices. Iam planing to implement some of the 3rd party services in a dummy webserver, so that i have full controll about the testcases. Looking for a simple python http webserver or framework to emulate the 3rd party interface.

From stackoverflow
  • I would look into Django.

  • You might be happiest with a WSGI service, since it's most like CGI.

    Look at werkzeug.

  • Take a look the standard module wsgiref:

    http://www.python.org/doc/2.6/library/wsgiref.html

    At the end of that page is a small example. Something like this could already be sufficient for your needs.

    nosklo : +1: I agree that included wsgiref server is probably enough for his needs.
  • Use cherrypy, take a look at Hello World:

    import cherrypy
    
    class HelloWorld(object):
        def index(self):
            return "Hello World!"
        index.exposed = True
    
    cherrypy.quickstart(HelloWorld())
    

    Run this code and you have a very fast Hello World server ready on localhost port 8080!! Pretty easy huh?

  • It might be simpler sense to mock (or stub, or whatever the term is) urllib, or whatever module you are using to communicate with the remote web-service?

    Even simply overriding urllib.urlopen might be enough:

    import urllib
    from StringIO import StringIO
    
    class mock_response(StringIO):
        def info(self):
            raise NotImplementedError("mocked urllib response has no info method")
        def getinfo():
            raise NotImplementedError("mocked urllib response has no getinfo method")
    
    def urlopen(url):
        if url == "http://example.com/api/something":
            resp = mock_response("<xml></xml>")
            return resp
        else:
            urllib.urlopen(url)
    
    
    is_unittest = True
    
    if is_unittest:
        urllib.urlopen = urlopen
    
    print urllib.urlopen("http://example.com/api/something").read()
    

    I used something very similar here, to emulate a simple API, before I got an API key.

0 comments:

Post a Comment