當我正常運行應用程序並在瀏覽器中登錄時,它可以工作。但是Unittest不會將我登錄到....,它會再次返回登錄頁面。
這兩個「打印rv.data」只是打印登錄頁面的內容,但它應該打印索引頁面的內容,這是login_required
如果有幫助,我使用SQLAlchemy作爲ORM。 任何人都知道什麼是問題?如果需要更多的代碼,我會提供。 我試圖在這裏搜索類似的問題,但沒有找到。單元測試的Flask-WTF/WTForms驗證失敗,但沒有單元測試工作
#!flask/bin/python
import os
import unittest
from config import basedir
from app import app, db
from app.models import User,Exam,Attempt,Subject
class TestCase(unittest.TestCase):
def setUp(self):
app.config['TESTING'] = True
app.config['CSRF_ENABLED'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'test.db')
self.app = app.test_client()
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
def login(self, username, password):
return self.app.post('/login', data=dict(
username=username,
password=password
), follow_redirects=True)
def test_users(self):
u = User(username = 'gaucan', password = 'gau')
db.session.add(u)
db.session.commit()
rv = self.login('gaucan','gau')
print rv.data
rv = self.app.get('/',follow_redirects=True)
print rv.data
if __name__ == '__main__':
unittest.main()
我添加的視圖登錄功能給你看......
@app.route('/login', methods = ['GET', 'POST'])
def login():
if g.user is not None and g.user.is_authenticated():
return redirect(url_for('index'))
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username = form.username.data).first()
if user is not None and form.password.data == user.password:
login_user(user)
flash("Logged in successfully.")
return redirect(request.args.get('next') or url_for('index'))
flash('Invalid password')
return render_template('login.html',
title = 'Sign In',
form = form)
這裏是從線回溯上述form.validate_on_submit
> /home/gaucan/webdev/projekt2/app/views.py(131)login()
-> if form.validate_on_submit():
(Pdb) bt
/home/gaucan/webdev/projekt2/shit.py(41)<module>()
-> unittest.main()
/usr/lib64/python2.7/unittest/main.py(95)__init__()
-> self.runTests()
/usr/lib64/python2.7/unittest/main.py(232)runTests()
-> self.result = testRunner.run(self.test)
/usr/lib64/python2.7/unittest/runner.py(151)run()
-> test(result)
/usr/lib64/python2.7/unittest/suite.py(70)__call__()
-> return self.run(*args, **kwds)
/usr/lib64/python2.7/unittest/suite.py(108)run()
-> test(result)
/usr/lib64/python2.7/unittest/suite.py(70)__call__()
-> return self.run(*args, **kwds)
/usr/lib64/python2.7/unittest/suite.py(108)run()
-> test(result)
/usr/lib64/python2.7/unittest/case.py(433)__call__()
-> return self.run(*args, **kwds)
/usr/lib64/python2.7/unittest/case.py(369)run()
-> testMethod()
/home/gaucan/webdev/projekt2/shit.py(35)test_users()
-> rv = self.login('gaucan','gau')
/home/gaucan/webdev/projekt2/shit.py(28)login()
->), follow_redirects=True)
/home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/werkzeug/test.py(771)post()
-> return self.open(*args, **kw)
/home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/flask/testing.py(108)open()
-> follow_redirects=follow_redirects)
/home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/werkzeug/test.py(735)open()
-> response = self.run_wsgi_app(environ, buffered=buffered)
/home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/werkzeug/test.py(658)run_wsgi_app()
-> rv = run_wsgi_app(self.application, environ, buffered=buffered)
/home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/werkzeug/test.py(854)run_wsgi_app()
-> app_iter = app(environ, start_response)
/home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/flask/app.py(1836)__call__()
-> return self.wsgi_app(environ, start_response)
/home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/flask/app.py(1817)wsgi_app()
-> response = self.full_dispatch_request()
/home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/flask/app.py(1475)full_dispatch_request()
-> rv = self.dispatch_request()
/home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/flask/app.py(1461)dispatch_request()
-> return self.view_functions[rule.endpoint](**req.view_args)
> /home/gaucan/webdev/projekt2/app/views.py(131)login()
-> if form.validate_on_submit():
(Pdb)
用戶是否已經存在於數據庫中? –
@肖恩:是的。它的奇怪,它運行時正常運行,但不是在unittests ...我現在添加視圖登錄功能,如果它幫助.. – user3263155
我想了很多「打印信息」:D問題是那form.validate_on_submit()返回FALSE ....但我不知道爲什麼......也許用Flask-login,單元測試必須以不同的方式完成? – user3263155