【问题标题】:Getting AttributeError: 'TestRoutes' object has no attribute 'app'获取 AttributeError:“TestRoutes”对象没有属性“app”
【发布时间】:2019-06-30 12:38:37
【问题描述】:

下面的代码是我为烧瓶应用编写的单元测试,但在运行测试时导致以下错误:

  File "/Users/ausername/projects/term_trader/tt2/tests/testRoutes.py", line 47, in test_deposit_route
    response = self.app.post(BASE_URL,
AttributeError: 'TestRoutes' object has no attribute 'app'

对我来说,导入看起来不错,因为我在“测试”文件夹中运行了一些其他测试,没有问题。当我在 curl 中测试它们时,它们本身工作得很好,我只是想养成编写测试的习惯。只是似乎无法弄清楚问题是什么。这是烧瓶版本 1.0.3。任何建议表示赞赏。

代码:

from unittest import TestCase
from model.user import User
from model.position import Position
from model.trade import Trade
from flask_app.app import app
from schema import build_user, build_positions, build_trades
import json
import os


BASE_URL = 'http://localhost:5000/api/'


class TestRoutes(TestCase):

    def setup(self):
        self.app.config['TESTING'] = True
        self.app.config['DEBUG'] = False
        self.app = app.test_client()

        build_user()
        build_positions()
        build_trades()

        bob = User(**{
        "user_name": "bobdean",
        "password": "password",
        "real_name": "Bob Dean",
        "balance": 0.0
        })
        bob.hash_password("password")
        bob.api_key = "11111111111111111111"
        bob.save()

    def tearDown(self):
        pass

    def test_deposit_route(self):
        bob = User.from_pk(1)
        self.assertEqual(mike.user_name, "bobdean")
        deposit = {"amount":1500.0}
        response = self.app.post(BASE_URL,
                                 data=json.dumps(deposit),
                                 content_type='application/json')
        self.assertEqual(response.status_code, 201, "Status Code should be 201")
        self.assertEqual(bob.balance, 1500.0, "Bob's balance should equal 1500")

【问题讨论】:

  • 那么,您认为self.app 是在哪里定义的?请注意,您的设置方法没有被调用;它应该被称为setUp 以自动运行;但是当你修复它时,你会在那个方法中得到同样的错误。
  • @DanielRoseman,正确,我确实需要清理其他一些奇怪的东西,但 setUp 为我指明了正确的方向,谢谢

标签: python flask python-unittest


【解决方案1】:

你需要根据documentation将setup重命名为setUp

【讨论】:

  • 是的,我知道这是小事。
  • 我花了两个小时才意识到这一点。这对我有用
猜你喜欢
  • 2013-06-16
  • 1970-01-01
  • 2019-06-03
  • 2015-02-17
  • 2020-12-18
  • 2013-11-06
  • 2021-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多