正在获取AttributeError:“ TestRoutes”对象没有属性“ app”

时间:2019-06-30 12:38:37

标签: python flask python-unittest

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

  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")

1 个答案:

答案 0 :(得分:1)

您需要根据documentation

将设置重命名为setUp。