Testing Flask web application with unittest POST Error 500

This is my test.py file:

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

The test_index method works fine, but self.app.post continues to return (in the print.data file):

<!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>

The method in my view.py is as follows:

@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

What am I doing wrong?

This is my first test for the Flask web application, so I'm still a bit confused!

Thank: -)

+4
source share
1 answer

(From my comment above): when testing you should install app.config['DEBUG'] = Trueand app.config['TESTING'] = True.

+5
source

All Articles