在不运行Bottle Server的情况下测试Bottle App [英] Test bottle app without running bottle server
问题描述
我正在关注
I am following Recipes of bottle framework.
当我尝试下面的代码
#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
时,出现以下错误。
When I run nosetests test_mywebapp.py
I got below error.
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)
快速入门,在 TestApp
提及。
如果您的WSGI应用程序需要任何配置,则必须在测试中手动设置
。
If your WSGI application requires any configuration, you must set that up manually in your tests.
我该如何配置?
需要运行瓶子服务器,有什么方法可以在不运行服务器的情况下测试瓶子应用?
It needs, bottle server running, is there any way to test bottle app without running server?
推荐答案
感谢发布堆栈跟踪。它清楚地表明这是导致500的行:
Thanks for posting the stack trace. It clearly indicates that this is the line that is causing the 500:
assert app.get('/hello').status_code == 200
为什么不打印 app.get的值('/ hello')。status_code
,以便您可以了解正在发生的事情?
Why don't you print the value of app.get('/hello').status_code
so you can learn what's happening?
我也很确定您应该检查 status_int
,而不是 status_code
。
I'm also pretty sure that you should be checking status_int
, not status_code
.
assert app.get('/hello').status_int == 200
这篇关于在不运行Bottle Server的情况下测试Bottle App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!