Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't create test client during unit test of Flask app

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

This is the view being tested:

@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)

This is the entire test file:

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?

like image 220
Al Ex Tsm Avatar asked Jan 19 '15 15:01

Al Ex Tsm


People also ask

How do you write a unit test case for Flask application?

First create a file named test_app.py and make sure there isn't an __init__.py in your directory. Open your terminal and cd to your directory then run python3 app.py . If you are using windows then run python app.py instead. Hope this will help you solve your problem.

Which of the following methods is used to create a test client of a Flask application?

Flask provides a way to test your application by exposing the Werkzeug test Client and handling the context locals for you. You can then use that with your favourite testing solution.

What is test client Flask?

Flask provides a test client that simulates requests to the application and returns the response data. You should test as much of your code as possible. Code in functions only runs when the function is called, and code in branches, such as if blocks, only runs when the condition is met.


1 Answers

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
like image 113
davidism Avatar answered Nov 09 '22 21:11

davidism