Python 简单web服务器或web测试框架
需要测试一个复杂的webapp,它可以与基于cgi的远程Web服务进行交互。 我计划在虚拟Web服务器中实现一些第三方服务,这样我就可以完全控制测试用例。Python 简单web服务器或web测试框架,python,web-services,testing,web-applications,Python,Web Services,Testing,Web Applications,需要测试一个复杂的webapp,它可以与基于cgi的远程Web服务进行交互。 我计划在虚拟Web服务器中实现一些第三方服务,这样我就可以完全控制测试用例。 正在寻找一个简单的python http Web服务器或框架来模拟第三方界面。我将深入研究。您可能最喜欢WSGI服务,因为它最像CGI 查看。查看标准模块wsgiref: 在那一页的末尾是一个小例子。像这样的东西已经足够满足您的需要。使用,看看Hello World: import cherrypy class HelloWorld(ob
正在寻找一个简单的python http Web服务器或框架来模拟第三方界面。我将深入研究。您可能最喜欢WSGI服务,因为它最像CGI
查看。查看标准模块wsgiref: 在那一页的末尾是一个小例子。像这样的东西已经足够满足您的需要。使用,看看Hello World:
import cherrypy
class HelloWorld(object):
def index(self):
return "Hello World!"
index.exposed = True
cherrypy.quickstart(HelloWorld())
运行此代码,您将在
localhost
端口8080
上有一个非常快速的Hello World服务器!!很简单吧?模拟(或存根,或任何术语)urllib,或您用来与远程web服务通信的任何模块可能更简单
即使简单地覆盖urllib.urlopen
也可能足够了:
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()
导入urllib
从StringIO导入StringIO
类模拟_响应(StringIO):
def信息(自我):
raise NOTEImplementedError(“模拟urllib响应没有info方法”)
def getinfo():
raise NOTEImplementedError(“模拟urllib响应没有getinfo方法”)
def urlopen(url):
如果url==”http://example.com/api/something":
resp=模拟_响应(“”)
返回响应
其他:
urllib.urlopen(url)
_unittest=True吗
如果是单元测试:
urllib.urlopen=urlopen
打印urlib.urlopen(“http://example.com/api/something)改为
在获得API密钥之前,我使用了非常类似的方法来模拟一个简单的API。+1:我同意,包含的wsgiref服务器可能足以满足他的需要。