Browse Source

Testsuite is not a package

pull/1165/head
Markus Unterwaditzer 10 years ago
parent
commit
192da325f8
  1. 62
      tests/__init__.py
  2. 7
      tests/conftest.py
  3. 46
      tests/test_helpers.py
  4. 16
      tests/test_instance_config.py

62
tests/__init__.py

@ -1,62 +0,0 @@
# -*- coding: utf-8 -*-
"""
tests
~~~~~~~~~~~~~~~
Tests Flask itself. The majority of Flask is already tested
as part of Werkzeug.
:copyright: (c) 2014 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
from __future__ import print_function
import os
import sys
import warnings
from functools import update_wrapper
from contextlib import contextmanager
from flask._compat import StringIO
@contextmanager
def catch_warnings():
"""Catch warnings in a with block in a list"""
# make sure deprecation warnings are active in tests
warnings.simplefilter('default', category=DeprecationWarning)
filters = warnings.filters
warnings.filters = filters[:]
old_showwarning = warnings.showwarning
log = []
def showwarning(message, category, filename, lineno, file=None, line=None):
log.append(locals())
try:
warnings.showwarning = showwarning
yield log
finally:
warnings.filters = filters
warnings.showwarning = old_showwarning
@contextmanager
def catch_stderr():
"""Catch stderr in a StringIO"""
old_stderr = sys.stderr
sys.stderr = rv = StringIO()
try:
yield rv
finally:
sys.stderr = old_stderr
def emits_module_deprecation_warning(f):
def new_f(self, *args, **kwargs):
with catch_warnings() as log:
f(self, *args, **kwargs)
assert log, 'expected deprecation warning'
for entry in log:
assert 'Modules are deprecated' in str(entry['message'])
return update_wrapper(new_f, f)

7
tests/conftest.py

@ -125,3 +125,10 @@ def purge_module(request):
def inner(name): def inner(name):
request.addfinalizer(lambda: sys.modules.pop(name, None)) request.addfinalizer(lambda: sys.modules.pop(name, None))
return inner return inner
@pytest.fixture
def catch_deprecation_warnings():
import warnings
warnings.simplefilter('default', category=DeprecationWarning)
return lambda: warnings.catch_warnings(record=True)

46
tests/test_helpers.py

@ -13,9 +13,7 @@ import pytest
import os import os
import flask import flask
import unittest
from logging import StreamHandler from logging import StreamHandler
from tests import catch_warnings, catch_stderr
from werkzeug.http import parse_cache_control_header, parse_options_header from werkzeug.http import parse_cache_control_header, parse_options_header
from flask._compat import StringIO, text_type from flask._compat import StringIO, text_type
@ -266,9 +264,9 @@ class TestSendfile(object):
assert rv.mimetype == 'text/html' assert rv.mimetype == 'text/html'
rv.close() rv.close()
def test_send_file_object(self): def test_send_file_object(self, catch_deprecation_warnings):
app = flask.Flask(__name__) app = flask.Flask(__name__)
with catch_warnings() as captured: with catch_deprecation_warnings() as captured:
with app.test_request_context(): with app.test_request_context():
f = open(os.path.join(app.root_path, 'static/index.html'), mode='rb') f = open(os.path.join(app.root_path, 'static/index.html'), mode='rb')
rv = flask.send_file(f) rv = flask.send_file(f)
@ -281,7 +279,7 @@ class TestSendfile(object):
assert len(captured) == 2 assert len(captured) == 2
app.use_x_sendfile = True app.use_x_sendfile = True
with catch_warnings() as captured: with catch_deprecation_warnings() as captured:
with app.test_request_context(): with app.test_request_context():
f = open(os.path.join(app.root_path, 'static/index.html')) f = open(os.path.join(app.root_path, 'static/index.html'))
rv = flask.send_file(f) rv = flask.send_file(f)
@ -295,7 +293,7 @@ class TestSendfile(object):
app.use_x_sendfile = False app.use_x_sendfile = False
with app.test_request_context(): with app.test_request_context():
with catch_warnings() as captured: with catch_deprecation_warnings() as captured:
f = StringIO('Test') f = StringIO('Test')
rv = flask.send_file(f) rv = flask.send_file(f)
rv.direct_passthrough = False rv.direct_passthrough = False
@ -304,7 +302,7 @@ class TestSendfile(object):
rv.close() rv.close()
# etags # etags
assert len(captured) == 1 assert len(captured) == 1
with catch_warnings() as captured: with catch_deprecation_warnings() as captured:
class PyStringIO(object): class PyStringIO(object):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self._io = StringIO(*args, **kwargs) self._io = StringIO(*args, **kwargs)
@ -319,7 +317,7 @@ class TestSendfile(object):
rv.close() rv.close()
# attachment_filename and etags # attachment_filename and etags
assert len(captured) == 3 assert len(captured) == 3
with catch_warnings() as captured: with catch_deprecation_warnings() as captured:
f = StringIO('Test') f = StringIO('Test')
rv = flask.send_file(f, mimetype='text/plain') rv = flask.send_file(f, mimetype='text/plain')
rv.direct_passthrough = False rv.direct_passthrough = False
@ -330,7 +328,7 @@ class TestSendfile(object):
assert len(captured) == 1 assert len(captured) == 1
app.use_x_sendfile = True app.use_x_sendfile = True
with catch_warnings() as captured: with catch_deprecation_warnings() as captured:
with app.test_request_context(): with app.test_request_context():
f = StringIO('Test') f = StringIO('Test')
rv = flask.send_file(f) rv = flask.send_file(f)
@ -339,9 +337,9 @@ class TestSendfile(object):
# etags # etags
assert len(captured) == 1 assert len(captured) == 1
def test_attachment(self): def test_attachment(self, catch_deprecation_warnings):
app = flask.Flask(__name__) app = flask.Flask(__name__)
with catch_warnings() as captured: with catch_deprecation_warnings() as captured:
with app.test_request_context(): with app.test_request_context():
f = open(os.path.join(app.root_path, 'static/index.html')) f = open(os.path.join(app.root_path, 'static/index.html'))
rv = flask.send_file(f, as_attachment=True) rv = flask.send_file(f, as_attachment=True)
@ -433,7 +431,7 @@ class TestLogging(object):
app.logger_name = __name__ + '/test_logger_cache' app.logger_name = __name__ + '/test_logger_cache'
assert app.logger is not logger1 assert app.logger is not logger1
def test_debug_log(self): def test_debug_log(self, capsys):
app = flask.Flask(__name__) app = flask.Flask(__name__)
app.debug = True app.debug = True
@ -448,21 +446,15 @@ class TestLogging(object):
1 // 0 1 // 0
with app.test_client() as c: with app.test_client() as c:
with catch_stderr() as err: c.get('/')
c.get('/') out, err = capsys.readouterr()
out = err.getvalue() assert 'WARNING in test_helpers [' in err
assert 'WARNING in test_helpers [' in out assert os.path.basename(__file__.rsplit('.', 1)[0] + '.py') in err
assert os.path.basename(__file__.rsplit('.', 1)[0] + '.py') in out assert 'the standard library is dead' in err
assert 'the standard library is dead' in out assert 'this is a debug statement' in err
assert 'this is a debug statement' in out
with pytest.raises(ZeroDivisionError):
with catch_stderr() as err: c.get('/exc')
try:
c.get('/exc')
except ZeroDivisionError:
pass
else:
assert False, 'debug log ate the exception'
def test_debug_log_override(self): def test_debug_log_override(self):
app = flask.Flask(__name__) app = flask.Flask(__name__)

16
tests/test_instance_config.py

@ -119,14 +119,16 @@ def test_egg_installed_paths(install_egg, modules_tmpdir,
@pytest.mark.skipif(not PY2, reason='This only works under Python 2.') @pytest.mark.skipif(not PY2, reason='This only works under Python 2.')
def test_meta_path_loader_without_is_package(): def test_meta_path_loader_without_is_package(request, modules_tmpdir):
app = modules_tmpdir.join('unimportable.py')
app.write('import flask\napp = flask.Flask(__name__)')
class Loader(object): class Loader(object):
def find_module(self, name): def find_module(self, name, path=None):
return self return self
sys.meta_path.append(Loader()) sys.meta_path.append(Loader())
try: request.addfinalizer(sys.meta_path.pop)
with pytest.raises(AttributeError):
flask.Flask(__name__) with pytest.raises(AttributeError):
finally: import unimportable
sys.meta_path.pop()

Loading…
Cancel
Save