2

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.

optixx
  • 2,070
  • 3
  • 16
  • 15

5 Answers5

4

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?

nosklo
  • 205,639
  • 55
  • 286
  • 290
2

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

Look at werkzeug.

S.Lott
  • 373,146
  • 78
  • 498
  • 766
2

Take a look the standard module wsgiref:

https://docs.python.org/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.

JasonMArcher
  • 13,296
  • 21
  • 55
  • 51
ashcatch
  • 2,297
  • 1
  • 17
  • 29
0

I would look into Django.

Andrew Hare
  • 333,516
  • 69
  • 632
  • 626
0

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.

dbr
  • 159,759
  • 65
  • 272
  • 339