class LoginTest(unittest.TestCase):
def setUp(self):
app.testing = True
self.client = app.test_client()
def tearDown(self):
pass
def test_empty_username_password(self):
response = app.test_client().post('/login', data={})
json_data = response.data
json_dict = json.loads(json_data)
self.assertIn('errcode', json_dict, '数据格式返回错误')
self.assertEqual(json_dict['errcode'], -2, '状态码返回错误')
观察代码,response = app.test_client().post('/login', data={"username": "aaaaa", "password": "12343”})中,app.test_client()返回的是一个类当前客户端的test_client_class类对象,如无则返回flask.testing基类
其中,当前的test_client的response_class会作为参数传入,response_class就等于Response类对象(response_class = Response)。具体可参见源码定义: