Test bottle app without running bottle server

こ雲淡風輕ζ 提交于 2019-12-23 03:00:13

问题


I am following Recipes of bottle framework.

When I try below code

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

Function testcase with 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!'

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

QuickStart on TestApp mention.

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

How I can configure this?

It needs, bottle server running, is there any way to test bottle app without running server?


回答1:


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

Why don't you print the value of app.get('/hello').status_code so you can learn what's happening?

I'm also pretty sure that you should be checking status_int, not status_code.

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


来源:https://stackoverflow.com/questions/37885638/test-bottle-app-without-running-bottle-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!