在不运行瓶子服务器的情况下测试瓶子应用程序[英] Test bottle app without running bottle server

本文是小编为大家收集整理的关于在不运行瓶子服务器的情况下测试瓶子应用程序的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在关注食谱 ="/问题/标记/瓶子" class=" post-tag" title ="显示标签'瓶'" rel =" tag">瓶子框架框架.

当我尝试以下代码

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

如果您的WSGI应用程序需要任何配置,则必须设置 在您的测试中手动进行.

我如何配置这个?

它需要,运行瓶服务器,是否有任何方法可以在不运行服务器的情况下测试瓶装应用程序?

推荐答案

感谢您发布堆栈跟踪.它清楚地表明这是导致500的行:

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

为什么不打印app.get('/hello').status_code的值,以便您可以学习发生的事情?

我也很确定您应该检查status_int,而不是status_code.

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

本文地址:https://www.itbaoku.cn/post/1975359.html

问题描述

I am following Recipes of 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?

推荐答案

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