64

I would like to fake request parameters for unit testing. How can I achieve this in Flask?

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
nubela
  • 1
  • 24
  • 75
  • 123

6 Answers6

101

Did you read Flask docs about testing?

You can use following:

self.app.post('/path-to-request', data=dict(var1='data1', var2='data2', ...))
self.app.get('/path-to-request', query_string=dict(arg1='data1', arg2='data2', ...))

Current development version of Flask also includes support for testing JSON APIs:

from flask import request, jsonify

@app.route('/jsonapi')
def auth():
    json_data = request.get_json()
    attribute = json_data['attr']
    return jsonify(resp=generate_response(attribute))

with app.test_client() as c:
    rv = c.post('/jsonapi', json={
        'attr': 'value', 'other': 'data'
    })
    json_data = rv.get_json()
    assert generate_response(email, json_data['resp'])
Ri1a
  • 737
  • 9
  • 26
plaes
  • 31,788
  • 11
  • 91
  • 89
44

POST:

self.app.post('/endpoint', data=params)

GET:

self.app.get('/endpoint', query_string=params)
Itay Kahana
  • 2,355
  • 1
  • 26
  • 20
6

If you prefer to use test_request_context:

import unittest
from myapp import extract_query_params

testapp = flask.Flask(__name__)

class TestFoo(unittest.TestCase):
    def test_happy(self):
        with testapp.test_request_context('?limit=1&offset=2'):
            limit, offset = extract_query_params(['limit', 'offset'])
            self.assertEquals(limit, 1)
            self.assertEquals(offset, 2)
callmebob
  • 6,128
  • 5
  • 29
  • 46
Matthew Moisen
  • 16,701
  • 27
  • 128
  • 231
  • 2
    `with testapp.test_request_context(headers=h):` can be used to pass **headers** without mentioning any **url**, this will mock a request with _default-url_ – Abhijeet Jun 02 '17 at 10:28
3

I needed an simple request for unit testing, and this was an easy solution:

from flask import Request
r = Request({})
new name
  • 15,861
  • 19
  • 68
  • 114
1

I still had issue with this when testing post form data for logging in this worked for me.

def login(self, username, password):
    return self.app.post('/', data='Client_id=' + username +'&Password=' + password,
                         follow_redirects=True,content_type='application/x-www-form-urlencoded')

I found this is out this way.

Chrome: Developer mode --> Doc --> HTML doc that was requested --> Headers Tab --> Form data --> view source

jmcgrath207
  • 1,317
  • 2
  • 19
  • 31
1

here is a complete code example of a unit test

testapp = app.test_client()

class Test_test(unittest.TestCase):
    def test_user_registration_bad_password_short(self):
        response = self.register(name='pat',
                                 email='me@mail.com', 
                                 password='Flask', 
                                 password2='Flask')
        self.assertEqual(response.status_code, 200)
        self.assertIn(b'password should be 8 or more characters long', 
                      response.data)

    def register(self, name, email, password, password2):
        return testapp.post(
            '/register',
            data=dict(username=name, 
                      email=email, 
                      password=password, 
                      password2=password2),
            follow_redirects=True
        )
Barka
  • 8,764
  • 15
  • 64
  • 91