2016-06-17 28 views
1

我正在關注框架的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) 

功能測試用例與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) 

QuickStartTestApp提。

如果您的WSGI應用程序需要任何配置,您必須在測試中手動設置該 。

我該如何配置?

它需要,瓶服務器運行,有沒有任何方式來測試瓶應用程序,而無需運行服務器?

+0

是什麼讓你覺得「它需要瓶服務器運行?」它不應該。 –

+0

@ ron.rothman,當我運行這個UT時,它會給出錯誤,無法連接'http:// localhost/login' :( – Nilesh

+0

請添加回溯,以便我們可以看到發生了什麼。 'mywebapp'。 –

回答

1

感謝您發佈堆棧跟蹤。這清楚地表明,這是導致該行500:

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

你爲什麼不打印的app.get('/hello').status_code值,以便您可以瞭解發生了什麼?

我也很肯定你應該檢查status_int而不是status_code

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

感謝您的幫助,我更改了代碼'return「Hello」+ request.get_header('name','')'及其作品。:) – Nilesh

-2

你的問題沒有意義。如何測試一個Web應用程序,而無需運行一個Web引擎來測試它?你可以測試任何操縱瓶外數據但是生成200狀態的函數,或者接收一個post命令本質上需要一個web引擎來運行?

這就像測試郵局沒有在郵箱中寫信。

+0

http://docs.pylonsproject.org/projects/webtest/en/latest/#what-this-does –