More DRYing up the test suite (#2325)

This commit is contained in:
Christian Stade-Schuldt 2017-05-24 17:27:36 -07:00 committed by David Lord
parent 50c6df7098
commit 4ec1fbc9f5
10 changed files with 230 additions and 283 deletions

View file

@ -38,14 +38,12 @@ def test_environ_defaults(app, client, app_ctx, req_ctx):
ctx = app.test_request_context()
assert ctx.request.url == 'http://localhost/'
with app.test_client() as c:
rv = c.get('/')
with client:
rv = client.get('/')
assert rv.data == b'http://localhost/'
def test_environ_base_default(app, client, app_ctx):
app.testing = True
@app.route('/')
def index():
flask.g.user_agent = flask.request.headers["User-Agent"]
@ -89,39 +87,39 @@ def test_redirect_keep_session(app, client, app_ctx):
def get_session():
return flask.session.get('data', '<missing>')
with client as c:
rv = c.get('/getsession')
with client:
rv = client.get('/getsession')
assert rv.data == b'<missing>'
rv = c.get('/')
rv = client.get('/')
assert rv.data == b'index'
assert flask.session.get('data') == 'foo'
rv = c.post('/', data={}, follow_redirects=True)
rv = client.post('/', data={}, follow_redirects=True)
assert rv.data == b'foo'
# This support requires a new Werkzeug version
if not hasattr(c, 'redirect_client'):
if not hasattr(client, 'redirect_client'):
assert flask.session.get('data') == 'foo'
rv = c.get('/getsession')
rv = client.get('/getsession')
assert rv.data == b'foo'
def test_session_transactions(app):
def test_session_transactions(app, client):
app.secret_key = 'testing'
@app.route('/')
def index():
return text_type(flask.session['foo'])
with app.test_client() as c:
with c.session_transaction() as sess:
with client:
with client.session_transaction() as sess:
assert len(sess) == 0
sess['foo'] = [42]
assert len(sess) == 1
rv = c.get('/')
rv = client.get('/')
assert rv.data == b'[42]'
with c.session_transaction() as sess:
with client.session_transaction() as sess:
assert len(sess) == 1
assert sess['foo'] == [42]
@ -146,6 +144,7 @@ def test_session_transactions_keep_context(app, client, req_ctx):
with client.session_transaction():
assert req is flask.request._get_current_object()
def test_session_transaction_needs_cookies(app):
c = app.test_client(use_cookies=False)
with pytest.raises(RuntimeError) as e:
@ -154,9 +153,9 @@ def test_session_transaction_needs_cookies(app):
assert 'cookies' in str(e.value)
def test_test_client_context_binding():
app = flask.Flask(__name__)
def test_test_client_context_binding(app, client):
app.config['LOGGER_HANDLER_POLICY'] = 'never'
app.testing = False
@app.route('/')
def index():
@ -167,13 +166,13 @@ def test_test_client_context_binding():
def other():
1 // 0
with app.test_client() as c:
resp = c.get('/')
with client:
resp = client.get('/')
assert flask.g.value == 42
assert resp.data == b'Hello World!'
assert resp.status_code == 200
resp = c.get('/other')
resp = client.get('/other')
assert not hasattr(flask.g, 'value')
assert b'Internal Server Error' in resp.data
assert resp.status_code == 500
@ -187,58 +186,52 @@ def test_test_client_context_binding():
raise AssertionError('some kind of exception expected')
def test_reuse_client():
app = flask.Flask(__name__)
c = app.test_client()
def test_reuse_client(client):
c = client
with c:
assert c.get('/').status_code == 404
assert client.get('/').status_code == 404
with c:
assert c.get('/').status_code == 404
assert client.get('/').status_code == 404
def test_test_client_calls_teardown_handlers():
app = flask.Flask(__name__)
def test_test_client_calls_teardown_handlers(app, client):
called = []
@app.teardown_request
def remember(error):
called.append(error)
with app.test_client() as c:
with client:
assert called == []
c.get('/')
client.get('/')
assert called == []
assert called == [None]
del called[:]
with app.test_client() as c:
with client:
assert called == []
c.get('/')
client.get('/')
assert called == []
c.get('/')
client.get('/')
assert called == [None]
assert called == [None, None]
def test_full_url_request():
app = flask.Flask(__name__)
app.testing = True
def test_full_url_request(app, client):
@app.route('/action', methods=['POST'])
def action():
return 'x'
with app.test_client() as c:
rv = c.post('http://domain.com/action?vodka=42', data={'gin': 43})
with client:
rv = client.post('http://domain.com/action?vodka=42', data={'gin': 43})
assert rv.status_code == 200
assert 'gin' in flask.request.form
assert 'vodka' in flask.request.args
def test_subdomain():
app = flask.Flask(__name__)
def test_subdomain(app, client):
app.config['SERVER_NAME'] = 'example.com'
@app.route('/', subdomain='<company_id>')
@ -248,15 +241,14 @@ def test_subdomain():
with app.test_request_context():
url = flask.url_for('view', company_id='xxx')
with app.test_client() as c:
response = c.get(url)
with client:
response = client.get(url)
assert 200 == response.status_code
assert b'xxx' == response.data
def test_nosubdomain():
app = flask.Flask(__name__)
def test_nosubdomain(app, client):
app.config['SERVER_NAME'] = 'example.com'
@app.route('/<company_id>')
@ -266,8 +258,8 @@ def test_nosubdomain():
with app.test_request_context():
url = flask.url_for('view', company_id='xxx')
with app.test_client() as c:
response = c.get(url)
with client:
response = client.get(url)
assert 200 == response.status_code
assert b'xxx' == response.data