簡體   English   中英

使用unittest POST Error 500測試Flask Web應用程序

[英]Testing Flask web app with unittest POST Error 500

這是我的test.py文件:

import unittest, views, json

class FlaskTestCase(unittest.TestCase):

def setUp(self):
    self.app = views.app.test_client()

def test_index(self):
    rv = self.app.get('/')
    assert 'Hamptons Bank' in rv.data

def test_credit(self):
    response = self.app.post('/credit', data=json.dumps({
            'amount': '20',
            'account': '1'
        }), content_type='application/json')

    print response.data
    assert 'Deposit of 20 to account 1' in response.data


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

test_index方法工作正常,但self.app.post保持返回(在print response.data中):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>

我的views.py中的方法如下:

@app.route('/credit', methods=['POST'])
def credit_account():
  bank = Bank()

  amount = int(request.json["amount"])
  depositCommand = DepositCommand(find_account(request.json["account"]), amount)
  bank.execute(depositCommand)

  message = "Deposit of " + str(request.json["amount"]) + " to account "+str(request.json["account"])
  return message

我究竟做錯了什么?

這是我第一次測試Flask網絡應用程序,所以我仍然有點困惑!

謝謝 :-)

(從上面的評論中):測試時,你應該設置app.config['DEBUG'] = Trueapp.config['TESTING'] = True

暫無
暫無

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

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