【发布时间】:2015-08-07 00:44:05
【问题描述】:
我在使用 web py 框架进行自动化测试时遇到问题。 我正在经历最后一个艰难地学习python的练习。在本练习中,我们制作了一个运行房间地图的 Web 应用程序“引擎”。 我希望能够自动测试每个房间,但有一个问题,引擎依赖于前一个房间来决定下一个房间(和用户输入)。
if web.config.get("_session") is None:
store = web.session.DiskStore("sessions")
session = web.session.Session(app, store, initializer={"room":None})
web.config._session = session
else:
session = web.config._session
这个类处理发送到/的GET请求
class Index(object):
def GET(self):
session.room = map.START
web.seeother("/game")
这个类处理对 /game 的 GET 和 POST 请求
class GameEngine(object):
def GET(self):
if session.room:
return render.show_room(room=session.room)
else:
return render.you_died()
def POST(self):
form = web.input(action=None)
if session.room and form.action:
session.room = session.room.go(form.action)
web.seeother("/game")
在我的自动化测试中,我使用了两件事:首先我使用 app.request API:
app.request(localpart='/', method='GET',data=None,
host='0.0.0.0:8080', headers=None, https=False)
创建一个响应对象,例如:
resp = app.request("/game", method = "GET")
其次,我将 resp 对象传递给该函数以检查某些事情:
from nose.tools import *
import re
def assert_response(resp, contains=None, matches=None, headers=None,
status="200"):
assert status in resp.status, "Expected response %r not in %r" %
(status, resp.status)
if status == "200":
assert resp.data, "Response data is empty"
if contains:
assert contains in resp.data, "Response does not contain %r" %
contains
if matches:
reg = re.compile(matches)
assert reg.matces(resp.data), "Response does not match %r" %
matches
if headers:
assert_equal(resp.headers, headers)
我们可以将变量作为字典传递给 API app.request 中的关键字参数数据,以修改 web.input()。
我的问题是:在我的自动化测试模块中,我们如何“传递”一个覆盖会话中初始化程序字典中房间值的值:
session = web.session.Session(app, store, initializer={"room":None})
在应用模块中通过设置完成
session.room = map.START
然后 session.room 更新使用:
if session.room and form.action:
session.room = session.room.go(form.action)
感谢您抽出宝贵时间阅读本文,我们将不胜感激!
【问题讨论】:
标签: python-2.7 automated-tests web.py