这是我的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网络应用,所以我仍然有点困惑!
谢谢: - )
答案 0 :(得分:6)
(来自我上面的评论):测试时,您应该设置app.config['DEBUG'] = True
和app.config['TESTING'] = True
。