How to send post data to flask using pytest-flask How to send post data to flask using pytest-flask flask flask

How to send post data to flask using pytest-flask


pytest-flask provide several fixtures, including a client one. With it you can make a test function similar to this:

def test_upload(client):    mimetype = 'application/json'    headers = {        'Content-Type': mimetype,        'Accept': mimetype    }    data = {        'Data': [20.0, 30.0, 401.0, 50.0],        'Date': ['2017-08-11', '2017-08-12', '2017-08-13', '2017-08-14'],        'Day': 4    }    url = '/upload/'    response = client.post(url, data=json.dumps(data), headers=headers)    assert response.content_type == mimetype    assert response.json['Result'] == 39


While the accepted answer works, it can be simplified by passing the kwarg json into the post method instead of data

def test_upload(client):    data = {        'Data': [20.0, 30.0, 401.0, 50.0],        'Date': ['2017-08-11', '2017-08-12', '2017-08-13', '2017-08-14'],        'Day': 4    }    url = '/upload/'    response = client.post(url, json=data)    assert response.content_type == 'application/json'    assert response.json['Result'] == 39