Codebase list flask-testing / upstream/0.8.0
New upstream version 0.8.0 Nilesh Patra 3 years ago
12 changed file(s) with 464 addition(s) and 86 deletion(s). Raw diff Collapse all Expand all
00 Metadata-Version: 1.1
11 Name: Flask-Testing
2 Version: 0.4.2
2 Version: 0.8.0
33 Summary: Unit testing for Flask
44 Home-page: https://github.com/jarus/flask-testing
55 Author: Dan Jacob
00 Metadata-Version: 1.1
11 Name: Flask-Testing
2 Version: 0.4.2
2 Version: 0.8.0
33 Summary: Unit testing for Flask
44 Home-page: https://github.com/jarus/flask-testing
55 Author: Dan Jacob
0 Flask-Testing
0 flask-testing
11
22 Unit testing support for Flask.
33
44 For full information please refer to the online docs:
55
6 http://packages.python.org/Flask-Testing
6 https://flask-testing.readthedocs.io/en/latest/
Binary diff not shown
2525
2626 Simply subclass the ``TestCase`` class::
2727
28 from flask.ext.testing import TestCase
28 from flask_testing import TestCase
2929
3030 class MyTest(TestCase):
3131
3535 You must specify the ``create_app`` method, which should return a Flask instance::
3636
3737 from flask import Flask
38 from flask.ext.testing import TestCase
38 from flask_testing import TestCase
3939
4040 class MyTest(TestCase):
4141
5656
5757 import urllib2
5858 from flask import Flask
59 from flask.ext.testing import LiveServerTestCase
59 from flask_testing import LiveServerTestCase
6060
6161 class MyTest(LiveServerTestCase):
6262
6565 app.config['TESTING'] = True
6666 # Default port is 5000
6767 app.config['LIVESERVER_PORT'] = 8943
68 # Default timeout is 5 seconds
69 app.config['LIVESERVER_TIMEOUT'] = 10
6870 return app
6971
7072 def test_server_is_up_and_running(self):
7274 self.assertEqual(response.code, 200)
7375
7476 The method ``get_server_url`` will return http://localhost:8943 in this case.
77
78
79 Dynamic LiveServerTestCase port
80 -------------------------------
81
82 By default, ``LiveServerTestCase`` will use the pre-defined port for running the live server. If
83 multiple tests need to run in parallel, the ``LIVESERVER_PORT`` can be set to ``0`` to have the
84 underlying operating system pick an open port for the server. The full address of the running
85 server can be accessed via the ``get_server_url`` call on the test case::
86
87
88 import urllib2
89 from flask import Flask
90 from flask_testing import LiveServerTestCase
91
92 class MyTest(LiveServerTestCase):
93
94 def create_app(self):
95 app = Flask(__name__)
96 app.config['TESTING'] = True
97
98 # Set to 0 to have the OS pick the port.
99 app.config['LIVESERVER_PORT'] = 0
100
101 return app
102
103 def test_server_is_up_and_running(self):
104 response = urllib2.urlopen(self.get_server_url())
105 self.assertEqual(response.code, 200)
75106
76107
77108 Testing JSON responses
149180 First, ensure you set the database URI to something other than your production database ! Second,
150181 it's usually a good idea to create and drop your tables with each test run, to ensure clean tests::
151182
152 from flask.ext.testing import TestCase
183 from flask_testing import TestCase
153184
154185 from myapp import create_app, db
155186
214245 with unittest
215246 -------------
216247
217 For the beginning I go on the theory that you put all your tests into one file
218 than you can use the :func:`unittest.main` function. This function will discover
219 all your test methods in your :class:`TestCase` classes. Remember, the test
220 methods and classes must starts with ``test`` (case-insensitive) that they will
221 discover.
248 I recommend you to put all your tests into one file so that you can use
249 the :func:`unittest.main` function. This function will discover all your test
250 methods in your :class:`TestCase` classes. Remember, the names of the test
251 methods and classes must start with ``test`` (case-insensitive) so that
252 they can be discovered.
222253
223254 An example test file could look like this::
224255
225256 import unittest
226 import flask.ext.testing
257 import flask_testing
227258
228259 # your test cases
229260
239270
240271 Changes
241272 =======
273
274 0.8.0 (05.03.2020)
275 ------------------
276
277 * Drops support for python 2.6, 3.3, and 3.4 due to end-of-life.
278 * Fixes werkzeug 1.0 compatibility issue with import paths
279
280 0.7.1 (19.12.2017)
281 ------------------
282
283 * Reverts the request context changes from ``0.7.0``. This change broke
284 backwards compatibility so it will be moved to a major version release
285 instead.
286
287 0.7.0 (18.12.2017)
288 ------------------
289
290 * Changes the way request contexts are managed. Let's Flask be responsible
291 for the context, which fixes some subtle bugs.
292
293 0.6.2 (26.02.2017)
294 ------------------
295
296 * Add support for OS chosen port in ``LiveServerTestCase``
297 * Better error messages when missing required modules
298 * ``assertRedirects`` now supports all valid redirect codes as specified
299 in the HTTP protocol
300 * Fixed bug that caused ``TypeError`` instead of ``AssertionError`` when
301 testing against used templates
302 * Fixed bug in ``assertRedirects`` where the location was not being
303 checked properly
304
305 0.6.1 (03.09.2016)
306 ------------------
307
308 * Fix issues that prevented tests from running when blinker was not installed
309
310 0.6.0 (02.09.2016)
311 ------------------
312
313 * ``LiveServerTestCase`` will now start running as soon as the server is up
314 * ``assertRedirects`` now respects the ``SERVER_NAME`` config value and can compare against absolute URLs
315 * Compatibility with Flask 0.11.1
316
317 0.5.0 (12.06.2016)
318 ------------------
319
320 * Improvements to ``LiveServerTestCase``
321
322 * The test case will now block until the server is available
323 * Fixed an issue where no request context was available
324 * Fixed an issue where tests would be run twice when ``DEBUG`` was set to True
325
326 * Add missing message arguments for assertRedirects and assertContext
327 * Better default failure message for assertRedirects
328 * Better default failure message for assertTemplateUsed
329 * Fix an issue that caused the ``render_templates`` option to not clean up after itself if set to False
330 * Update docs to use new Flask extension import specification
242331
243332 0.4.2 (24.07.2014)
244333 ------------------
268357 API
269358 ===
270359
271 .. module:: flask.ext.testing
360 .. module:: flask_testing
272361
273362 .. autoclass:: TestCase
274363 :members:
1010 from __future__ import absolute_import, with_statement
1111
1212 import gc
13 import multiprocessing
14 import socket
1315 import time
16
17 try:
18 import socketserver
19 except ImportError:
20 # Python 2 SocketServer fallback
21 import SocketServer as socketserver
22
1423 try:
1524 import unittest2 as unittest
1625 except ImportError:
1726 import unittest
18 import multiprocessing
19
20 from werkzeug import cached_property
27
28 try:
29 from urllib.parse import urlparse, urljoin
30 except ImportError:
31 # Python 2 urlparse fallback
32 from urlparse import urlparse, urljoin
33
34 from werkzeug.utils import cached_property
2135
2236 # Use Flask's preferred JSON module so that our runtime behavior matches.
2337 from flask import json_available, templating, template_rendered
38
39 try:
40 from flask import message_flashed
41
42 _is_message_flashed = True
43 except ImportError:
44 message_flashed = None
45 _is_message_flashed = False
46
2447 if json_available:
2548 from flask import json
2649
2851 # available in this version of Flask
2952 try:
3053 import blinker
54
3155 _is_signals = True
3256 except ImportError: # pragma: no cover
3357 _is_signals = False
4367 """
4468 Mixin with testing helper methods
4569 """
70
4671 @cached_property
4772 def json(self):
4873 if not json_available: # pragma: no cover
6893 return ""
6994
7095
96 def _check_for_message_flashed_support():
97 if not _is_signals or not _is_message_flashed:
98 raise RuntimeError(
99 "Your version of Flask doesn't support message_flashed. "
100 "This requires Flask 0.10+ with the blinker module installed."
101 )
102
103
104 def _check_for_signals_support():
105 if not _is_signals:
106 raise RuntimeError(
107 "Your version of Flask doesn't support signals. "
108 "This requires Flask 0.6+ with the blinker module installed."
109 )
110
111
71112 class TestCase(unittest.TestCase):
72
73113 render_templates = True
74114 run_gc_after_test = False
75115
92132 finally:
93133 self._post_teardown()
94134
135 def debug(self):
136 try:
137 self._pre_setup()
138 super(TestCase, self).debug()
139 finally:
140 self._post_teardown()
141
95142 def _pre_setup(self):
96143 self.app = self.create_app()
97144
109156 templating._render = _empty_render
110157
111158 self.templates = []
159 self.flashed_messages = []
160
112161 if _is_signals:
113162 template_rendered.connect(self._add_template)
163
164 if _is_message_flashed:
165 message_flashed.connect(self._add_flash_message)
166
167 def _add_flash_message(self, app, message, category):
168 self.flashed_messages.append((message, category))
114169
115170 def _add_template(self, app, template, context):
116171 if len(self.templates) > 0:
133188 if hasattr(self, 'templates'):
134189 del self.templates
135190
191 if hasattr(self, 'flashed_messages'):
192 del self.flashed_messages
193
136194 if _is_signals:
137195 template_rendered.disconnect(self._add_template)
138 if hasattr(self, '_true_render'):
139 templating._render = self._true_render
196
197 if _is_message_flashed:
198 message_flashed.disconnect(self._add_flash_message)
199
200 if hasattr(self, '_original_template_render'):
201 templating._render = self._original_template_render
140202
141203 if self.run_gc_after_test:
142204 gc.collect()
205
206 def assertMessageFlashed(self, message, category='message'):
207 """
208 Checks if a given message was flashed.
209 Only works if your version of Flask has message_flashed
210 signal support (0.10+) and blinker is installed.
211
212 :param message: expected message
213 :param category: expected message category
214 """
215 _check_for_message_flashed_support()
216
217 for _message, _category in self.flashed_messages:
218 if _message == message and _category == category:
219 return True
220
221 raise AssertionError("Message '%s' in category '%s' wasn't flashed" % (message, category))
222
223 assert_message_flashed = assertMessageFlashed
143224
144225 def assertTemplateUsed(self, name, tmpl_name_attribute='name'):
145226 """
154235 :param name: template name
155236 :param tmpl_name_attribute: template engine specific attribute name
156237 """
157 if not _is_signals:
158 raise RuntimeError("Signals not supported")
238 _check_for_signals_support()
239
240 used_templates = []
159241
160242 for template, context in self.templates:
161243 if getattr(template, tmpl_name_attribute) == name:
162244 return True
163 raise AssertionError("template %s not used" % name)
245
246 used_templates.append(template)
247
248 raise AssertionError("Template %s not used. Templates were used: %s" % (name, ' '.join(repr(used_templates))))
164249
165250 assert_template_used = assertTemplateUsed
166251
176261 :versionadded: 0.2
177262 :param name: name of variable
178263 """
179 if not _is_signals:
180 raise RuntimeError("Signals not supported")
264 _check_for_signals_support()
181265
182266 for template, context in self.templates:
183267 if name in context:
184268 return context[name]
185269 raise ContextVariableDoesNotExist
186270
187 def assertContext(self, name, value):
271 def assertContext(self, name, value, message=None):
188272 """
189273 Checks if given name exists in the template context
190274 and equals the given value.
195279 """
196280
197281 try:
198 self.assertEqual(self.get_context_variable(name), value)
282 self.assertEqual(self.get_context_variable(name), value, message)
199283 except ContextVariableDoesNotExist:
200 self.fail("Context variable does not exist: %s" % name)
284 self.fail(message or "Context variable does not exist: %s" % name)
201285
202286 assert_context = assertContext
203287
204 def assertRedirects(self, response, location):
288 def assertRedirects(self, response, location, message=None):
205289 """
206290 Checks if response is an HTTP redirect to the
207291 given location.
208292
209293 :param response: Flask response
210 :param location: relative URL (i.e. without **http://localhost**)
211 """
212 self.assertTrue(response.status_code in (301, 302))
213 self.assertEqual(response.location, "http://localhost" + location)
294 :param location: relative URL path to SERVER_NAME or an absolute URL
295 """
296 parts = urlparse(location)
297
298 if parts.netloc:
299 expected_location = location
300 else:
301 server_name = self.app.config.get('SERVER_NAME') or 'localhost'
302 expected_location = urljoin("http://%s" % server_name, location)
303
304 valid_status_codes = (301, 302, 303, 305, 307)
305 valid_status_code_str = ', '.join(str(code) for code in valid_status_codes)
306 not_redirect = "HTTP Status %s expected but got %d" % (valid_status_code_str, response.status_code)
307 self.assertTrue(response.status_code in valid_status_codes, message or not_redirect)
308 self.assertEqual(response.location, expected_location, message)
214309
215310 assert_redirects = assertRedirects
216311
323418 # Inspired by https://docs.djangoproject.com/en/dev/topics/testing/#django.test.LiveServerTestCase
324419
325420 class LiveServerTestCase(unittest.TestCase):
326
327421 def create_app(self):
328422 """
329423 Create your Flask app here, with any
339433
340434 # Get the app
341435 self.app = self.create_app()
436
437 self._configured_port = self.app.config.get('LIVESERVER_PORT', 5000)
438 self._port_value = multiprocessing.Value('i', self._configured_port)
439
440 # We need to create a context in order for extensions to catch up
441 self._ctx = self.app.test_request_context()
442 self._ctx.push()
342443
343444 try:
344445 self._spawn_live_server()
345446 super(LiveServerTestCase, self).__call__(result)
346447 finally:
448 self._post_teardown()
347449 self._terminate_live_server()
348450
349451 def get_server_url(self):
350452 """
351453 Return the url of the test server
352454 """
353 return 'http://localhost:%s' % self.port
455 return 'http://localhost:%s' % self._port_value.value
354456
355457 def _spawn_live_server(self):
356458 self._process = None
357 self.port = self.app.config.get('LIVESERVER_PORT', 5000)
358
359 worker = lambda app, port: app.run(port=port)
459 port_value = self._port_value
460
461 def worker(app, port):
462 # Based on solution: http://stackoverflow.com/a/27598916
463 # Monkey-patch the server_bind so we can determine the port bound by Flask.
464 # This handles the case where the port specified is `0`, which means that
465 # the OS chooses the port. This is the only known way (currently) of getting
466 # the port out of Flask once we call `run`.
467 original_socket_bind = socketserver.TCPServer.server_bind
468 def socket_bind_wrapper(self):
469 ret = original_socket_bind(self)
470
471 # Get the port and save it into the port_value, so the parent process
472 # can read it.
473 (_, port) = self.socket.getsockname()
474 port_value.value = port
475 socketserver.TCPServer.server_bind = original_socket_bind
476 return ret
477
478 socketserver.TCPServer.server_bind = socket_bind_wrapper
479 app.run(port=port, use_reloader=False)
360480
361481 self._process = multiprocessing.Process(
362 target=worker, args=(self.app, self.port)
482 target=worker, args=(self.app, self._configured_port)
363483 )
364484
365485 self._process.start()
366486
367 # we must wait the server start listening
368 time.sleep(1)
487 # We must wait for the server to start listening, but give up
488 # after a specified maximum timeout
489 timeout = self.app.config.get('LIVESERVER_TIMEOUT', 5)
490 start_time = time.time()
491
492 while True:
493 elapsed_time = (time.time() - start_time)
494 if elapsed_time > timeout:
495 raise RuntimeError(
496 "Failed to start the server after %d seconds. " % timeout
497 )
498
499 if self._can_ping_server():
500 break
501
502 def _can_ping_server(self):
503 host, port = self._get_server_address()
504 if port == 0:
505 # Port specified by the user was 0, and the OS has not yet assigned
506 # the proper port.
507 return False
508
509 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
510 try:
511 sock.connect((host, port))
512 except socket.error as e:
513 success = False
514 else:
515 success = True
516 finally:
517 sock.close()
518
519 return success
520
521 def _get_server_address(self):
522 """
523 Gets the server address used to test the connection with a socket.
524 Respects both the LIVESERVER_PORT config value and overriding
525 get_server_url()
526 """
527 parts = urlparse(self.get_server_url())
528
529 host = parts.hostname
530 port = parts.port
531
532 if port is None:
533 if parts.scheme == 'http':
534 port = 80
535 elif parts.scheme == 'https':
536 port = 443
537 else:
538 raise RuntimeError(
539 "Unsupported server url scheme: %s" % parts.scheme
540 )
541
542 return host, port
543
544 def _post_teardown(self):
545 if getattr(self, '_ctx', None) is not None:
546 self._ctx.pop()
547 del self._ctx
369548
370549 def _terminate_live_server(self):
371550 if self._process:
00 [egg_info]
11 tag_build =
22 tag_date = 0
3 tag_svn_revision = 0
43
2222 ]
2323
2424 if sys.version_info[0] < 3:
25 tests_require.append('twill==0.9')
25 tests_require.append('twill==0.9.1')
2626
2727 if sys.version_info < (2, 6):
2828 tests_require.append('simplejson')
3030
3131 setup(
3232 name='Flask-Testing',
33 version='0.4.2',
33 version='0.8.0',
3434 url='https://github.com/jarus/flask-testing',
3535 license='BSD',
3636 author='Dan Jacob',
11
22 from flask_testing import is_twill_available
33
4 from .test_utils import TestSetup, TestSetupFailure, TestClientUtils, TestLiveServer, TestTeardownGraceful
54 from .test_twill import TestTwill, TestTwillDeprecated
5 from .test_utils import TestSetup, TestSetupFailure, TestClientUtils, \
6 TestLiveServer, TestTeardownGraceful, TestRenderTemplates, \
7 TestNotRenderTemplates, TestRestoreTheRealRender, \
8 TestLiveServerOSPicksPort
69
710
811 def suite():
912 suite = unittest.TestSuite()
1013 suite.addTest(unittest.makeSuite(TestSetup))
1114 suite.addTest(unittest.makeSuite(TestSetupFailure))
12 suite.addTest(unittest.makeSuite(TestTeardownGraceful))
1315 suite.addTest(unittest.makeSuite(TestClientUtils))
1416 suite.addTest(unittest.makeSuite(TestLiveServer))
17 suite.addTest(unittest.makeSuite(TestLiveServerOSPicksPort))
18 suite.addTest(unittest.makeSuite(TestTeardownGraceful))
19 suite.addTest(unittest.makeSuite(TestRenderTemplates))
20 suite.addTest(unittest.makeSuite(TestNotRenderTemplates))
21 suite.addTest(unittest.makeSuite(TestRestoreTheRealRender))
1522 if is_twill_available:
1623 suite.addTest(unittest.makeSuite(TestTwill))
1724 suite.addTest(unittest.makeSuite(TestTwillDeprecated))
0 from flask import Flask, Response, abort, redirect, jsonify, render_template,\
1 url_for
0 from flask import (
1 Flask,
2 Response,
3 abort,
4 redirect,
5 jsonify,
6 render_template,
7 url_for,
8 flash,
9 request
10 )
211
312
413 def create_app():
514
615 app = Flask(__name__)
16 app.config['SECRET_KEY'] = 'super secret testing key'
717
818 @app.route("/")
919 def index():
1323 def index_with_template():
1424 return render_template("index.html", name="test")
1525
26 @app.route("/flash/")
27 def index_with_flash():
28 flash("Flashed message")
29 return render_template("index.html")
30
31 @app.route("/no_flash/")
32 def index_without_flash():
33 return render_template("index.html")
34
1635 @app.route("/oops/")
1736 def bad_url():
1837 abort(404)
1938
2039 @app.route("/redirect/")
2140 def redirect_to_index():
22 return redirect(url_for("index"))
41 code = request.args.get('code') or 301
42 return redirect(url_for("index"), code=code)
43
44 @app.route("/external_redirect/")
45 def redirect_to_flask_docs():
46 return redirect("http://flask.pocoo.org/")
2347
2448 @app.route("/ajax/")
2549 def ajax():
26 return jsonify(name="test")
50 return jsonify(**dict(name="test"))
2751
2852 @app.route("/forbidden/")
2953 def forbidden():
5555 try:
5656 self.assertStatus(self.client.get('/'), 404, expected_message)
5757 except AssertionError as e:
58 self.assertTrue(expected_message in e.args[0] or \
59 expected_message in e.message)
58 self.assertTrue(expected_message in str(e))
6059
6160 def test_default_status_failure_message(self):
6261 expected_message = 'HTTP Status 404 expected but got 200'
6362 try:
6463 self.assertStatus(self.client.get('/'), 404)
6564 except AssertionError as e:
66 self.assertTrue(expected_message in e.args[0] or \
67 expected_message in e.message)
65 self.assertTrue(expected_message in str(e))
6866
6967 def test_assert_200(self):
7068 self.assert200(self.client.get("/"))
8886 response = self.client.get("/redirect/")
8987 self.assertRedirects(response, "/")
9088
89 def test_assert_redirects_full_url(self):
90 response = self.client.get("/external_redirect/")
91 self.assertRedirects(response, "http://flask.pocoo.org/")
92
93 def test_assert_redirects_failure_message(self):
94 response = self.client.get("/")
95 try:
96 self.assertRedirects(response, "/anything")
97 except AssertionError as e:
98 self.assertTrue("HTTP Status 301, 302, 303, 305, 307 expected but got 200" in str(e))
99
100 def test_assert_redirects_custom_message(self):
101 response = self.client.get("/")
102 try:
103 self.assertRedirects(response, "/anything", "Custom message")
104 except AssertionError as e:
105 self.assertTrue("Custom message" in str(e))
106
107 def test_assert_redirects_valid_status_codes(self):
108 valid_redirect_status_codes = (301, 302, 303, 305, 307)
109
110 for status_code in valid_redirect_status_codes:
111 response = self.client.get("/redirect/?code=" + str(status_code))
112 self.assertRedirects(response, "/")
113 self.assertStatus(response, status_code)
114
115 def test_assert_redirects_invalid_status_code(self):
116 status_code = 200
117 response = self.client.get("/redirect/?code=" + str(status_code))
118 self.assertStatus(response, status_code)
119 try:
120 self.assertRedirects(response, "/")
121 except AssertionError as e:
122 self.assertTrue("HTTP Status 301, 302, 303, 305, 307 expected but got 200" in str(e))
123
91124 def test_assert_template_used(self):
92125 try:
93126 self.client.get("/template/")
96129 pass
97130
98131 def test_assert_template_not_used(self):
99 self.client.get("/")
100 try:
101 self.assert_template_used("index.html")
102 assert False
103 except AssertionError:
104 pass
132 self.client.get("/template/")
133 try:
134 self.assertRaises(AssertionError, self.assert_template_used, "invalid.html")
105135 except RuntimeError:
106136 pass
107137
116146 try:
117147 self.client.get("/template/")
118148 self.assert_context("name", "test")
149 except RuntimeError:
150 pass
151
152 def test_assert_context_custom_message(self):
153 self.client.get("/template/")
154 try:
155 self.assert_context("name", "nothing", "Custom message")
156 except AssertionError as e:
157 self.assertTrue("Custom message" in str(e))
119158 except RuntimeError:
120159 pass
121160
129168 except RuntimeError:
130169 pass
131170
171 def test_assert_bad_context_custom_message(self):
172 self.client.get("/template/")
173 try:
174 self.assert_context("foo", "foo", "Custom message")
175 except AssertionError as e:
176 self.assertTrue("Custom message" in str(e))
177 except RuntimeError:
178 pass
179
132180 def test_assert_get_context_variable_not_exists(self):
133181 try:
134182 self.client.get("/template/")
137185 except RuntimeError:
138186 pass
139187
140
141 class TestLiveServer(LiveServerTestCase):
142
143 def create_app(self):
144 app = create_app()
145 app.config['LIVESERVER_PORT'] = 8943
146 return app
147
148 def test_server_process_is_spawned(self):
149 process = self._process
150
151 # Check the process is spawned
152 self.assertNotEqual(process, None)
153
154 # Check the process is alive
155 self.assertTrue(process.is_alive())
156
157 def test_server_listening(self):
158 response = urlopen(self.get_server_url())
159 self.assertTrue(b'OK' in response.read())
160 self.assertEqual(response.code, 200)
188 def test_assert_flashed_messages_succeed(self):
189 try:
190 self.client.get("/flash/")
191 self.assertMessageFlashed("Flashed message")
192 except RuntimeError:
193 pass
194
195 def test_assert_flashed_messages_failed(self):
196 try:
197 self.client.get("/flash/")
198 self.assertRaises(AssertionError, self.assertMessageFlashed, "Flask-testing has assertMessageFlashed now")
199 except RuntimeError:
200 pass
201
202 def test_assert_no_flashed_messages_fail(self):
203 try:
204 self.client.get("/no_flash/")
205 self.assertRaises(AssertionError, self.assertMessageFlashed, "Flashed message")
206 except RuntimeError:
207 pass
208
209
210 class BaseTestLiveServer(LiveServerTestCase):
211
212 def test_server_process_is_spawned(self):
213 process = self._process
214
215 # Check the process is spawned
216 self.assertNotEqual(process, None)
217
218 # Check the process is alive
219 self.assertTrue(process.is_alive())
220
221 def test_server_listening(self):
222 response = urlopen(self.get_server_url())
223 self.assertTrue(b'OK' in response.read())
224 self.assertEqual(response.code, 200)
225
226
227 class TestLiveServer(BaseTestLiveServer):
228
229 def create_app(self):
230 app = create_app()
231 app.config['LIVESERVER_PORT'] = 8943
232 return app
233
234
235 class TestLiveServerOSPicksPort(BaseTestLiveServer):
236
237 def create_app(self):
238 app = create_app()
239 app.config['LIVESERVER_PORT'] = 0
240 return app
161241
162242
163243 class TestNotRenderTemplates(TestCase):
170250 def test_assert_not_process_the_template(self):
171251 response = self.client.get("/template/")
172252
173 assert "" == response.data
253 assert len(response.data) == 0
174254
175255 def test_assert_template_rendered_signal_sent(self):
176256 self.client.get("/template/")
188268 def test_assert_not_process_the_template(self):
189269 response = self.client.get("/template/")
190270
191 assert "" != response.data
271 assert len(response.data) > 0
192272
193273
194274 class TestRestoreTheRealRender(TestCase):
205285
206286 response = self.client.get("/template/")
207287
208 assert "" != response.data
288 assert len(response.data) > 0