mardi 4 octobre 2016

Unit Testing JSON output in Flask app

Consider the following app segment created within Flask:

from flask import Flask, jsonify

@app.route('/user/listall')
def list_all_users():
    all_users = User.query.all()
    list_dict = []

    for user in all_users:
    user_dict = {
        "id": user.id,
        "username": user.username,
        "ip": user.ip,
        "time_created": user.timestamp
    }
    list_dict.append(user_dict)
    return jsonify(list_dict)

So a user has created an entry in an SQLite database and visiting

http://localhost:500/user/listall

should see some jsonified result that looks like this:

[
  {
    "id": "50048c54-8a1c-11e6-bfe7-90b11c839b66", 
    "ip": "127.0.0.1", 
    "time_created": "2016-10-04 11:21:28.757255", 
    "username": "list"
  }
]

My question is when unit testing this using unitest, how does one test that the output looks like the above?

Aucun commentaire:

Enregistrer un commentaire