How can I fake request.POST and GET params for unit testing in Flask?

后端 未结 5 1340
广开言路
广开言路 2020-12-30 18:33

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

5条回答
  •  醉酒成梦
    2020-12-30 19:22

    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)
    

提交回复
热议问题