2017-07-13 10 views
2

知らないが、私は、エラーにAttributeError: 'NoneType' object has no attribute 'app'私はフラスコは私が私のtests.pyファイル内のアプリをインスタンス化したいと思いますが、私は、私はフラスコは私がアプリをインスタンス化したいと思いますどのように

を取得していますする方法を知りませんトレースバック

C:\Users\Mlamba\Envs\vir\Scripts\python.exe D:/code/web-projects/Bucketlist-Python-Flask-project/tests.py 
E 
====================================================================== 
ERROR: test_index_view (__main__.ViewTests) 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "D:/code/web-projects/Bucketlist-Python-Flask-project/tests.py", line 11, in test_index_view 
    response = make_response(render_template("index.html")) 
    File "C:\Users\Mlamba\Envs\vir\lib\site-packages\flask\templating.py", line 132, in render_template 
    ctx.app.update_template_context(context) 
AttributeError: 'NoneType' object has no attribute 'app' 

---------------------------------------------------------------------- 
Ran 1 test in 0.001s 

FAILED (errors=1) 

Run.pyファイル:

​​

のinitの.pyファイル:

from flask import Flask 

# Load the views 
from app import views 

# Initialize the app 
app = Flask(__name__, instance_relative_config=True) 

# Load the config file 
app.config.from_object('config') 

テストファイル

import unittest 

from flask import render_template, make_response 


class ViewTests(unittest.TestCase): 
    def test_index_view(self): 
     """ 
     Test that index page is accessible without login 
     """ 
     response = make_response(render_template("index.html")) 
     self.assertEquals(response.status_code, 200) 


if __name__ == '__main__': 
    unittest.main() 

ディレクトリ構造:

|-- README.md 
|-- __pycache__ 
| `-- config.cpython-36.pyc 
|-- app 
| |-- __init__.py 
| |-- __pycache__ 
| | |-- __init__.cpython-36.pyc 
| | `-- views.cpython-36.pyc 
| |-- models.py 
| |-- static 
| |-- templates 
| | |-- index.html 
| | `-- layout.html 
| `-- views.py 
|-- config.py 
|-- requirements.txt 
|-- run.py 
`-- tests.py 
+0

あなたのテストでは、docstringが何を言っているか、つまりログインせずにインデックスページにアクセスできるかどうかをチェックすることは全くありません。 –

+0

しかし、実際の問題については、完全なトレースバックを表示してください。 –

+0

質問にトレースバックを追加しました –

答えて

0

私は確かに知りませんが。あなたのアプリケーションモジュールをrun.pyにインポートするのを忘れていたと思います。あなたのrun.pyとinit .pyの内容をもっと確実に投稿してください。

あなたは import appに持っているとも app.run()の両方が必要であり、ちょうど python run.pyを行うと、サーバはポート番号5000 Iから開始されますその後、あなたのアプリに行うための最初のステップであるべき機能を実行し、あなたのrun.pyで

信じる。

EDIT: あなたinit.pyが表示された場合は、from app import viewsをインポートしたアプリで、すでにしているinit.pyの文脈と再びアプリから入力する必要はありませんからインポートが見つけようとしますのでので、これは間違っています現在の階層内のappという名前のフォルダまたはパッケージ。 をちょうどimport viewsに変更してください。 Kemis氏が指摘したように、あなたのアプリをunittestファイルにインポートしてください。

+0

の✓をつけて "受諾" –

3

appをインポートしたことがないので、テストできません。 アプリのインポート方法とテスト方法については、documentationをご覧ください。ここで

は基本flaskテストの例です:

main.py:

from flask import Flask 

app = Flask(__name__) 

@app.route('/') 
def hello_world(): 
    return "Hello world!" 

if __name__ = '__main__': 
    app.run() 

test_app.py:

import unittest 
from main import app 

class FlaskTestCase(unittest.TestCase): 
    def setUp(self): 
     self.app = app.test_client() 
     self.app.testing = True 
     pass 

    def test_num1(self): 
     rv = self.app.get('/') 
     assert b'Hello world' in rv.data 


if __name__ == '__main__': 
    unittest.main() 

あなたがしてテストを実行します。 python test_app.py

関連する問題