2016-06-17 21 views
1

Recipesフレームワークをフォローしています。ボトルサーバーを稼働していないテストボトルアプリ

私はエラーの下になったnosetests test_mywebapp.pyを実行すると、私はコード

#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!' 

と機能テストケースの下にしてみてください。 TestApp言及上

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アプリケーションに設定が必要な場合は、テストで手動で を設定する必要があります。

どうすれば設定できますか?

これは、ボトルサーバーを実行する必要があります、サーバーを実行せずにボトルのアプリをテストする方法はありますか?

+0

「ボトルサーバーを実行する必要がありますか?それはしないでください。 –

+0

@ ron.rothman、このUTを実行すると、エラーが発生し、 'http:// localhost/login'に接続できません:( – Nilesh

+0

何が起きているかを見るためにトレースバックを追加してください。 –

答えて

1

スタックトレースを送信していただきありがとうございます。これは明らかに、これが原因となっている行であることを示している500:

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

なぜあなたはapp.get('/hello').status_codeの値は、あなたが何が起こっているかを学ぶことができます印刷されませんか?

私はまた、status_codeではなく、status_intを確認する必要があります。

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

助けてくれてありがとう、私はコードを変更します'return" Hello "+ request.get_header( 'name'、 '')'とその動作:) – Nilesh

-2

あなたの質問は理にかなっていません。どのようにWebアプリケーションをテストして、Webエンジンをテストしてテストすることができますか?ボトルの外でデータを操作する機能はテストできますが、200ステータスを生成するか、ポストコマンドを受け取るには本質的にWebエンジンを実行する必要がありますか?

メールボックスに手紙を入れないで郵便局をテストするようなものです。

+0

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

関連する問題