【发布时间】:2016-06-17 15:48:08
【问题描述】:
当我尝试下面的代码时
#filename: mywebapp.py
from bottle import Bottle, run, request
app = Bottle()
@app.get('/hello')
def hello():
return "Hello " + request.get_header('name')
if __name__ == '__main__':
run(app, host='localhost', port=80)
带有TestApp的函数测试用例
#filename: test_mywebapp.py
from webtest import TestApp
import mywebapp
def test_functional_hello_world():
app = TestApp(mywebapp.app)
assert app.get('/hello').status_code == 200
assert app.get('/hello', headers=dict(name='World!')).text == 'Hello World!'
当我运行 nosetests test_mywebapp.py 时,出现以下错误。
nosetests test_mywebapp.py
E
======================================================================
ERROR: test_mywebapp.test_functional_hello_world
----------------------------------------------------------------------
Traceback (most recent call last):
File "/private/tmp/venv/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/private/tmp/test_mywebapp.py", line 6, in test_functional_hello_world
assert app.get('/hello').status_code == 200
File "/private/tmp/venv/lib/python2.7/site-packages/webtest/app.py", line 327, in get
expect_errors=expect_errors)
File "/private/tmp/venv/lib/python2.7/site-packages/webtest/app.py", line 636, in do_request
self._check_status(status, res)
File "/private/tmp/venv/lib/python2.7/site-packages/webtest/app.py", line 668, in _check_status
res)
AppError: Bad response: 500 Internal Server Error (not 200 OK or 3xx redirect for http://localhost/hello)
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head>
<title>Error: 500 Internal Server Error</title>
<style type="text/css">
html {background-color: #eee; font-family: sans;}
body {background-color: #fff; border: 1px solid #ddd;
padding: 15px; margin: 15px;}
pre {background-color: #eee; border: 1px solid #ddd; padding: 5px;}
</style>
</head>
<body>
<h1>Error: 500 Internal Server Error</h1>
<p>Sorry, the requested URL <tt>'http://localhost:80/hello'</tt>
caused an error:</p>
<pre>Internal Server Error</pre>
</body>
</html>
----------------------------------------------------------------------
Ran 1 test in 0.008s
FAILED (errors=1)
QuickStart TestApp 提及。
如果您的 WSGI 应用程序需要任何配置,您必须设置 在您的测试中手动设置。
我该如何配置?
需要,bottle server 正在运行,有什么方法可以在不运行 server 的情况下测试 bottle 应用程序?
【问题讨论】:
-
是什么让您认为“它需要运行瓶服务器?”它不应该。
-
@ron.rothman,当我运行这个 UT 时,它给出了错误,无法连接
http://localhost/login:( -
请添加回溯,以便我们查看发生了什么。了解
mywebapp中的内容也很有帮助。 -
@ron.rothman 我用代码更新了我的问题。
标签: bottle python paste functional-testing bottle testapp