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.
Asked
Active
Viewed 2,128 times
5 Answers
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
- 217,122
- 57
- 293
- 297
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
- 14,195
- 22
- 56
- 52

ashcatch
- 2,327
- 1
- 18
- 29
-
+1: I agree that included wsgiref server is probably enough for his needs. – nosklo Apr 22 '09 at 12:22
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
- 165,801
- 69
- 278
- 343