【问题标题】:Simple webserver or web testing framework简单的网络服务器或网络测试框架
【发布时间】:2010-10-21 01:28:52
【问题描述】:

需要测试一个复杂的 web 应用程序,该应用程序与远程 3rd 方基于 cgi 的 web 服务进行交互。 我计划在一个虚拟网络服务器中实现一些第 3 方服务,这样我就可以完全控制测试用例。 正在寻找一个简单的 python http webserver 或框架来模拟第 3 方接口。

【问题讨论】:

    标签: python web-services testing web-applications


    【解决方案1】:

    使用cherrypy,看看Hello World:

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

    运行此代码,您将在localhost 端口8080 上准备好一个非常快速的Hello World 服务器!很简单吧?

    【讨论】:

      【解决方案2】:

      您可能对 WSGI 服务最满意,因为它最像 CGI。

      werkzeug

      【讨论】:

        【解决方案3】:

        看看标准模块wsgiref:

        https://docs.python.org/2.6/library/wsgiref.html

        该页面的末尾是一个小示例。这样的东西可能已经足够满足您的需求了。

        【讨论】:

        • +1:我同意包含的 wsgiref 服务器可能足以满足他的需求。
        【解决方案4】:

        我会调查Django

        【讨论】:

          【解决方案5】:

          模拟(或存根,或任何术语)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()
          

          在获得 API 密钥之前,我使用了非常相似的 here 来模拟一个简单的 API。

          【讨论】:

            猜你喜欢
            • 2011-07-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-09-14
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多