Browse Source

Improved test coverage for the test client

pull/365/head
Armin Ronacher 13 years ago
parent
commit
9c8f138a42
  1. 27
      flask/testsuite/testing.py

27
flask/testsuite/testing.py

@ -53,24 +53,32 @@ class TestToolsTestCase(FlaskTestCase):
@app.route('/', methods=['GET', 'POST']) @app.route('/', methods=['GET', 'POST'])
def index(): def index():
if flask.request.method == 'POST': if flask.request.method == 'POST':
return flask.redirect('/redirect') return flask.redirect('/getsession')
flask.session['data'] = 'foo' flask.session['data'] = 'foo'
return 'index' return 'index'
@app.route('/redirect') @app.route('/getsession')
def redirect(): def get_session():
return 'redirect' return flask.session.get('data', '<missing>')
with app.test_client() as c: with app.test_client() as c:
ctx = app.test_request_context() rv = c.get('/getsession')
ctx.push() assert rv.data == '<missing>'
rv = c.get('/') rv = c.get('/')
assert rv.data == 'index' assert rv.data == 'index'
assert flask.session.get('data') == 'foo' assert flask.session.get('data') == 'foo'
rv = c.post('/', data={}, follow_redirects=True) rv = c.post('/', data={}, follow_redirects=True)
assert rv.data == 'redirect' assert rv.data == 'foo'
assert flask.session.get('data') == 'foo'
ctx.pop() # XXX: Currently the test client does not support
# keeping the context around if a redirect is followed.
# This would be nice to fix but right now the Werkzeug
# test client does not support that.
##assert flask.session.get('data') == 'foo'
rv = c.get('/getsession')
assert rv.data == 'foo'
def test_session_transactions(self): def test_session_transactions(self):
app = flask.Flask(__name__) app = flask.Flask(__name__)
@ -113,6 +121,7 @@ class TestToolsTestCase(FlaskTestCase):
with app.test_client() as c: with app.test_client() as c:
rv = c.get('/') rv = c.get('/')
req = flask.request._get_current_object() req = flask.request._get_current_object()
self.assert_(req is not None)
with c.session_transaction(): with c.session_transaction():
self.assert_(req is flask.request._get_current_object()) self.assert_(req is flask.request._get_current_object())

Loading…
Cancel
Save