【问题标题】:I think Flask wants me to instantiate the app in my tests.py file but i dont know how to我认为 Flask 希望我在我的 tests.py 文件中实例化应用程序,但我不知道如何
【发布时间】:2017-12-18 01:47:22
【问题描述】:

我认为 Flask 想让我实例化应用程序,但我不知道如何,收到错误 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()

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

【问题讨论】:

  • 你的测试没有做任何与文档字符串相关的事情,即检查索引页面是否可以在没有登录的情况下访问。
  • 但是,对于您的实际问题,请显示完整的回溯。
  • 添加了对问题的追溯
  • @A.Mlamba 我已经编辑了我的答案,所以你有一个例子。希望这有帮助:) p.s.我看到您是新人,所以我会告诉您:如果某个答案对您有帮助 upvote it 和最佳答案 accept it,请单击答案旁边的 ✓

标签: python unit-testing flask tdd


【解决方案1】:

您从未导入过您的app,因此您无法对其进行测试。 查看documentation 了解如何导入应用以及如何对其进行测试。

这是一个基本的flask 测试示例:

ma​​in.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

【讨论】:

  • 我和 OP 有同样的问题,但在我的主程序中,我有一个函数 def create_app 返回烧瓶对象,所以在这种情况下,如何将应用程序导入测试文件?
【解决方案2】:

虽然我不确定。我猜你忘了将你的应用程序模块导入run.py。还请发布您的 run.py 和 init.py 的内容,以便更加确定。

在您的 run.py 中,您必须 import app 并运行函数 app.run() 两者都是必需的,并且应该是在您的应用程序中执行的第一步之后,只需执行 python run.py 并且服务器将从我相信端口号 5000。

编辑: 如果你看到你的 init.py 你已经导入了from app import views 这是错误的,因为从 init.py 的上下文中你已经在 app 中并且不需要再次从 app 输入,因为导入会尝试找到一个文件夹或一个包当前层次结构中的命名应用程序。 将其更改为import views,您应该正在路上。同样正如 Kemis 指出的,将您的应用程序导入您的单元测试文件中。

【讨论】:

  • 将它们添加到问题中
猜你喜欢
  • 2015-10-24
  • 2020-03-25
  • 2017-08-13
  • 1970-01-01
  • 2021-03-05
  • 2022-06-22
  • 1970-01-01
  • 2015-03-20
  • 1970-01-01
相关资源
最近更新 更多