无需运行瓶子的测试瓶应用程序

2024-06-11 20:07:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我遵循框架的Recipes。在

当我尝试下面的代码

#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)

^{}的函数测试用例

^{pr2}$

当我运行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>&#039;http://localhost:80/hello&#039;</tt>
               caused an error:</p>
            <pre>Internal Server Error</pre>
        </body>
    </html>


----------------------------------------------------------------------
Ran 1 test in 0.008s

FAILED (errors=1)

QuickStartTestApp提到。在

If your WSGI application requires any configuration, you must set that up manually in your tests.

如何配置?在

它需要,瓶子服务器正在运行,有没有办法在不运行服务器的情况下测试瓶子应用程序?在


Tags: inpytestapphellogetvenvlib
1条回答
网友
1楼 · 发布于 2024-06-11 20:07:58

谢谢你发布堆栈跟踪。它清楚地表明,正是这条线导致了500:

assert app.get('/hello').status_code == 200

为什么不打印app.get('/hello').status_code的值以便了解发生了什么?在

我也很确定您应该检查status_int,而不是{}。在

^{pr2}$

相关问题 更多 >