如何使用Flask-Login在Flask中模拟和测试多用户登录/注销?

时间:2018-12-13 02:52:03

标签: python testing flask flask-login

测试多个用户的身份验证过程需要多个会话/客户端。我尝试使用以下代码测试app.test_client(...),它可以测试单用户的登录/注销功能,但是当多个用户在自己的会话中登录/注销时,我无法确定它是否仍然有效。

setUp, tearDown, and other branche tests...

def test_login(self):
    """Check if server can handle login successfully.
    """
    with self.client as c:
        login_res = c.post(API.login, data=self.valid_user)
        assert_equals(login_res.get_json(), {
            'data': {'userName': 'user'}, 'status': 200,
            'msg': 'login success.'
        })

def test_logout(self):
    """Check if server can handle user logout.
    """
    with self.client as c:
        # login user first
        c.post(API.login, data=self.valid_user)
        # then log out.
        res = c.get(API.logout)
        assert_equals(res.get_json(), {
            'data': {'userName': 'user'}, 'status': 200,
            'msg': 'logout success.'
        })

0 个答案:

没有答案