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

后端 未结 5 1140
广开言路
广开言路 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:12

    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
            )
    

提交回复
热议问题