簡體   English   中英

我認為Flask希望我在我的tests.py文件中實例化該應用程序,但我不知道如何

[英]I think Flask wants me to instantiate the app in my tests.py file but i dont know how to

我認為Flask希望我實例化該應用程序,但是我不知道該怎么AttributeError: 'NoneType' object has no attribute 'app' ,但出現錯誤AttributeError: 'NoneType' object has no attribute 'app'

追溯

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文件:

from app import app

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

初始化 .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

您從未導入過您的app因此無法對其進行測試。 查看有關如何導入應用程序以及如何對其進行測試的文檔

這是基本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

雖然我不確定。 我猜您忘記了將應用程序模塊導入run.py。 為了更加確定,也請發布run.py和init .py的內容。

在您的run.py中,您必須import app並運行函數app.run()都是必需的,並且應該是在您的應用程序中要做的python run.py ,之后只需執行python run.py ,服務器就會從端口啟動-我相信5000。

編輯:如果您看到from app import views導入的init.py,這是錯誤的,因為從init.py上下文中您已經在應用程序中,無需再次從應用程序鍵入內容,因為導入將嘗試查找文件夾或當前層次結構中名為app的軟件包。 更改為僅import views ,您應該就這樣了。 同樣,正如Kemis指出的那樣,將您的應用程序導入單元測試文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM