使用 unittest POST 错误 500 测试 Flask 网络应用程序
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 不断返回(打印 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 web 应用程序,所以我还是有点困惑!
谢谢:-)
(来自我上面的评论):测试时,您应该设置 app.config['DEBUG'] = True
和 app.config['TESTING'] = True
.
这是我的 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 不断返回(打印 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 web 应用程序,所以我还是有点困惑!
谢谢:-)
(来自我上面的评论):测试时,您应该设置 app.config['DEBUG'] = True
和 app.config['TESTING'] = True
.