test_testing.py
This file
Path
tests/test_testing.py
243 lines · 6.4 KiB · Python
Last changed by
Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
History of this fileTake it
Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
lines 1–10why, one step back1# -*- coding: utf-8 -*-2"""3 tests.testing4 ~~~~~~~~~~~~~~~~~~~~~~~56 Test client and more.78 :copyright: (c) 2014 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""Tests pass now.Markus Unterwaditzerbefore adoption8fa5e32
line 11why, one step back11import pytestMade tests recognizableMarkus Unterwaditzerbefore adoption961db8a
lines 12–14why, one step back1213import flask14import unittestRemove flask superclassMarkus Unterwaditzerbefore adoptionaf4cb0f
line 15why, one step back15Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
lines 16–18why, one step back16from flask._compat import text_type1718Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 19–26why, one step back19def test_environ_defaults_from_config():20 app = flask.Flask(__name__)21 app.testing = True22 app.config['SERVER_NAME'] = 'example.com:1234'23 app.config['APPLICATION_ROOT'] = '/foo'24 @app.route('/')25 def index():26 return flask.request.urlMade tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 27why, one step back27Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 28–32why, one step back28 ctx = app.test_request_context()29 assert ctx.request.url == 'http://example.com:1234/foo/'30 with app.test_client() as c:31 rv = c.get('/')32 assert rv.data == b'http://example.com:1234/foo/'Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 33why, one step back33Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 34–39why, one step back34def test_environ_defaults():35 app = flask.Flask(__name__)36 app.testing = True37 @app.route('/')38 def index():39 return flask.request.urlMade tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 40why, one step back40Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 41–45why, one step back41 ctx = app.test_request_context()42 assert ctx.request.url == 'http://localhost/'43 with app.test_client() as c:44 rv = c.get('/')45 assert rv.data == b'http://localhost/'Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 46why, one step back46Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 47–49why, one step back47def test_redirect_keep_session():48 app = flask.Flask(__name__)49 app.secret_key = 'testing'Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 50why, one step back50Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 51–56why, one step back51 @app.route('/', methods=['GET', 'POST'])52 def index():53 if flask.request.method == 'POST':54 return flask.redirect('/getsession')55 flask.session['data'] = 'foo'56 return 'index'Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 57why, one step back57Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 58–60why, one step back58 @app.route('/getsession')59 def get_session():60 return flask.session.get('data', '<missing>')Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 61why, one step back61Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 62–64why, one step back62 with app.test_client() as c:63 rv = c.get('/getsession')64 assert rv.data == b'<missing>'Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 65why, one step back65Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 66–70why, one step back66 rv = c.get('/')67 assert rv.data == b'index'68 assert flask.session.get('data') == 'foo'69 rv = c.post('/', data={}, follow_redirects=True)70 assert rv.data == b'foo'Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 71why, one step back71Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 72–73why, one step back72 # This support requires a new Werkzeug version73 if not hasattr(c, 'redirect_client'):Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
lines 74–75why, one step back74 assert flask.session.get('data') == 'foo'75Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 76–77why, one step back76 rv = c.get('/getsession')77 assert rv.data == b'foo'Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 78why, one step back78Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 79–82why, one step back79def test_session_transactions():80 app = flask.Flask(__name__)81 app.testing = True82 app.secret_key = 'testing'Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 83why, one step back83Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 84–86why, one step back84 @app.route('/')85 def index():86 return text_type(flask.session['foo'])Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 87why, one step back87Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 88–97why, one step back88 with app.test_client() as c:89 with c.session_transaction() as sess:90 assert len(sess) == 091 sess['foo'] = [42]92 assert len(sess) == 193 rv = c.get('/')94 assert rv.data == b'[42]'95 with c.session_transaction() as sess:96 assert len(sess) == 197 assert sess['foo'] == [42]Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 98why, one step back98Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 99–101why, one step back99def test_session_transactions_no_null_sessions():100 app = flask.Flask(__name__)101 app.testing = TrueMade tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 102why, one step back102Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
line 103why, one step back103 with app.test_client() as c:Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 104why, one step back104 try:Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
line 105why, one step back105 with c.session_transaction() as sess:Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
lines 106–107why, one step back106 pass107 except RuntimeError as e:Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
line 108why, one step back108 assert 'Session backend did not open a session' in str(e)Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 109why, one step back109 else:Rewrite assertion methodsMarkus Unterwaditzerbefore adoption5da2c00
line 110why, one step back110 assert False, 'Expected runtime error'Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 111why, one step back111Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 112–115why, one step back112def test_session_transactions_keep_context():113 app = flask.Flask(__name__)114 app.testing = True115 app.secret_key = 'testing'Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 116why, one step back116Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 117–122why, one step back117 with app.test_client() as c:118 rv = c.get('/')119 req = flask.request._get_current_object()120 assert req is not None121 with c.session_transaction():122 assert req is flask.request._get_current_object()Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 123why, one step back123Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 124–129why, one step back124def test_session_transaction_needs_cookies():125 app = flask.Flask(__name__)126 app.testing = True127 c = app.test_client(use_cookies=False)128 try:129 with c.session_transaction() as s:Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 130why, one step back130 passKill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 131–134why, one step back131 except RuntimeError as e:132 assert 'cookies' in str(e)133 else:134 assert False, 'Expected runtime error'Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 135why, one step back135Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 136–142why, one step back136def test_test_client_context_binding():137 app = flask.Flask(__name__)138 app.config['LOGGER_HANDLER_POLICY'] = 'never'139 @app.route('/')140 def index():141 flask.g.value = 42142 return 'Hello World!'Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 143why, one step back143Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 144–146why, one step back144 @app.route('/other')145 def other():146 1 // 0Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 147why, one step back147Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 148–152why, one step back148 with app.test_client() as c:149 resp = c.get('/')150 assert flask.g.value == 42151 assert resp.data == b'Hello World!'152 assert resp.status_code == 200Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 153why, one step back153Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 154–158why, one step back154 resp = c.get('/other')155 assert not hasattr(flask.g, 'value')156 assert b'Internal Server Error' in resp.data157 assert resp.status_code == 500158 flask.g.value = 23Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 159why, one step back159Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 160–165why, one step back160 try:161 flask.g.value162 except (AttributeError, RuntimeError):163 pass164 else:165 raise AssertionError('some kind of exception expected')Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 166why, one step back166Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 167–169why, one step back167def test_reuse_client():168 app = flask.Flask(__name__)169 c = app.test_client()Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 170why, one step back170Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 171–172why, one step back171 with c:172 assert c.get('/').status_code == 404Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 173why, one step back173Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 174–175why, one step back174 with c:175 assert c.get('/').status_code == 404Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 176why, one step back176Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 177–182why, one step back177def test_test_client_calls_teardown_handlers():178 app = flask.Flask(__name__)179 called = []180 @app.teardown_request181 def remember(error):182 called.append(error)Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 183why, one step back183Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 184–188why, one step back184 with app.test_client() as c:185 assert called == []186 c.get('/')187 assert called == []188 assert called == [None]Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 189why, one step back189Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 190–197why, one step back190 del called[:]191 with app.test_client() as c:192 assert called == []193 c.get('/')194 assert called == []195 c.get('/')196 assert called == [None]197 assert called == [None, None]Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 198why, one step back198Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 199–201why, one step back199def test_full_url_request():200 app = flask.Flask(__name__)201 app.testing = TrueMade tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 202why, one step back202Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 203–205why, one step back203 @app.route('/action', methods=['POST'])204 def action():205 return 'x'Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 206why, one step back206Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 207–211why, one step back207 with app.test_client() as c:208 rv = c.post('http://domain.com/action?vodka=42', data={'gin': 43})209 assert rv.status_code == 200210 assert 'gin' in flask.request.form211 assert 'vodka' in flask.request.argsMade tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 212why, one step back212Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 213–218why, one step back213def test_subdomain():214 app = flask.Flask(__name__)215 app.config['SERVER_NAME'] = 'example.com'216 @app.route('/', subdomain='<company_id>')217 def view(company_id):218 return company_idTests pass now.Markus Unterwaditzerbefore adoption8fa5e32
line 219why, one step back219Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
line 220why, one step back220 with app.test_request_context():Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
lines 221–222why, one step back221 url = flask.url_for('view', company_id='xxx')222Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 223–224why, one step back223 with app.test_client() as c:224 response = c.get(url)Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 225why, one step back225Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 226–227why, one step back226 assert 200 == response.status_code227 assert b'xxx' == response.dataMade tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 228why, one step back228Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 229–234why, one step back229def test_nosubdomain():230 app = flask.Flask(__name__)231 app.config['SERVER_NAME'] = 'example.com'232 @app.route('/<company_id>')233 def view(company_id):234 return company_idMade tests recognizableMarkus Unterwaditzerbefore adoption961db8a
line 235why, one step back235Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
line 236why, one step back236 with app.test_request_context():Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a
lines 237–238why, one step back237 url = flask.url_for('view', company_id='xxx')238Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d
lines 239–243why, one step back239 with app.test_client() as c:240 response = c.get(url)241242 assert 200 == response.status_code243 assert b'xxx' == response.data