Flask应用程序的单元测试期间无法创建测试客户端 [英] Can't create test client during unit test of Flask app

查看:27
本文介绍了Flask应用程序的单元测试期间无法创建测试客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何对从 session ['user_id'] 获取变量值的函数进行测试.这是具体的测试方法:

I am trying to find out how to run a test on a function which grabs a variable value from session['user_id']. This is the specific test method:

def test_myProfile_page(self):
    with app.test_client() as c:
        with c.session_transaction() as sess:
            sess['user_id'] = '1'

    rv = c.get('/myProfile')
    assert 'My Profile' in rv.data

这是正在测试的视图:

@app.route('/myProfile')
def myProfile():
    if not session.get('logged_in'):
        return render_template('login.html')
    else:
        profileID = session['user_id']
        userList = users.query.filter_by(id=profileID).all()
        flash('My Profile')
        return render_template('myProfile.html', userList=userList)

这是整个测试文件:

import os
import app
import unittest
import tempfile

class AppTestCase(unittest.TestCase):
    def setUp(self):
        self.db_fd, app.app.config['DATABASE'] = tempfile.mkstemp()
        app.app.config['TESTING'] = True
        self.app = app.app.test_client()

    def tearDown(self):
        os.close(self.db_fd)
        os.unlink(app.app.config['DATABASE'])

    def test_profile_page(self):
        rv = self.app.get('/profile1')
        assert 'Profile' in rv.data

    def login(self, username, password):
        return self.app.post('/login', data=dict(
            username=username,
            password=password
        ), follow_redirects=True)

    def logout(self):
        return self.app.get('/logout', follow_redirects=True)

    def test_login_logout(self):
        rv = self.login('Alex', 'passwordAlex')
        assert 'Welcome' in rv.data
        rv = self.logout()
        assert 'You have been logged out' in rv.data
        rv = self.login('Alex', 'noPassword')
        assert 'You have to Login' in rv.data
        rv = self.login('WrongName', 'passwordAlex')
        assert 'You have to Login' in rv.data

    def test_myProfile_page(self):
        with app.test_client() as c:
            with c.session_transaction() as sess:
                sess['user_id'] = '1'

        rv = c.get('/myProfile')
        assert 'My Profile' in rv.data

if __name__ == '__main__':
    unittest.main()

运行测试时显示以下错误:

The following error is shown when running the test:

ERROR: test_myProfile_page (__main__.AppTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "app_tests.py", line 46, in test_myProfile_page
    with app.test_client() as c:
AttributeError: 'module' object has no attribute 'test_client'

----------------------------------------------------------------------
Ran 3 tests in 0.165s


FAILED (errors=1)

为什么会出现此错误以及如何解决?

Why am I getting this error and how do I fix it?

推荐答案

您已经在 setUp 方法: self.app 期间创建了测试客户端. app 是您在顶部导入的模块,您需要在 app.app 上引用该应用程序对象才能访问Flask应用程序.而且,由于您已经创建了测试客户端,因此可以将测试更改为:

You've already created a test client during the setUp method: self.app. app is the module you imported at the top, you need to reference the app object at app.app to get to the Flask app. And since you've already created a test client, you can change the test to be:

def test_myProfile_page(self):
    with self.app as c:
        with self.app.session_transaction() as sess:
            sess['user_id'] = 1
            sess['logged_in'] = True
            rv = self.app.get('myProfile')
    assert 'My Profile' in rv.data

这篇关于Flask应用程序的单元测试期间无法创建测试客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆