我有一个只有 POST 方法实现的 python FLASK 视图。我不需要 GET,因为外部模板会将数据发布到此视图。现在我想为此视图编写单元测试,我想在其中模拟初始 POST 请求并断言视图实现正常工作。
将访问我的视图实现的外部模板示例:
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Author" CONTENT="Richard Ward">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="Test Page">
</HEAD>
<BODY>
<FORM METHOD="POST" ACTION="www.myView.com/testview">
<INPUT TYPE="hidden" NAME="employeeId" value="304253498022">
<INPUT TYPE="hidden" NAME="employeeName" value="testName">
<INPUT TYPE="submit" name="submit" value="Show Information">
</FORM>
</BODY>
</HTML>
我的视图类
class TestView(MethodView):
app = None
def post(self):
"""
Implementation of the post request for this view
"""
employeeId= request.form.get('employeeId', None)
employeeName = request.form.get('employeeName', None)
# some data processing code
return render_template("summary.html")
@classmethod
def registerSelf(cls, app):
"""
Registers itself as a view with the passed Flask application
"""
TestView.app = app
app.flaskApp.add_url_rule('/testview', view_func=TestView.as_view('testview'))
我想直接调用我的视图的帖子并在请求的正文中传递模拟表单数据