gpt4 book ai didi

python - 使用 Webtest 和/或 nose 测试 cherrypy WSGI 应用程序?

转载 作者:行者123 更新时间:2023-11-28 20:09:09 25 4
gpt4 key购买 nike

如何使用 json 或 html 结果广泛测试 cherrypy 应用程序 nosewebtest使用 WSGI 接口(interface)?

任何示例将不胜感激。

最佳答案

这个例子可能有帮助:

from webtest import TestApp
from nose.tools import eq_

class TestAuthentication(object):
"""
Tests for the default authentication setup.

If your application changes how the authentication layer is configured
those tests should be updated accordingly
"""

application_under_test = 'main'

def setUp(self):
"""Method called by nose before running each test"""
# Loading the application:
conf_dir = config.here
wsgiapp = loadapp('config:test.ini#%s' % self.application_under_test,
relative_to=conf_dir)
self.app = TestApp(wsgiapp)
# Setting it up:
test_file = path.join(conf_dir, 'test.ini')
cmd = SetupCommand('setup-app')
cmd.run([test_file])

def tearDown(self):
"""Method called by nose after running each test"""
# Cleaning up the database:
model.DBSession.remove()
teardown_db()

def test_forced_login(self):
"""Anonymous users are forced to login

Test that anonymous users are automatically redirected to the login
form when authorization is denied. Next, upon successful login they
should be redirected to the initially requested page.

"""
# Requesting a protected area
resp = self.app.get('/secc/', status=302)
assert resp.location.startswith('http://localhost/login')
# Getting the login form:
resp = resp.follow(status=200)
print resp.forms
form = resp.forms
# Submitting the login form:
form['login'] = u'manager'
form['password'] = 'managepass'
post_login = form.submit(status=302)
# Being redirected to the initially requested page:
assert post_login.location.startswith('http://localhost/post_login')
initial_page = post_login.follow(status=302)
assert 'authtkt' in initial_page.request.cookies, \
"Session cookie wasn't defined: %s" % initial_page.request.cookies
assert initial_page.location.startswith('http://localhost/secc/'), \
initial_page.location

关于python - 使用 Webtest 和/或 nose 测试 cherrypy WSGI 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13902103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com