Skip to content
Myr

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 file

Take it

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

lines 1–10why, one step back
1# -*- 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 back
11import pytest

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

lines 12–14why, one step back
12
13import flask14import unittest

Remove flask superclassMarkus Unterwaditzerbefore adoptionaf4cb0f

line 15why, one step back
15

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

lines 16–18why, one step back
16from flask._compat import text_type
1718

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 19–26why, one step back
19def 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.url

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 27why, one step back
27

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 28–32why, one step back
28 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 back
33

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 34–39why, one step back
34def test_environ_defaults():
35 app = flask.Flask(__name__)36 app.testing = True37 @app.route('/')38 def index():39 return flask.request.url

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 40why, one step back
40

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 41–45why, one step back
41 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 back
46

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 47–49why, one step back
47def test_redirect_keep_session():
48 app = flask.Flask(__name__)49 app.secret_key = 'testing'

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 50why, one step back
50

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 51–56why, one step back
51 @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 back
57

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 58–60why, one step back
58 @app.route('/getsession')
59 def get_session():60 return flask.session.get('data', '<missing>')

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 61why, one step back
61

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 62–64why, one step back
62 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 back
65

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 66–70why, one step back
66 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 back
71

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 72–73why, one step back
72 # This support requires a new Werkzeug version
73 if not hasattr(c, 'redirect_client'):

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

lines 74–75why, one step back
74 assert flask.session.get('data') == 'foo'
75

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 76–77why, one step back
76 rv = c.get('/getsession')
77 assert rv.data == b'foo'

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 78why, one step back
78

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 79–82why, one step back
79def 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 back
83

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 84–86why, one step back
84 @app.route('/')
85 def index():86 return text_type(flask.session['foo'])

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 87why, one step back
87

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 88–97why, one step back
88 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 back
98

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 99–101why, one step back
99def test_session_transactions_no_null_sessions():
100 app = flask.Flask(__name__)101 app.testing = True

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 102why, one step back
102

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

line 103why, one step back
103 with app.test_client() as c:

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 104why, one step back
104 try:

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

line 105why, one step back
105 with c.session_transaction() as sess:

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

lines 106–107why, one step back
106 pass
107 except RuntimeError as e:

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

line 108why, one step back
108 assert 'Session backend did not open a session' in str(e)

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 109why, one step back
109 else:

Rewrite assertion methodsMarkus Unterwaditzerbefore adoption5da2c00

line 110why, one step back
110 assert False, 'Expected runtime error'

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 111why, one step back
111

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 112–115why, one step back
112def 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 back
116

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 117–122why, one step back
117 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 back
123

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 124–129why, one step back
124def 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 back
130 pass

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 131–134why, one step back
131 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 back
135

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 136–142why, one step back
136def 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 back
143

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 144–146why, one step back
144 @app.route('/other')
145 def other():146 1 // 0

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 147why, one step back
147

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 148–152why, one step back
148 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 == 200

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 153why, one step back
153

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 154–158why, one step back
154 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 = 23

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 159why, one step back
159

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 160–165why, one step back
160 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 back
166

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 167–169why, one step back
167def test_reuse_client():
168 app = flask.Flask(__name__)169 c = app.test_client()

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 170why, one step back
170

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 171–172why, one step back
171 with c:
172 assert c.get('/').status_code == 404

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 173why, one step back
173

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 174–175why, one step back
174 with c:
175 assert c.get('/').status_code == 404

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 176why, one step back
176

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 177–182why, one step back
177def 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 back
183

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 184–188why, one step back
184 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 back
189

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 190–197why, one step back
190 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 back
198

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 199–201why, one step back
199def test_full_url_request():
200 app = flask.Flask(__name__)201 app.testing = True

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 202why, one step back
202

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 203–205why, one step back
203 @app.route('/action', methods=['POST'])
204 def action():205 return 'x'

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 206why, one step back
206

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 207–211why, one step back
207 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.args

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 212why, one step back
212

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 213–218why, one step back
213def 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_id

Tests pass now.Markus Unterwaditzerbefore adoption8fa5e32

line 219why, one step back
219

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

line 220why, one step back
220 with app.test_request_context():

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

lines 221–222why, one step back
221 url = flask.url_for('view', company_id='xxx')
222

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 223–224why, one step back
223 with app.test_client() as c:
224 response = c.get(url)

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 225why, one step back
225

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 226–227why, one step back
226 assert 200 == response.status_code
227 assert b'xxx' == response.data

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 228why, one step back
228

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 229–234why, one step back
229def 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_id

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

line 235why, one step back
235

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

line 236why, one step back
236 with app.test_request_context():

Made tests recognizableMarkus Unterwaditzerbefore adoption961db8a

lines 237–238why, one step back
237 url = flask.url_for('view', company_id='xxx')
238

Kill classes in test_testingMarkus Unterwaditzerbefore adoptionf8a778d

lines 239–243why, one step back
239 with app.test_client() as c:
240 response = c.get(url)241242 assert 200 == response.status_code243 assert b'xxx' == response.data