test_testing.py
This file
Path
tests/test_testing.py
386 lines · 10.0 KiB · Python
Last changed by
Merge pull request #2635 from pallets/feature/server-name-routingDavid Lordbefore adoptionf808c20
History of this fileTake it
The walk stopped after 200 commits: lines older than f05aeea point at it.
Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 1–7why, one step back1# -*- coding: utf-8 -*-2"""3 tests.testing4 ~~~~~~~~~~~~~56 Test client and more.7Merge pull request #2624 from pallets/metadataDavid Lordbefore adoption4309c7c
line 8why, one step back8 :copyright: © 2010 by the Pallets team.Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 9–10why, one step back9 :license: BSD, see LICENSE for more details.10"""Merge pull request #2636 from pallets/test-cli-runnerDavid Lordbefore adoption4a7db66
line 11why, one step back11import clickCorrect the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 12–14why, one step back12import pytest1314import flaskresolve merge conflictsKenneth Reitzbefore adoption31174fe
line 15why, one step back15import werkzeugCorrect the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 16–17why, one step back1617from flask._compat import text_typeMerge pull request #2636 from pallets/test-cli-runnerDavid Lordbefore adoption4a7db66
line 18why, one step back18from flask.cli import ScriptInfoMerge pull request #2358 from davidism/json-mixinDavid Lordbefore adoption63129e8
line 19why, one step back19from flask.json import jsonifyMerge pull request #2636 from pallets/test-cli-runnerDavid Lordbefore adoption4a7db66
line 20why, one step back20from flask.testing import make_test_environ_builder, FlaskCliRunnerCorrect the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 21–22why, one step back2122resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 23why, one step back23def test_environ_defaults_from_config(app, client):Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 24–25why, one step back24 app.config['SERVER_NAME'] = 'example.com:1234'25 app.config['APPLICATION_ROOT'] = '/foo'resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 26why, one step back26Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 27–33why, one step back27 @app.route('/')28 def index():29 return flask.request.url3031 ctx = app.test_request_context()32 assert ctx.request.url == 'http://example.com:1234/foo/'33resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 34–38why, one step back34 rv = client.get('/')35 assert rv.data == b'http://example.com:1234/foo/'363738def test_environ_defaults(app, client, app_ctx, req_ctx):Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 39–44why, one step back39 @app.route('/')40 def index():41 return flask.request.url4243 ctx = app.test_request_context()44 assert ctx.request.url == 'http://localhost/'resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 45–46why, one step back45 with client:46 rv = client.get('/')Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 47–48why, one step back47 assert rv.data == b'http://localhost/'48resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 49–77why, one step back4950def test_environ_base_default(app, client, app_ctx):51 @app.route('/')52 def index():53 flask.g.user_agent = flask.request.headers["User-Agent"]54 return flask.request.remote_addr5556 rv = client.get('/')57 assert rv.data == b'127.0.0.1'58 assert flask.g.user_agent == 'werkzeug/' + werkzeug.__version__596061def test_environ_base_modified(app, client, app_ctx):62 @app.route('/')63 def index():64 flask.g.user_agent = flask.request.headers["User-Agent"]65 return flask.request.remote_addr6667 client.environ_base['REMOTE_ADDR'] = '0.0.0.0'68 client.environ_base['HTTP_USER_AGENT'] = 'Foo'69 rv = client.get('/')70 assert rv.data == b'0.0.0.0'71 assert flask.g.user_agent == 'Foo'7273 client.environ_base['REMOTE_ADDR'] = '0.0.0.1'74 client.environ_base['HTTP_USER_AGENT'] = 'Bar'75 rv = client.get('/')76 assert rv.data == b'0.0.0.1'77 assert flask.g.user_agent == 'Bar'support passing environ to test client (#2412)David Lordbefore adoption59f7966
lines 78–94why, one step back787980def test_client_open_environ(app, client, request):81 @app.route('/index')82 def index():83 return flask.request.remote_addr8485 builder = make_test_environ_builder(app, path='/index', method='GET')86 request.addfinalizer(builder.close)8788 rv = client.open(builder)89 assert rv.data == b'127.0.0.1'9091 environ = builder.get_environ()92 client.environ_base['REMOTE_ADDR'] = '127.0.0.2'93 rv = client.open(environ)94 assert rv.data == b'127.0.0.2'resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 95–96why, one step back9596Merge remote-tracking branch 'origin/0.10-maintenance'David Lordbefore adoptiona37f675
lines 97–108why, one step back97def test_specify_url_scheme(app, client):98 @app.route('/')99 def index():100 return flask.request.url101102 ctx = app.test_request_context(url_scheme='https')103 assert ctx.request.url == 'https://localhost/'104105 rv = client.get('/', url_scheme='https')106 assert rv.data == b'https://localhost/'107108Merge pull request #2430 from erebus1/env_url_scheme_fixDavid Lordbefore adoption85fa8aa
lines 109–116why, one step back109def test_path_is_url(app):110 eb = make_test_environ_builder(app, 'https://example.com/')111 assert eb.url_scheme == 'https'112 assert eb.host == 'example.com'113 assert eb.script_root == ''114 assert eb.path == '/'115116Merge pull request #2635 from pallets/feature/server-name-routingDavid Lordbefore adoptionf808c20
lines 117–118why, one step back117def test_blueprint_with_subdomain():118 app = flask.Flask(__name__, subdomain_matching=True)Merge remote-tracking branch 'origin/0.10-maintenance'David Lordbefore adoptiona37f675
lines 119–120why, one step back119 app.config['SERVER_NAME'] = 'example.com:1234'120 app.config['APPLICATION_ROOT'] = '/foo'Merge pull request #2635 from pallets/feature/server-name-routingDavid Lordbefore adoptionf808c20
line 121why, one step back121 client = app.test_client()Merge remote-tracking branch 'origin/0.10-maintenance'David Lordbefore adoptiona37f675
lines 122–138why, one step back122123 bp = flask.Blueprint('company', __name__, subdomain='xxx')124125 @bp.route('/')126 def index():127 return flask.request.url128129 app.register_blueprint(bp)130131 ctx = app.test_request_context('/', subdomain='xxx')132 assert ctx.request.url == 'http://xxx.example.com:1234/foo/'133 assert ctx.request.blueprint == bp.name134135 rv = client.get('/', subdomain='xxx')136 assert rv.data == b'http://xxx.example.com:1234/foo/'137138resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 139why, one step back139def test_redirect_keep_session(app, client, app_ctx):Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 140–150why, one step back140 @app.route('/', methods=['GET', 'POST'])141 def index():142 if flask.request.method == 'POST':143 return flask.redirect('/getsession')144 flask.session['data'] = 'foo'145 return 'index'146147 @app.route('/getsession')148 def get_session():149 return flask.session.get('data', '<missing>')150resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 151–152why, one step back151 with client:152 rv = client.get('/getsession')Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 153–154why, one step back153 assert rv.data == b'<missing>'154resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 155why, one step back155 rv = client.get('/')Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 156–157why, one step back156 assert rv.data == b'index'157 assert flask.session.get('data') == 'foo'resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 158why, one step back158 rv = client.post('/', data={}, follow_redirects=True)Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 159–161why, one step back159 assert rv.data == b'foo'160161 # This support requires a new Werkzeug versionresolve merge conflictsKenneth Reitzbefore adoption31174fe
line 162why, one step back162 if not hasattr(client, 'redirect_client'):Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 163–164why, one step back163 assert flask.session.get('data') == 'foo'164resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 165why, one step back165 rv = client.get('/getsession')Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 166–167why, one step back166 assert rv.data == b'foo'167resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 168–169why, one step back168169def test_session_transactions(app, client):Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 170–173why, one step back170 @app.route('/')171 def index():172 return text_type(flask.session['foo'])173resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 174–175why, one step back174 with client:175 with client.session_transaction() as sess:Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 176–178why, one step back176 assert len(sess) == 0177 sess['foo'] = [42]178 assert len(sess) == 1resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 179why, one step back179 rv = client.get('/')Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
line 180why, one step back180 assert rv.data == b'[42]'resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 181why, one step back181 with client.session_transaction() as sess:Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 182–184why, one step back182 assert len(sess) == 1183 assert sess['foo'] == [42]184resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 185why, one step back185Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 186–190why, one step back186def test_session_transactions_no_null_sessions():187 app = flask.Flask(__name__)188 app.testing = True189190 with app.test_client() as c:resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 191why, one step back191 with pytest.raises(RuntimeError) as e:Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 192–193why, one step back192 with c.session_transaction() as sess:193 passresolve merge conflictsKenneth Reitzbefore adoption31174fe
line 194why, one step back194 assert 'Session backend did not open a session' in str(e.value)Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
line 195why, one step back195resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 196–202why, one step back196197def test_session_transactions_keep_context(app, client, req_ctx):198 rv = client.get('/')199 req = flask.request._get_current_object()200 assert req is not None201 with client.session_transaction():202 assert req is flask.request._get_current_object()Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
line 203why, one step back203resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 204–205why, one step back204205def test_session_transaction_needs_cookies(app):Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
line 206why, one step back206 c = app.test_client(use_cookies=False)resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 207why, one step back207 with pytest.raises(RuntimeError) as e:Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 208–209why, one step back208 with c.session_transaction() as s:209 passresolve merge conflictsKenneth Reitzbefore adoption31174fe
line 210why, one step back210 assert 'cookies' in str(e.value)Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
line 211why, one step back211resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 212–215why, one step back212213def test_test_client_context_binding(app, client):214 app.testing = False215Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 216–224why, one step back216 @app.route('/')217 def index():218 flask.g.value = 42219 return 'Hello World!'220221 @app.route('/other')222 def other():223 1 // 0224resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 225–226why, one step back225 with client:226 resp = client.get('/')Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 227–230why, one step back227 assert flask.g.value == 42228 assert resp.data == b'Hello World!'229 assert resp.status_code == 200230resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 231why, one step back231 resp = client.get('/other')Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 232–244why, one step back232 assert not hasattr(flask.g, 'value')233 assert b'Internal Server Error' in resp.data234 assert resp.status_code == 500235 flask.g.value = 23236237 try:238 flask.g.value239 except (AttributeError, RuntimeError):240 pass241 else:242 raise AssertionError('some kind of exception expected')243244resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 245–247why, one step back245def test_reuse_client(client):246 c = client247Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
line 248why, one step back248 with c:resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 249why, one step back249 assert client.get('/').status_code == 404Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 250–251why, one step back250251 with c:resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 252why, one step back252 assert client.get('/').status_code == 404Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
line 253why, one step back253resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 254–255why, one step back254255def test_test_client_calls_teardown_handlers(app, client):Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
line 256why, one step back256 called = []resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 257why, one step back257Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 258–261why, one step back258 @app.teardown_request259 def remember(error):260 called.append(error)261resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 262why, one step back262 with client:Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
line 263why, one step back263 assert called == []resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 264why, one step back264 client.get('/')Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 265–268why, one step back265 assert called == []266 assert called == [None]267268 del called[:]resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 269why, one step back269 with client:Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
line 270why, one step back270 assert called == []resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 271why, one step back271 client.get('/')Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
line 272why, one step back272 assert called == []resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 273why, one step back273 client.get('/')Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 274–277why, one step back274 assert called == [None]275 assert called == [None, None]276277resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 278why, one step back278def test_full_url_request(app, client):Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 279–282why, one step back279 @app.route('/action', methods=['POST'])280 def action():281 return 'x'282resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 283–284why, one step back283 with client:284 rv = client.post('http://domain.com/action?vodka=42', data={'gin': 43})Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 285–287why, one step back285 assert rv.status_code == 200286 assert 'gin' in flask.request.form287 assert 'vodka' in flask.request.argsMerge pull request #2358 from davidism/json-mixinDavid Lordbefore adoption63129e8
lines 288–306why, one step back288289290def test_json_request_and_response(app, client):291 @app.route('/echo', methods=['POST'])292 def echo():293 return jsonify(flask.request.get_json())294295 with client:296 json_data = {'drink': {'gin': 1, 'tonic': True}, 'price': 10}297 rv = client.post('/echo', json=json_data)298299 # Request should be in JSON300 assert flask.request.is_json301 assert flask.request.get_json() == json_data302303 # Response should be in JSON304 assert rv.status_code == 200305 assert rv.is_json306 assert rv.get_json() == json_dataCorrect the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
line 307why, one step back307resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 308why, one step back308Merge pull request #2635 from pallets/feature/server-name-routingDavid Lordbefore adoptionf808c20
lines 309–310why, one step back309def test_subdomain():310 app = flask.Flask(__name__, subdomain_matching=True)Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
line 311why, one step back311 app.config['SERVER_NAME'] = 'example.com'Merge pull request #2635 from pallets/feature/server-name-routingDavid Lordbefore adoptionf808c20
line 312why, one step back312 client = app.test_client()resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 313why, one step back313Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 314–320why, one step back314 @app.route('/', subdomain='<company_id>')315 def view(company_id):316 return company_id317318 with app.test_request_context():319 url = flask.url_for('view', company_id='xxx')320resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 321–322why, one step back321 with client:322 response = client.get(url)Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 323–326why, one step back323324 assert 200 == response.status_code325 assert b'xxx' == response.data326resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 327–328why, one step back327328def test_nosubdomain(app, client):Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
line 329why, one step back329 app.config['SERVER_NAME'] = 'example.com'resolve merge conflictsKenneth Reitzbefore adoption31174fe
line 330why, one step back330Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 331–337why, one step back331 @app.route('/<company_id>')332 def view(company_id):333 return company_id334335 with app.test_request_context():336 url = flask.url_for('view', company_id='xxx')337resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 338–339why, one step back338 with client:339 response = client.get(url)Correct the order of suggested syntax for extension importsJeff Widmanbefore adoptionf05aeea
lines 340–342why, one step back340341 assert 200 == response.status_code342 assert b'xxx' == response.dataMerge pull request #2636 from pallets/test-cli-runnerDavid Lordbefore adoption4a7db66
lines 343–386why, one step back343344345def test_cli_runner_class(app):346 runner = app.test_cli_runner()347 assert isinstance(runner, FlaskCliRunner)348349 class SubRunner(FlaskCliRunner):350 pass351352 app.test_cli_runner_class = SubRunner353 runner = app.test_cli_runner()354 assert isinstance(runner, SubRunner)355356357def test_cli_invoke(app):358 @app.cli.command('hello')359 def hello_command():360 click.echo('Hello, World!')361362 runner = app.test_cli_runner()363 # invoke with command name364 result = runner.invoke(args=['hello'])365 assert 'Hello' in result.output366 # invoke with command object367 result = runner.invoke(hello_command)368 assert 'Hello' in result.output369370371def test_cli_custom_obj(app):372 class NS(object):373 called = False374375 def create_app():376 NS.called = True377 return app378379 @app.cli.command('hello')380 def hello_command():381 click.echo('Hello, World!')382383 script_info = ScriptInfo(create_app=create_app)384 runner = app.test_cli_runner()385 runner.invoke(hello_command, obj=script_info)386 assert NS.called