【发布时间】:2014-10-11 16:57:35
【问题描述】:
我有一个自定义的 HTTP 请求处理程序,可以简化为:
# Python 3:
from http import server
class MyHandler(server.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
# Here's where all the complicated logic is done to generate HTML.
# For clarity here, replace with a simple stand-in:
html = "<html><p>hello world</p></html>"
self.wfile.write(html.encode())
我想对这个处理程序进行单元测试(即确保我的do_GET 无异常执行)而不实际启动 Web 服务器。是否有任何轻量级的方法来模拟 SimpleHTTPServer 以便我可以测试这段代码?
【问题讨论】:
-
import mock和一点阅读应该可以解决问题
标签: python unit-testing simplehttpserver