Flask 单元测试问题

Flask Unit Test Proplem

我似乎无法正确测试我的 "saveEditAccount" 功能!

app.py:

@app.route('/editAccount')
def editAccount():
    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('Edit profile')
        return render_template('edit.html', userList=userList)



@app.route('/saveEditAccount', methods=['GET', 'POST'])
def saveEditAccount():

    if not session.get('logged_in'):
        return render_template('login.html')
    else:

        if request.method == 'POST':
            userCheck1 = request.form['userName']
            userCheck2 = request.form['userEmail']
            userCheck3 = request.form['userPhone']
            userCheck4 = request.form['userPassA']
            userCheck5 = request.form['userPassB']
            profileID = session['user_id']

            if userCheck1 == "":
                        flash('Username required!')
                        return redirect(url_for('editAccount'))
            newUser = users.query.filter_by(userName=userCheck1).first()
            if newUser:
                if newUser.id != profileID:
                    flash('Username Taken!')
                    return redirect(url_for('editAccount'))
                else:
                    if userCheck2 == "":
                        flash('Email required!')
                        return redirect(url_for('editAccount'))
                    if userCheck3 == "":
                        flash('Phone required!')
                        return redirect(url_for('editAccount'))
                    if userCheck4 == "":
                        flash('Password required!')
                        return redirect(url_for('editAccount'))
                    if userCheck5 == "":
                        flash('Confirm password!')
                        return redirect(url_for('editAccount'))
                    else:
                        if userCheck4 == userCheck5:

                            newUser = users.query.get(profileID)
                            newUser.userName=userCheck1
                            newUser.userEmail=userCheck2
                            newUser.userPhone=userCheck3
                            newUser.userPass=userCheck4
                            db.session.commit()
                            flash('Saved!')
                            return redirect(url_for('myProfile'))
                        else:
                            flash('Retype passwords!')
                        return redirect(url_for('editAccount'))
            else:
                if userCheck2 == "":
                    flash('Email required!')
                    return redirect(url_for('editAccount'))
                if userCheck3 == "":
                    flash('Phone required!')
                    return redirect(url_for('editAccount'))
                if userCheck4 == "":
                    flash('Password required!')
                    return redirect(url_for('editAccount'))
                if userCheck5 == "":
                    flash('Confirm password!')
                    return redirect(url_for('editAccount'))
                else:
                    if userCheck4 == userCheck5:

                        newUser = users.query.get(profileID)
                        newUser.userName=userCheck1
                        newUser.userEmail=userCheck2
                        newUser.userPhone=userCheck3
                        newUser.userPass=userCheck4
                        db.session.commit()
                        flash('Saved!')
                        return redirect(url_for('myProfile'))
                    else:
                        flash('Retype passwords!')
                        return redirect(url_for('editAccount'))
        else:
            return render_template('myProfile.html')

app_tests.py

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 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 'Friends' 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 saveEditAccount(self, unserName, userEmail,  userPhone, userPassA, userPassB):
        self.login('Carlos', 'passwordCarlos')
        return self.app.post('/saveEditAccount', data=dict(
            unserName=unserName,
            userEmail=userEmail,
            userPhone=userPhone,
            userPassA=userPassA,
            userPassB=userPassB
        ), follow_redirects=True)

    def test_saveEditAccount(self):

        #successfully changed with same username
        rv = self.saveEditAccount('Carlos', 'carlitos@carlitos.com', '900102030', 'passwordCarlos', 'passwordCarlos')
        assert 'Saved!' in rv.data
         #successfully changed with different(not taken) username
        rv = self.saveEditAccount('Karlos', 'carlitos@carlitos.com', '900102030', 'passwordKarlos', 'passwordKarlos')
        assert 'Saved!' in rv.data
        #existing username/user
        rv = self.saveEditAccount('Laura', 'carlitos@carlitos.com', '900102030', 'passwordKarlos', 'passwordKarlos')
        assert 'Username Taken!' in rv.data
         #no username
        rv = self.saveEditAccount('', 'carlitos@carlitos.com', '900102030', 'passwordKarlos', 'passwordKarlos')
        assert 'Username required!' in rv.data
        #no email
        rv = self.saveEditAccount('Karlos', '', '900102030' 'passwordKarlos', 'passwordKarlos')
        assert 'Email required!' in rv.data
        #no password
        rv = self.saveEditAccount('Karlos', 'carlitos@carlitos.com', '900102030', '', 'passwordKarlos')
        assert 'Password required!' in rv.data
        # no password confirmation
        rv = self.saveEditAccount('Karlos', 'carlitos@carlitos.com', '900102030', 'passwordKarlos', '')
        assert 'Confirm password!' in rv.data
        #no password match
        rv = self.saveEditAccount('Karlos', 'carlitos@carlitos.com', 'passwordKarlos', 'passwordKarkos')
        assert 'Retype passwords!' in rv.data
        #no phone
        rv = self.saveEditAccount('Karlos', 'carlitos@carlitos.com', '', 'passwordKarlos', 'passwordKarlos')
        assert 'Phone required!' in rv.data

数据库数据预测试:

用户table:

友谊table:

我在测试 "saveEditAccount" 函数时遇到断言错误:

不确定我是否正确传递值(需要来自会话的用户 ID 和发布的变量)

我也试过用下面的方式编写测试,但给了我同样的错误:

def test_saveEditAccount(self):
    self.login('Carlos', 'passwordCarlos')
    rv = self.app.post('/saveEditAccount', data=dict(
        unserName='Carlos',
        userEmail='carlitos@carlitos.com',
        userPhone='900102030',
        userPassA='passwordCarlos',
        userPassB='passwordCarlos'
    ), follow_redirects=True)
    assert 'Saved!' in rv.data

我是单元测试的新手,非常需要启发。非常感谢您提供的任何帮助!

userName='Carlos' 而不是 userName='Carlos' .....