title: Flask-WTF / WTForms with Unittest fails validation, but works without Unittest
body: <p>When I run app normally and do login in browser, it works. But with Unittest it won't log me in .... , it returns login page again. <br>
  Both &quot;print rv.data&quot; just prints content of login page, but it should print content of index page, which is login_required
  <br>
  if it helps, I'm using SQLAlchemy as ORM.
  Anyone knows what can be problem? If any more code is needed, I will provide.
  I ve tried to search for similar question here , but didnt find.</p>
  <pre><code>#!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()
  </code></pre>
  <p>I added view login function for you to see...</p>
  <pre class="lang-py prettyprint-override"><code>@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(&quot;Logged in successfully.&quot;)
              return redirect(request.args.get('next') or url_for('index'))
  
          flash('Invalid password')
  
      return render_template('login.html', title='Sign In', form=form)
  </code></pre>
  <p>here is backtrace from line above form.validate_on_submit
  <code></p>
  <pre><code>&gt; /home/gaucan/webdev/projekt2/app/views.py(131)login()
  -&gt; if form.validate_on_submit():
  (Pdb) bt
    /home/gaucan/webdev/projekt2/shit.py(41)&lt;module&gt;()
  -&gt; unittest.main()
    /usr/lib64/python2.7/unittest/main.py(95)__init__()
  -&gt; self.runTests()
    /usr/lib64/python2.7/unittest/main.py(232)runTests()
  -&gt; self.result = testRunner.run(self.test)
    /usr/lib64/python2.7/unittest/runner.py(151)run()
  -&gt; test(result)
    /usr/lib64/python2.7/unittest/suite.py(70)__call__()
  -&gt; return self.run(*args, **kwds)
    /usr/lib64/python2.7/unittest/suite.py(108)run()
  -&gt; test(result)
    /usr/lib64/python2.7/unittest/suite.py(70)__call__()
  -&gt; return self.run(*args, **kwds)
    /usr/lib64/python2.7/unittest/suite.py(108)run()
  -&gt; test(result)
    /usr/lib64/python2.7/unittest/case.py(433)__call__()
  -&gt; return self.run(*args, **kwds)
    /usr/lib64/python2.7/unittest/case.py(369)run()
  -&gt; testMethod()
    /home/gaucan/webdev/projekt2/shit.py(35)test_users()
  -&gt; rv = self.login('gaucan','gau')
    /home/gaucan/webdev/projekt2/shit.py(28)login()
  -&gt; ), follow_redirects=True)
    /home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/werkzeug/test.py(771)post()
  -&gt; return self.open(*args, **kw)
    /home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/flask/testing.py(108)open()
  -&gt; follow_redirects=follow_redirects)
    /home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/werkzeug/test.py(735)open()
  -&gt; 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()
  -&gt; 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()
  -&gt; app_iter = app(environ, start_response)
    /home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/flask/app.py(1836)__call__()
  -&gt; return self.wsgi_app(environ, start_response)
    /home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/flask/app.py(1817)wsgi_app()
  -&gt; response = self.full_dispatch_request()
    /home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/flask/app.py(1475)full_dispatch_request()
  -&gt; rv = self.dispatch_request()
    /home/gaucan/webdev/projekt2/flask/lib/python2.7/site-packages/flask/app.py(1461)dispatch_request()
  -&gt; return self.view_functions[rule.endpoint](**req.view_args)
  &gt; /home/gaucan/webdev/projekt2/app/views.py(131)login()
  -&gt; if form.validate_on_submit():
  (Pdb) 
  </code></pre>
  </code>