diff --git a/tests/__init__.py b/tests/__init__.py index 9c349854..30024d10 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -78,9 +78,9 @@ def emits_module_deprecation_warning(f): def new_f(self, *args, **kwargs): with catch_warnings() as log: f(self, *args, **kwargs) - self.assert_true(log, 'expected deprecation warning') + assert log, 'expected deprecation warning' for entry in log: - self.assert_in('Modules are deprecated', str(entry['message'])) + assert 'Modules are deprecated' in str(entry['message']) return update_wrapper(new_f, f) @@ -107,7 +107,7 @@ class TestFlask(object): leaks = [] while flask._request_ctx_stack.top is not None: leaks.append(flask._request_ctx_stack.pop()) - self.assert_equal(leaks, []) + assert leaks == [] def setup_method(self, method): self.setup() @@ -120,31 +120,3 @@ class TestFlask(object): def teardown(self): pass - - def assert_equal(self, x, y): - assert x == y - - def assert_raises(self, exc_type, callable=None, *args, **kwargs): - if callable: - return pytest.raises(exc_type, callable, *args, **kwargs) - else: - return pytest.raises(exc_type) - - def assert_true(self, x, msg=None): - assert x - assert_ = assert_true - - def assert_false(self, x, msg=None): - assert not x - - def assert_in(self, x, y): - assert x in y - - def assert_not_in(self, x, y): - assert x not in y - - def assert_isinstance(self, obj, cls): - assert isinstance(obj, cls) - - def fail(self, msg): - raise AssertionError(msg) diff --git a/tests/test_appctx.py b/tests/test_appctx.py index 2430f577..0c291b87 100644 --- a/tests/test_appctx.py +++ b/tests/test_appctx.py @@ -9,6 +9,8 @@ :license: BSD, see LICENSE for more details. """ +import pytest + import flask import unittest from tests import TestFlask @@ -27,29 +29,29 @@ class TestAppContext(TestFlask): with app.app_context(): rv = flask.url_for('index') - self.assert_equal(rv, 'https://localhost/') + assert rv == 'https://localhost/' def test_url_generation_requires_server_name(self): app = flask.Flask(__name__) with app.app_context(): - with self.assert_raises(RuntimeError): + with pytest.raises(RuntimeError): flask.url_for('index') def test_url_generation_without_context_fails(self): - with self.assert_raises(RuntimeError): + with pytest.raises(RuntimeError): flask.url_for('index') def test_request_context_means_app_context(self): app = flask.Flask(__name__) with app.test_request_context(): - self.assert_equal(flask.current_app._get_current_object(), app) - self.assert_equal(flask._app_ctx_stack.top, None) + assert flask.current_app._get_current_object() == app + assert flask._app_ctx_stack.top == None def test_app_context_provides_current_app(self): app = flask.Flask(__name__) with app.app_context(): - self.assert_equal(flask.current_app._get_current_object(), app) - self.assert_equal(flask._app_ctx_stack.top, None) + assert flask.current_app._get_current_object() == app + assert flask._app_ctx_stack.top == None def test_app_tearing_down(self): cleanup_stuff = [] @@ -61,7 +63,7 @@ class TestAppContext(TestFlask): with app.app_context(): pass - self.assert_equal(cleanup_stuff, [None]) + assert cleanup_stuff == [None] def test_app_tearing_down_with_previous_exception(self): cleanup_stuff = [] @@ -78,7 +80,7 @@ class TestAppContext(TestFlask): with app.app_context(): pass - self.assert_equal(cleanup_stuff, [None]) + assert cleanup_stuff == [None] def test_custom_app_ctx_globals_class(self): class CustomRequestGlobals(object): @@ -87,8 +89,7 @@ class TestAppContext(TestFlask): app = flask.Flask(__name__) app.app_ctx_globals_class = CustomRequestGlobals with app.app_context(): - self.assert_equal( - flask.render_template_string('{{ g.spam }}'), 'eggs') + assert flask.render_template_string('{{ g.spam }}') == 'eggs' def test_context_refcounts(self): called = [] @@ -104,12 +105,14 @@ class TestAppContext(TestFlask): with flask._app_ctx_stack.top: with flask._request_ctx_stack.top: pass - self.assert_true(flask._request_ctx_stack.top.request.environ - ['werkzeug.request'] is not None) + env = flask._request_ctx_stack.top.request.environ + assert env['werkzeug.request'] is not None return u'' c = app.test_client() - c.get('/') - self.assert_equal(called, ['request', 'app']) + res = c.get('/') + assert res.status_code == 200 + assert res.data == u'' + assert called == ['request', 'app'] def suite(): diff --git a/tests/test_basic.py b/tests/test_basic.py index c06907e6..eff5419d 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -9,6 +9,8 @@ :license: BSD, see LICENSE for more details. """ +import pytest + import re import uuid import time @@ -32,8 +34,8 @@ class TestBasicFunctionality(TestFlask): def index(): return 'Hello World' rv = app.test_client().open('/', method='OPTIONS') - self.assert_equal(sorted(rv.allow), ['GET', 'HEAD', 'OPTIONS', 'POST']) - self.assert_equal(rv.data, b'') + assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS', 'POST'] + assert rv.data == b'' def test_options_on_multiple_rules(self): app = flask.Flask(__name__) @@ -44,7 +46,7 @@ class TestBasicFunctionality(TestFlask): def index_put(): return 'Aha!' rv = app.test_client().open('/', method='OPTIONS') - self.assert_equal(sorted(rv.allow), ['GET', 'HEAD', 'OPTIONS', 'POST', 'PUT']) + assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS', 'POST', 'PUT'] def test_options_handling_disabled(self): app = flask.Flask(__name__) @@ -53,7 +55,7 @@ class TestBasicFunctionality(TestFlask): index.provide_automatic_options = False app.route('/')(index) rv = app.test_client().open('/', method='OPTIONS') - self.assert_equal(rv.status_code, 405) + assert rv.status_code == 405 app = flask.Flask(__name__) def index2(): @@ -61,7 +63,7 @@ class TestBasicFunctionality(TestFlask): index2.provide_automatic_options = True app.route('/', methods=['OPTIONS'])(index2) rv = app.test_client().open('/', method='OPTIONS') - self.assert_equal(sorted(rv.allow), ['OPTIONS']) + assert sorted(rv.allow) == ['OPTIONS'] def test_request_dispatching(self): app = flask.Flask(__name__) @@ -73,22 +75,22 @@ class TestBasicFunctionality(TestFlask): return flask.request.method c = app.test_client() - self.assert_equal(c.get('/').data, b'GET') + assert c.get('/').data == b'GET' rv = c.post('/') - self.assert_equal(rv.status_code, 405) - self.assert_equal(sorted(rv.allow), ['GET', 'HEAD', 'OPTIONS']) + assert rv.status_code == 405 + assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS'] rv = c.head('/') - self.assert_equal(rv.status_code, 200) - self.assert_false(rv.data) # head truncates - self.assert_equal(c.post('/more').data, b'POST') - self.assert_equal(c.get('/more').data, b'GET') + assert rv.status_code == 200 + assert not rv.data # head truncates + assert c.post('/more').data == b'POST' + assert c.get('/more').data == b'GET' rv = c.delete('/more') - self.assert_equal(rv.status_code, 405) - self.assert_equal(sorted(rv.allow), ['GET', 'HEAD', 'OPTIONS', 'POST']) + assert rv.status_code == 405 + assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS', 'POST'] def test_disallow_string_for_allowed_methods(self): app = flask.Flask(__name__) - with self.assert_raises(TypeError): + with pytest.raises(TypeError): @app.route('/', methods='GET POST') def index(): return "Hey" @@ -104,18 +106,18 @@ class TestBasicFunctionality(TestFlask): app.add_url_rule('/more', 'more', more, methods=['GET', 'POST']) c = app.test_client() - self.assert_equal(c.get('/').data, b'GET') + assert c.get('/').data == b'GET' rv = c.post('/') - self.assert_equal(rv.status_code, 405) - self.assert_equal(sorted(rv.allow), ['GET', 'HEAD', 'OPTIONS']) + assert rv.status_code == 405 + assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS'] rv = c.head('/') - self.assert_equal(rv.status_code, 200) - self.assert_false(rv.data) # head truncates - self.assert_equal(c.post('/more').data, b'POST') - self.assert_equal(c.get('/more').data, b'GET') + assert rv.status_code == 200 + assert not rv.data # head truncates + assert c.post('/more').data == b'POST' + assert c.get('/more').data == b'GET' rv = c.delete('/more') - self.assert_equal(rv.status_code, 405) - self.assert_equal(sorted(rv.allow), ['GET', 'HEAD', 'OPTIONS', 'POST']) + assert rv.status_code == 405 + assert sorted(rv.allow) == ['GET', 'HEAD', 'OPTIONS', 'POST'] def test_werkzeug_routing(self): from werkzeug.routing import Submount, Rule @@ -132,8 +134,8 @@ class TestBasicFunctionality(TestFlask): app.view_functions['index'] = index c = app.test_client() - self.assert_equal(c.get('/foo/').data, b'index') - self.assert_equal(c.get('/foo/bar').data, b'bar') + assert c.get('/foo/').data == b'index' + assert c.get('/foo/bar').data == b'bar' def test_endpoint_decorator(self): from werkzeug.routing import Submount, Rule @@ -152,8 +154,8 @@ class TestBasicFunctionality(TestFlask): return 'index' c = app.test_client() - self.assert_equal(c.get('/foo/').data, b'index') - self.assert_equal(c.get('/foo/bar').data, b'bar') + assert c.get('/foo/').data == b'index' + assert c.get('/foo/bar').data == b'bar' def test_session(self): app = flask.Flask(__name__) @@ -167,8 +169,8 @@ class TestBasicFunctionality(TestFlask): return flask.session['value'] c = app.test_client() - self.assert_equal(c.post('/set', data={'value': '42'}).data, b'value set') - self.assert_equal(c.get('/get').data, b'42') + assert c.post('/set', data={'value': '42'}).data == b'value set' + assert c.get('/get').data == b'42' def test_session_using_server_name(self): app = flask.Flask(__name__) @@ -181,8 +183,8 @@ class TestBasicFunctionality(TestFlask): flask.session['testing'] = 42 return 'Hello World' rv = app.test_client().get('/', 'http://example.com/') - self.assert_in('domain=.example.com', rv.headers['set-cookie'].lower()) - self.assert_in('httponly', rv.headers['set-cookie'].lower()) + assert 'domain=.example.com' in rv.headers['set-cookie'].lower() + assert 'httponly' in rv.headers['set-cookie'].lower() def test_session_using_server_name_and_port(self): app = flask.Flask(__name__) @@ -195,8 +197,8 @@ class TestBasicFunctionality(TestFlask): flask.session['testing'] = 42 return 'Hello World' rv = app.test_client().get('/', 'http://example.com:8080/') - self.assert_in('domain=.example.com', rv.headers['set-cookie'].lower()) - self.assert_in('httponly', rv.headers['set-cookie'].lower()) + assert 'domain=.example.com' in rv.headers['set-cookie'].lower() + assert 'httponly' in rv.headers['set-cookie'].lower() def test_session_using_server_name_port_and_path(self): app = flask.Flask(__name__) @@ -210,9 +212,9 @@ class TestBasicFunctionality(TestFlask): flask.session['testing'] = 42 return 'Hello World' rv = app.test_client().get('/', 'http://example.com:8080/foo') - self.assert_in('domain=example.com', rv.headers['set-cookie'].lower()) - self.assert_in('path=/foo', rv.headers['set-cookie'].lower()) - self.assert_in('httponly', rv.headers['set-cookie'].lower()) + assert 'domain=example.com' in rv.headers['set-cookie'].lower() + assert 'path=/foo' in rv.headers['set-cookie'].lower() + assert 'httponly' in rv.headers['set-cookie'].lower() def test_session_using_application_root(self): class PrefixPathMiddleware(object): @@ -234,7 +236,7 @@ class TestBasicFunctionality(TestFlask): flask.session['testing'] = 42 return 'Hello World' rv = app.test_client().get('/', 'http://example.com:8080/') - self.assert_in('path=/bar', rv.headers['set-cookie'].lower()) + assert 'path=/bar' in rv.headers['set-cookie'].lower() def test_session_using_session_settings(self): app = flask.Flask(__name__) @@ -253,10 +255,10 @@ class TestBasicFunctionality(TestFlask): return 'Hello World' rv = app.test_client().get('/', 'http://www.example.com:8080/test/') cookie = rv.headers['set-cookie'].lower() - self.assert_in('domain=.example.com', cookie) - self.assert_in('path=/', cookie) - self.assert_in('secure', cookie) - self.assert_not_in('httponly', cookie) + assert 'domain=.example.com' in cookie + assert 'path=/' in cookie + assert 'secure' in cookie + assert 'httponly' not in cookie def test_missing_session(self): app = flask.Flask(__name__) @@ -264,11 +266,11 @@ class TestBasicFunctionality(TestFlask): try: f(*args, **kwargs) except RuntimeError as e: - self.assert_true(e.args and 'session is unavailable' in e.args[0]) + assert e.args and 'session is unavailable' in e.args[0] else: - self.assert_true(False, 'expected exception') + assert False, 'expected exception' with app.test_request_context(): - self.assert_true(flask.session.get('missing_key') is None) + assert flask.session.get('missing_key') is None expect_exception(flask.session.__setitem__, 'foo', 42) expect_exception(flask.session.pop, 'foo') @@ -288,22 +290,22 @@ class TestBasicFunctionality(TestFlask): client = app.test_client() rv = client.get('/') - self.assert_in('set-cookie', rv.headers) + assert 'set-cookie' in rv.headers match = re.search(r'\bexpires=([^;]+)(?i)', rv.headers['set-cookie']) expires = parse_date(match.group()) expected = datetime.utcnow() + app.permanent_session_lifetime - self.assert_equal(expires.year, expected.year) - self.assert_equal(expires.month, expected.month) - self.assert_equal(expires.day, expected.day) + assert expires.year == expected.year + assert expires.month == expected.month + assert expires.day == expected.day rv = client.get('/test') - self.assert_equal(rv.data, b'True') + assert rv.data == b'True' permanent = False rv = app.test_client().get('/') - self.assert_in('set-cookie', rv.headers) + assert 'set-cookie' in rv.headers match = re.search(r'\bexpires=([^;]+)', rv.headers['set-cookie']) - self.assert_true(match is None) + assert match is None def test_session_stored_last(self): app = flask.Flask(__name__) @@ -319,8 +321,8 @@ class TestBasicFunctionality(TestFlask): return repr(flask.session.get('foo')) c = app.test_client() - self.assert_equal(c.get('/').data, b'None') - self.assert_equal(c.get('/').data, b'42') + assert c.get('/').data == b'None' + assert c.get('/').data == b'42' def test_session_special_types(self): app = flask.Flask(__name__) @@ -345,13 +347,13 @@ class TestBasicFunctionality(TestFlask): c = app.test_client() c.get('/') rv = pickle.loads(c.get('/').data) - self.assert_equal(rv['m'], flask.Markup('Hello!')) - self.assert_equal(type(rv['m']), flask.Markup) - self.assert_equal(rv['dt'], now) - self.assert_equal(rv['u'], the_uuid) - self.assert_equal(rv['b'], b'\xff') - self.assert_equal(type(rv['b']), bytes) - self.assert_equal(rv['t'], (1, 2, 3)) + assert rv['m'] == flask.Markup('Hello!') + assert type(rv['m']) == flask.Markup + assert rv['dt'] == now + assert rv['u'] == the_uuid + assert rv['b'] == b'\xff' + assert type(rv['b']) == bytes + assert rv['t'] == (1, 2, 3) def test_session_cookie_setting(self): app = flask.Flask(__name__) @@ -371,14 +373,14 @@ class TestBasicFunctionality(TestFlask): def run_test(expect_header): with app.test_client() as c: - self.assert_equal(c.get('/bump').data, b'1') - self.assert_equal(c.get('/bump').data, b'2') - self.assert_equal(c.get('/bump').data, b'3') + assert c.get('/bump').data == b'1' + assert c.get('/bump').data == b'2' + assert c.get('/bump').data == b'3' rv = c.get('/read') set_cookie = rv.headers.get('set-cookie') - self.assert_equal(set_cookie is not None, expect_header) - self.assert_equal(rv.data, b'3') + assert (set_cookie is not None) == expect_header + assert rv.data == b'3' is_permanent = True app.config['SESSION_REFRESH_EACH_REQUEST'] = True @@ -401,12 +403,12 @@ class TestBasicFunctionality(TestFlask): app.secret_key = 'testkey' with app.test_request_context(): - self.assert_false(flask.session.modified) + assert not flask.session.modified flask.flash('Zap') flask.session.modified = False flask.flash('Zip') - self.assert_true(flask.session.modified) - self.assert_equal(list(flask.get_flashed_messages()), ['Zap', 'Zip']) + assert flask.session.modified + assert list(flask.get_flashed_messages()) == ['Zap', 'Zip'] def test_extended_flashing(self): # Be sure app.testing=True below, else tests can fail silently. @@ -429,42 +431,45 @@ class TestBasicFunctionality(TestFlask): @app.route('/test/') def test(): messages = flask.get_flashed_messages() - self.assert_equal(len(messages), 3) - self.assert_equal(messages[0], u'Hello World') - self.assert_equal(messages[1], u'Hello World') - self.assert_equal(messages[2], flask.Markup(u'Testing')) + assert list(messages) == [ + u'Hello World', + u'Hello World', + flask.Markup(u'Testing') + ] return '' @app.route('/test_with_categories/') def test_with_categories(): messages = flask.get_flashed_messages(with_categories=True) - self.assert_equal(len(messages), 3) - self.assert_equal(messages[0], ('message', u'Hello World')) - self.assert_equal(messages[1], ('error', u'Hello World')) - self.assert_equal(messages[2], ('warning', flask.Markup(u'Testing'))) + assert len(messages) == 3 + assert list(messages) == [ + ('message', u'Hello World'), + ('error', u'Hello World'), + ('warning', flask.Markup(u'Testing')) + ] return '' @app.route('/test_filter/') def test_filter(): messages = flask.get_flashed_messages(category_filter=['message'], with_categories=True) - self.assert_equal(len(messages), 1) - self.assert_equal(messages[0], ('message', u'Hello World')) + assert list(messages) == [('message', u'Hello World')] return '' @app.route('/test_filters/') def test_filters(): messages = flask.get_flashed_messages(category_filter=['message', 'warning'], with_categories=True) - self.assert_equal(len(messages), 2) - self.assert_equal(messages[0], ('message', u'Hello World')) - self.assert_equal(messages[1], ('warning', flask.Markup(u'Testing'))) + assert list(messages) == [ + ('message', u'Hello World'), + ('warning', flask.Markup(u'Testing')) + ] return '' @app.route('/test_filters_without_returning_categories/') def test_filters2(): messages = flask.get_flashed_messages(category_filter=['message', 'warning']) - self.assert_equal(len(messages), 2) - self.assert_equal(messages[0], u'Hello World') - self.assert_equal(messages[1], flask.Markup(u'Testing')) + assert len(messages) == 2 + assert messages[0] == u'Hello World' + assert messages[1] == flask.Markup(u'Testing') return '' # Create new test client on each test to clean flashed messages. @@ -502,13 +507,13 @@ class TestBasicFunctionality(TestFlask): return response @app.route('/') def index(): - self.assert_in('before', evts) - self.assert_not_in('after', evts) + assert 'before' in evts + assert 'after' not in evts return 'request' - self.assert_not_in('after', evts) + assert 'after' not in evts rv = app.test_client().get('/').data - self.assert_in('after', evts) - self.assert_equal(rv, b'request|after') + assert 'after' in evts + assert rv == b'request|after' def test_after_request_processing(self): app = flask.Flask(__name__) @@ -522,8 +527,8 @@ class TestBasicFunctionality(TestFlask): return 'Test' c = app.test_client() resp = c.get('/') - self.assert_equal(resp.status_code, 200) - self.assert_equal(resp.headers['X-Foo'], 'a header') + assert resp.status_code == 200 + assert resp.headers['X-Foo'] == 'a header' def test_teardown_request_handler(self): called = [] @@ -536,9 +541,9 @@ class TestBasicFunctionality(TestFlask): def root(): return "Response" rv = app.test_client().get('/') - self.assert_equal(rv.status_code, 200) - self.assert_in(b'Response', rv.data) - self.assert_equal(len(called), 1) + assert rv.status_code == 200 + assert b'Response' in rv.data + assert len(called) == 1 def test_teardown_request_handler_debug_mode(self): called = [] @@ -552,9 +557,9 @@ class TestBasicFunctionality(TestFlask): def root(): return "Response" rv = app.test_client().get('/') - self.assert_equal(rv.status_code, 200) - self.assert_in(b'Response', rv.data) - self.assert_equal(len(called), 1) + assert rv.status_code == 200 + assert b'Response' in rv.data + assert len(called) == 1 def test_teardown_request_handler_error(self): called = [] @@ -562,7 +567,7 @@ class TestBasicFunctionality(TestFlask): app.config['LOGGER_HANDLER_POLICY'] = 'never' @app.teardown_request def teardown_request1(exc): - self.assert_equal(type(exc), ZeroDivisionError) + assert type(exc) == ZeroDivisionError called.append(True) # This raises a new error and blows away sys.exc_info(), so we can # test that all teardown_requests get passed the same original @@ -573,7 +578,7 @@ class TestBasicFunctionality(TestFlask): pass @app.teardown_request def teardown_request2(exc): - self.assert_equal(type(exc), ZeroDivisionError) + assert type(exc) == ZeroDivisionError called.append(True) # This raises a new error and blows away sys.exc_info(), so we can # test that all teardown_requests get passed the same original @@ -586,9 +591,9 @@ class TestBasicFunctionality(TestFlask): def fails(): 1 // 0 rv = app.test_client().get('/') - self.assert_equal(rv.status_code, 500) - self.assert_in(b'Internal Server Error', rv.data) - self.assert_equal(len(called), 2) + assert rv.status_code == 500 + assert b'Internal Server Error' in rv.data + assert len(called) == 2 def test_before_after_request_order(self): called = [] @@ -617,8 +622,8 @@ class TestBasicFunctionality(TestFlask): def index(): return '42' rv = app.test_client().get('/') - self.assert_equal(rv.data, b'42') - self.assert_equal(called, [1, 2, 3, 4, 5, 6]) + assert rv.data == b'42' + assert called == [1, 2, 3, 4, 5, 6] def test_error_handling(self): app = flask.Flask(__name__) @@ -643,14 +648,14 @@ class TestBasicFunctionality(TestFlask): flask.abort(403) c = app.test_client() rv = c.get('/') - self.assert_equal(rv.status_code, 404) - self.assert_equal(rv.data, b'not found') + assert rv.status_code == 404 + assert rv.data == b'not found' rv = c.get('/error') - self.assert_equal(rv.status_code, 500) - self.assert_equal(b'internal server error', rv.data) + assert rv.status_code == 500 + assert b'internal server error' == rv.data rv = c.get('/forbidden') - self.assert_equal(rv.status_code, 403) - self.assert_equal(b'forbidden', rv.data) + assert rv.status_code == 403 + assert b'forbidden' == rv.data def test_before_request_and_routing_errors(self): app = flask.Flask(__name__) @@ -661,8 +666,8 @@ class TestBasicFunctionality(TestFlask): def return_something(error): return flask.g.something, 404 rv = app.test_client().get('/') - self.assert_equal(rv.status_code, 404) - self.assert_equal(rv.data, b'value') + assert rv.status_code == 404 + assert rv.data == b'value' def test_user_error_handling(self): class MyException(Exception): @@ -671,14 +676,14 @@ class TestBasicFunctionality(TestFlask): app = flask.Flask(__name__) @app.errorhandler(MyException) def handle_my_exception(e): - self.assert_true(isinstance(e, MyException)) + assert isinstance(e, MyException) return '42' @app.route('/') def index(): raise MyException() c = app.test_client() - self.assert_equal(c.get('/').data, b'42') + assert c.get('/').data == b'42' def test_http_error_subclass_handling(self): class ForbiddenSubclass(Forbidden): @@ -687,12 +692,12 @@ class TestBasicFunctionality(TestFlask): app = flask.Flask(__name__) @app.errorhandler(ForbiddenSubclass) def handle_forbidden_subclass(e): - self.assert_true(isinstance(e, ForbiddenSubclass)) + assert isinstance(e, ForbiddenSubclass) return 'banana' @app.errorhandler(403) def handle_forbidden_subclass(e): - self.assert_false(isinstance(e, ForbiddenSubclass)) - self.assert_true(isinstance(e, Forbidden)) + assert not isinstance(e, ForbiddenSubclass) + assert isinstance(e, Forbidden) return 'apple' @app.route('/1') @@ -706,9 +711,9 @@ class TestBasicFunctionality(TestFlask): raise Forbidden() c = app.test_client() - self.assert_equal(c.get('/1').data, b'banana') - self.assert_equal(c.get('/2').data, b'apple') - self.assert_equal(c.get('/3').data, b'apple') + assert c.get('/1').data == b'banana' + assert c.get('/2').data == b'apple' + assert c.get('/3').data == b'apple' def test_trapping_of_bad_request_key_errors(self): app = flask.Flask(__name__) @@ -717,16 +722,16 @@ class TestBasicFunctionality(TestFlask): def fail(): flask.request.form['missing_key'] c = app.test_client() - self.assert_equal(c.get('/fail').status_code, 400) + assert c.get('/fail').status_code == 400 app.config['TRAP_BAD_REQUEST_ERRORS'] = True c = app.test_client() try: c.get('/fail') except KeyError as e: - self.assert_true(isinstance(e, BadRequest)) + assert isinstance(e, BadRequest) else: - self.fail('Expected exception') + assert False, 'Expected exception' def test_trapping_of_all_http_exceptions(self): app = flask.Flask(__name__) @@ -742,7 +747,7 @@ class TestBasicFunctionality(TestFlask): except NotFound as e: pass else: - self.fail('Expected exception') + assert False, 'Expected exception' def test_enctype_debug_helper(self): from flask.debughelpers import DebugFilesKeyError @@ -759,10 +764,10 @@ class TestBasicFunctionality(TestFlask): try: c.post('/fail', data={'foo': 'index.txt'}) except DebugFilesKeyError as e: - self.assert_in('no file contents were transmitted', str(e)) - self.assert_in('This was submitted: "index.txt"', str(e)) + assert 'no file contents were transmitted' in str(e) + assert 'This was submitted: "index.txt"' in str(e) else: - self.fail('Expected exception') + assert False, 'Expected exception' def test_response_creation(self): app = flask.Flask(__name__) @@ -795,67 +800,67 @@ class TestBasicFunctionality(TestFlask): } c = app.test_client() - self.assert_equal(c.get('/unicode').data, u'Hällo Wörld'.encode('utf-8')) - self.assert_equal(c.get('/string').data, u'Hällo Wörld'.encode('utf-8')) + assert c.get('/unicode').data == u'Hällo Wörld'.encode('utf-8') + assert c.get('/string').data == u'Hällo Wörld'.encode('utf-8') rv = c.get('/args') - self.assert_equal(rv.data, b'Meh') - self.assert_equal(rv.headers['X-Foo'], 'Testing') - self.assert_equal(rv.status_code, 400) - self.assert_equal(rv.mimetype, 'text/plain') + assert rv.data == b'Meh' + assert rv.headers['X-Foo'] == 'Testing' + assert rv.status_code == 400 + assert rv.mimetype == 'text/plain' rv2 = c.get('/two_args') - self.assert_equal(rv2.data, b'Hello') - self.assert_equal(rv2.headers['X-Foo'], 'Test') - self.assert_equal(rv2.status_code, 200) - self.assert_equal(rv2.mimetype, 'text/plain') + assert rv2.data == b'Hello' + assert rv2.headers['X-Foo'] == 'Test' + assert rv2.status_code == 200 + assert rv2.mimetype == 'text/plain' rv3 = c.get('/args_status') - self.assert_equal(rv3.data, b'Hi, status!') - self.assert_equal(rv3.status_code, 400) - self.assert_equal(rv3.mimetype, 'text/html') + assert rv3.data == b'Hi, status!' + assert rv3.status_code == 400 + assert rv3.mimetype == 'text/html' rv4 = c.get('/args_header') - self.assert_equal(rv4.data, b'Hello world') - self.assert_equal(rv4.headers['X-Foo'], 'Bar') - self.assert_equal(rv4.headers['X-Bar'], 'Foo') - self.assert_equal(rv4.status_code, 404) + assert rv4.data == b'Hello world' + assert rv4.headers['X-Foo'] == 'Bar' + assert rv4.headers['X-Bar'] == 'Foo' + assert rv4.status_code == 404 def test_make_response(self): app = flask.Flask(__name__) with app.test_request_context(): rv = flask.make_response() - self.assert_equal(rv.status_code, 200) - self.assert_equal(rv.data, b'') - self.assert_equal(rv.mimetype, 'text/html') + assert rv.status_code == 200 + assert rv.data == b'' + assert rv.mimetype == 'text/html' rv = flask.make_response('Awesome') - self.assert_equal(rv.status_code, 200) - self.assert_equal(rv.data, b'Awesome') - self.assert_equal(rv.mimetype, 'text/html') + assert rv.status_code == 200 + assert rv.data == b'Awesome' + assert rv.mimetype == 'text/html' rv = flask.make_response('W00t', 404) - self.assert_equal(rv.status_code, 404) - self.assert_equal(rv.data, b'W00t') - self.assert_equal(rv.mimetype, 'text/html') + assert rv.status_code == 404 + assert rv.data == b'W00t' + assert rv.mimetype == 'text/html' def test_make_response_with_response_instance(self): app = flask.Flask(__name__) with app.test_request_context(): rv = flask.make_response( flask.jsonify({'msg': 'W00t'}), 400) - self.assert_equal(rv.status_code, 400) - self.assert_equal(rv.data, b'{\n "msg": "W00t"\n}') - self.assert_equal(rv.mimetype, 'application/json') + assert rv.status_code == 400 + assert rv.data == b'{\n "msg": "W00t"\n}' + assert rv.mimetype == 'application/json' rv = flask.make_response( flask.Response(''), 400) - self.assert_equal(rv.status_code, 400) - self.assert_equal(rv.data, b'') - self.assert_equal(rv.mimetype, 'text/html') + assert rv.status_code == 400 + assert rv.data == b'' + assert rv.mimetype == 'text/html' rv = flask.make_response( flask.Response('', headers={'Content-Type': 'text/html'}), 400, [('X-Foo', 'bar')]) - self.assert_equal(rv.status_code, 400) - self.assert_equal(rv.headers['Content-Type'], 'text/html') - self.assert_equal(rv.headers['X-Foo'], 'bar') + assert rv.status_code == 400 + assert rv.headers['Content-Type'] == 'text/html' + assert rv.headers['X-Foo'] == 'bar' def test_url_generation(self): app = flask.Flask(__name__) @@ -863,16 +868,16 @@ class TestBasicFunctionality(TestFlask): def hello(): pass with app.test_request_context(): - self.assert_equal(flask.url_for('hello', name='test x'), '/hello/test%20x') - self.assert_equal(flask.url_for('hello', name='test x', _external=True), - 'http://localhost/hello/test%20x') + assert flask.url_for('hello', name='test x') == '/hello/test%20x' + assert flask.url_for('hello', name='test x', _external=True) == \ + 'http://localhost/hello/test%20x' def test_build_error_handler(self): app = flask.Flask(__name__) # Test base case, a URL which results in a BuildError. with app.test_request_context(): - self.assert_raises(BuildError, flask.url_for, 'spam') + pytest.raises(BuildError, flask.url_for, 'spam') # Verify the error is re-raised if not the current exception. try: @@ -883,7 +888,7 @@ class TestBasicFunctionality(TestFlask): try: raise RuntimeError('Test case where BuildError is not current.') except RuntimeError: - self.assert_raises(BuildError, app.handle_url_build_error, error, 'spam', {}) + pytest.raises(BuildError, app.handle_url_build_error, error, 'spam', {}) # Test a custom handler. def handler(error, endpoint, values): @@ -891,7 +896,7 @@ class TestBasicFunctionality(TestFlask): return '/test_handler/' app.url_build_error_handlers.append(handler) with app.test_request_context(): - self.assert_equal(flask.url_for('spam'), '/test_handler/') + assert flask.url_for('spam') == '/test_handler/' def test_custom_converters(self): from werkzeug.routing import BaseConverter @@ -907,17 +912,17 @@ class TestBasicFunctionality(TestFlask): def index(args): return '|'.join(args) c = app.test_client() - self.assert_equal(c.get('/1,2,3').data, b'1|2|3') + assert c.get('/1,2,3').data == b'1|2|3' def test_static_files(self): app = flask.Flask(__name__) app.testing = True rv = app.test_client().get('/static/index.html') - self.assert_equal(rv.status_code, 200) - self.assert_equal(rv.data.strip(), b'

Hello World!

') + assert rv.status_code == 200 + assert rv.data.strip() == b'

Hello World!

' with app.test_request_context(): - self.assert_equal(flask.url_for('static', filename='index.html'), - '/static/index.html') + assert flask.url_for('static', filename='index.html') == \ + '/static/index.html' rv.close() def test_none_response(self): @@ -929,14 +934,14 @@ class TestBasicFunctionality(TestFlask): try: app.test_client().get('/') except ValueError as e: - self.assert_equal(str(e), 'View function did not return a response') + assert str(e) == 'View function did not return a response' pass else: - self.assert_true("Expected ValueError") + assert "Expected ValueError" def test_request_locals(self): - self.assert_equal(repr(flask.g), '') - self.assert_false(flask.g) + assert repr(flask.g) == '' + assert not flask.g def test_test_app_proper_environ(self): app = flask.Flask(__name__) @@ -952,42 +957,46 @@ class TestBasicFunctionality(TestFlask): return 'Foo SubDomain' rv = app.test_client().get('/') - self.assert_equal(rv.data, b'Foo') + assert rv.data == b'Foo' rv = app.test_client().get('/', 'http://localhost.localdomain:5000') - self.assert_equal(rv.data, b'Foo') + assert rv.data == b'Foo' rv = app.test_client().get('/', 'https://localhost.localdomain:5000') - self.assert_equal(rv.data, b'Foo') + assert rv.data == b'Foo' app.config.update(SERVER_NAME='localhost.localdomain') rv = app.test_client().get('/', 'https://localhost.localdomain') - self.assert_equal(rv.data, b'Foo') + assert rv.data == b'Foo' try: app.config.update(SERVER_NAME='localhost.localdomain:443') rv = app.test_client().get('/', 'https://localhost.localdomain') # Werkzeug 0.8 - self.assert_equal(rv.status_code, 404) + assert rv.status_code == 404 except ValueError as e: # Werkzeug 0.7 - self.assert_equal(str(e), "the server name provided " + - "('localhost.localdomain:443') does not match the " + \ - "server name from the WSGI environment ('localhost.localdomain')") + assert str(e) == ( + "the server name provided " + "('localhost.localdomain:443') does not match the " + "server name from the WSGI environment ('localhost.localdomain')" + ) try: app.config.update(SERVER_NAME='localhost.localdomain') rv = app.test_client().get('/', 'http://foo.localhost') # Werkzeug 0.8 - self.assert_equal(rv.status_code, 404) + assert rv.status_code == 404 except ValueError as e: # Werkzeug 0.7 - self.assert_equal(str(e), "the server name provided " + \ - "('localhost.localdomain') does not match the " + \ - "server name from the WSGI environment ('foo.localhost')") + assert str(e) == ( + "the server name provided " + "('localhost.localdomain') does not match the " + "server name from the WSGI environment ('foo.localhost')" + ) rv = app.test_client().get('/', 'http://foo.localhost.localdomain') - self.assert_equal(rv.data, b'Foo SubDomain') + assert rv.data == b'Foo SubDomain' def test_exception_propagation(self): def apprunner(configkey): @@ -1004,9 +1013,9 @@ class TestBasicFunctionality(TestFlask): except Exception: pass else: - self.fail('expected exception') + assert False, 'expected exception' else: - self.assert_equal(c.get('/').status_code, 500) + assert c.get('/').status_code == 500 # we have to run this test in an isolated thread because if the # debug flag is set to true and an exception happens the context is @@ -1023,18 +1032,18 @@ class TestBasicFunctionality(TestFlask): @app.before_request def always_first(): flask.request.form['myfile'] - self.assert_true(False) + assert False @app.route('/accept', methods=['POST']) def accept_file(): flask.request.form['myfile'] - self.assert_true(False) + assert False @app.errorhandler(413) def catcher(error): return '42' c = app.test_client() rv = c.post('/accept', data={'myfile': 'foo' * 100}) - self.assert_equal(rv.data, b'42') + assert rv.data == b'42' def test_url_processors(self): app = flask.Flask(__name__) @@ -1063,9 +1072,9 @@ class TestBasicFunctionality(TestFlask): c = app.test_client() - self.assert_equal(c.get('/de/').data, b'/de/about') - self.assert_equal(c.get('/de/about').data, b'/foo') - self.assert_equal(c.get('/foo').data, b'/en/about') + assert c.get('/de/').data == b'/de/about' + assert c.get('/de/about').data == b'/foo' + assert c.get('/foo').data == b'/en/about' def test_inject_blueprint_url_defaults(self): app = flask.Flask(__name__) @@ -1083,12 +1092,12 @@ class TestBasicFunctionality(TestFlask): values = dict() app.inject_url_defaults('foo.bar.baz.view', values) expected = dict(page='login') - self.assert_equal(values, expected) + assert values == expected with app.test_request_context('/somepage'): url = flask.url_for('foo.bar.baz.view') expected = '/login' - self.assert_equal(url, expected) + assert url == expected def test_nonascii_pathinfo(self): app = flask.Flask(__name__) @@ -1100,7 +1109,7 @@ class TestBasicFunctionality(TestFlask): c = app.test_client() rv = c.get(u'/киртест') - self.assert_equal(rv.data, b'Hello World!') + assert rv.data == b'Hello World!' def test_debug_mode_complains_after_first_request(self): app = flask.Flask(__name__) @@ -1108,23 +1117,23 @@ class TestBasicFunctionality(TestFlask): @app.route('/') def index(): return 'Awesome' - self.assert_false(app.got_first_request) - self.assert_equal(app.test_client().get('/').data, b'Awesome') + assert not app.got_first_request + assert app.test_client().get('/').data == b'Awesome' try: @app.route('/foo') def broken(): return 'Meh' except AssertionError as e: - self.assert_in('A setup function was called', str(e)) + assert 'A setup function was called' in str(e) else: - self.fail('Expected exception') + assert False, 'Expected exception' app.debug = False @app.route('/foo') def working(): return 'Meh' - self.assert_equal(app.test_client().get('/foo').data, b'Meh') - self.assert_true(app.got_first_request) + assert app.test_client().get('/foo').data == b'Meh' + assert app.got_first_request def test_before_first_request_functions(self): got = [] @@ -1134,10 +1143,10 @@ class TestBasicFunctionality(TestFlask): got.append(42) c = app.test_client() c.get('/') - self.assert_equal(got, [42]) + assert got == [42] c.get('/') - self.assert_equal(got, [42]) - self.assert_true(app.got_first_request) + assert got == [42] + assert app.got_first_request def test_before_first_request_functions_concurrent(self): got = [] @@ -1151,13 +1160,13 @@ class TestBasicFunctionality(TestFlask): c = app.test_client() def get_and_assert(): c.get("/") - self.assert_equal(got, [42]) + assert got == [42] t = Thread(target=get_and_assert) t.start() get_and_assert() t.join() - self.assert_true(app.got_first_request) + assert app.got_first_request def test_routing_redirect_debugging(self): app = flask.Flask(__name__) @@ -1169,19 +1178,19 @@ class TestBasicFunctionality(TestFlask): try: c.post('/foo', data={}) except AssertionError as e: - self.assert_in('http://localhost/foo/', str(e)) - self.assert_in('Make sure to directly send your POST-request ' - 'to this URL', str(e)) + assert 'http://localhost/foo/' in str(e) + assert ('Make sure to directly send ' + 'your POST-request to this URL') in str(e) else: - self.fail('Expected exception') + assert False, 'Expected exception' rv = c.get('/foo', data={}, follow_redirects=True) - self.assert_equal(rv.data, b'success') + assert rv.data == b'success' app.debug = False with app.test_client() as c: rv = c.post('/foo', data={}, follow_redirects=True) - self.assert_equal(rv.data, b'success') + assert rv.data == b'success' def test_route_decorator_custom_endpoint(self): app = flask.Flask(__name__) @@ -1205,9 +1214,9 @@ class TestBasicFunctionality(TestFlask): assert flask.url_for('123') == '/bar/123' c = app.test_client() - self.assert_equal(c.get('/foo/').data, b'foo') - self.assert_equal(c.get('/bar/').data, b'bar') - self.assert_equal(c.get('/bar/123').data, b'123') + assert c.get('/foo/').data == b'foo' + assert c.get('/bar/').data == b'bar' + assert c.get('/bar/123').data == b'123' def test_preserve_only_once(self): app = flask.Flask(__name__) @@ -1219,15 +1228,15 @@ class TestBasicFunctionality(TestFlask): c = app.test_client() for x in range(3): - with self.assert_raises(ZeroDivisionError): + with pytest.raises(ZeroDivisionError): c.get('/fail') - self.assert_true(flask._request_ctx_stack.top is not None) - self.assert_true(flask._app_ctx_stack.top is not None) + assert flask._request_ctx_stack.top is not None + assert flask._app_ctx_stack.top is not None # implicit appctx disappears too flask._request_ctx_stack.top.pop() - self.assert_true(flask._request_ctx_stack.top is None) - self.assert_true(flask._app_ctx_stack.top is None) + assert flask._request_ctx_stack.top is None + assert flask._app_ctx_stack.top is None def test_preserve_remembers_exception(self): app = flask.Flask(__name__) @@ -1249,30 +1258,30 @@ class TestBasicFunctionality(TestFlask): c = app.test_client() # After this failure we did not yet call the teardown handler - with self.assert_raises(ZeroDivisionError): + with pytest.raises(ZeroDivisionError): c.get('/fail') - self.assert_equal(errors, []) + assert errors == [] # But this request triggers it, and it's an error c.get('/success') - self.assert_equal(len(errors), 2) - self.assert_true(isinstance(errors[0], ZeroDivisionError)) + assert len(errors) == 2 + assert isinstance(errors[0], ZeroDivisionError) # At this point another request does nothing. c.get('/success') - self.assert_equal(len(errors), 3) - self.assert_equal(errors[1], None) + assert len(errors) == 3 + assert errors[1] == None def test_get_method_on_g(self): app = flask.Flask(__name__) app.testing = True with app.app_context(): - self.assert_equal(flask.g.get('x'), None) - self.assert_equal(flask.g.get('x', 11), 11) + assert flask.g.get('x') == None + assert flask.g.get('x', 11) == 11 flask.g.x = 42 - self.assert_equal(flask.g.get('x'), 42) - self.assert_equal(flask.g.x, 42) + assert flask.g.get('x') == 42 + assert flask.g.x == 42 def test_g_iteration_protocol(self): app = flask.Flask(__name__) @@ -1281,9 +1290,9 @@ class TestBasicFunctionality(TestFlask): with app.app_context(): flask.g.foo = 23 flask.g.bar = 42 - self.assert_equal('foo' in flask.g, True) - self.assert_equal('foos' in flask.g, False) - self.assert_equal(sorted(flask.g), ['bar', 'foo']) + assert 'foo' in flask.g + assert 'foos' not in flask.g + assert sorted(flask.g) == ['bar', 'foo'] class TestSubdomain(TestFlask): @@ -1300,10 +1309,10 @@ class TestSubdomain(TestFlask): c = app.test_client() rv = c.get('/', 'http://localhost/') - self.assert_equal(rv.data, b'normal index') + assert rv.data == b'normal index' rv = c.get('/', 'http://test.localhost/') - self.assert_equal(rv.data, b'test index') + assert rv.data == b'test index' def test_subdomain_matching(self): app = flask.Flask(__name__) @@ -1314,7 +1323,7 @@ class TestSubdomain(TestFlask): c = app.test_client() rv = c.get('/', 'http://mitsuhiko.localhost/') - self.assert_equal(rv.data, b'index for mitsuhiko') + assert rv.data == b'index for mitsuhiko' def test_subdomain_matching_with_ports(self): app = flask.Flask(__name__) @@ -1325,7 +1334,7 @@ class TestSubdomain(TestFlask): c = app.test_client() rv = c.get('/', 'http://mitsuhiko.localhost:3000/') - self.assert_equal(rv.data, b'index for mitsuhiko') + assert rv.data == b'index for mitsuhiko' def test_multi_route_rules(self): app = flask.Flask(__name__) @@ -1336,9 +1345,9 @@ class TestSubdomain(TestFlask): return test rv = app.test_client().open('/') - self.assert_equal(rv.data, b'a') + assert rv.data == b'a' rv = app.test_client().open('/b/') - self.assert_equal(rv.data, b'b') + assert rv.data == b'b' def test_multi_route_class_views(self): class View(object): @@ -1352,6 +1361,6 @@ class TestSubdomain(TestFlask): app = flask.Flask(__name__) _ = View(app) rv = app.test_client().open('/') - self.assert_equal(rv.data, b'a') + assert rv.data == b'a' rv = app.test_client().open('/b/') - self.assert_equal(rv.data, b'b') + assert rv.data == b'b' diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index c8e85b4a..6f2be559 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -9,6 +9,8 @@ :license: BSD, see LICENSE for more details. """ +import pytest + import flask import unittest from tests import TestFlask @@ -55,9 +57,9 @@ class TestBlueprint(TestFlask): c = app.test_client() - self.assert_equal(c.get('/frontend-no').data, b'frontend says no') - self.assert_equal(c.get('/backend-no').data, b'backend says no') - self.assert_equal(c.get('/what-is-a-sideend').data, b'application itself says no') + assert c.get('/frontend-no').data == b'frontend says no' + assert c.get('/backend-no').data == b'backend says no' + assert c.get('/what-is-a-sideend').data == b'application itself says no' def test_blueprint_specific_user_error_handling(self): class MyDecoratorException(Exception): @@ -69,11 +71,11 @@ class TestBlueprint(TestFlask): @blue.errorhandler(MyDecoratorException) def my_decorator_exception_handler(e): - self.assert_true(isinstance(e, MyDecoratorException)) + assert isinstance(e, MyDecoratorException) return 'boom' def my_function_exception_handler(e): - self.assert_true(isinstance(e, MyFunctionException)) + assert isinstance(e, MyFunctionException) return 'bam' blue.register_error_handler(MyFunctionException, my_function_exception_handler) @@ -89,8 +91,8 @@ class TestBlueprint(TestFlask): c = app.test_client() - self.assert_equal(c.get('/decorator').data, b'boom') - self.assert_equal(c.get('/function').data, b'bam') + assert c.get('/decorator').data == b'boom' + assert c.get('/function').data == b'bam' def test_blueprint_url_definitions(self): bp = flask.Blueprint('test', __name__) @@ -108,10 +110,10 @@ class TestBlueprint(TestFlask): app.register_blueprint(bp, url_prefix='/2', url_defaults={'bar': 19}) c = app.test_client() - self.assert_equal(c.get('/1/foo').data, b'23/42') - self.assert_equal(c.get('/2/foo').data, b'19/42') - self.assert_equal(c.get('/1/bar').data, b'23') - self.assert_equal(c.get('/2/bar').data, b'19') + assert c.get('/1/foo').data == b'23/42' + assert c.get('/2/foo').data == b'19/42' + assert c.get('/1/bar').data == b'23' + assert c.get('/2/bar').data == b'19' def test_blueprint_url_processors(self): bp = flask.Blueprint('frontend', __name__, url_prefix='/') @@ -137,24 +139,24 @@ class TestBlueprint(TestFlask): c = app.test_client() - self.assert_equal(c.get('/de/').data, b'/de/about') - self.assert_equal(c.get('/de/about').data, b'/de/') + assert c.get('/de/').data == b'/de/about' + assert c.get('/de/about').data == b'/de/' def test_templates_and_static(self): from blueprintapp import app c = app.test_client() rv = c.get('/') - self.assert_equal(rv.data, b'Hello from the Frontend') + assert rv.data == b'Hello from the Frontend' rv = c.get('/admin/') - self.assert_equal(rv.data, b'Hello from the Admin') + assert rv.data == b'Hello from the Admin' rv = c.get('/admin/index2') - self.assert_equal(rv.data, b'Hello from the Admin') + assert rv.data == b'Hello from the Admin' rv = c.get('/admin/static/test.txt') - self.assert_equal(rv.data.strip(), b'Admin File') + assert rv.data.strip() == b'Admin File' rv.close() rv = c.get('/admin/static/css/test.css') - self.assert_equal(rv.data.strip(), b'/* nested file */') + assert rv.data.strip() == b'/* nested file */' rv.close() # try/finally, in case other tests use this app for Blueprint tests. @@ -166,25 +168,24 @@ class TestBlueprint(TestFlask): app.config['SEND_FILE_MAX_AGE_DEFAULT'] = expected_max_age rv = c.get('/admin/static/css/test.css') cc = parse_cache_control_header(rv.headers['Cache-Control']) - self.assert_equal(cc.max_age, expected_max_age) + assert cc.max_age == expected_max_age rv.close() finally: app.config['SEND_FILE_MAX_AGE_DEFAULT'] = max_age_default with app.test_request_context(): - self.assert_equal(flask.url_for('admin.static', filename='test.txt'), - '/admin/static/test.txt') + assert flask.url_for('admin.static', filename='test.txt') == '/admin/static/test.txt' with app.test_request_context(): try: flask.render_template('missing.html') except TemplateNotFound as e: - self.assert_equal(e.name, 'missing.html') + assert e.name == 'missing.html' else: - self.assert_true(0, 'expected exception') + assert 0, 'expected exception' with flask.Flask(__name__).test_request_context(): - self.assert_equal(flask.render_template('nested/nested.txt'), 'I\'m nested') + assert flask.render_template('nested/nested.txt') == 'I\'m nested' def test_default_static_cache_timeout(self): app = flask.Flask(__name__) @@ -205,7 +206,7 @@ class TestBlueprint(TestFlask): app.config['SEND_FILE_MAX_AGE_DEFAULT'] = unexpected_max_age rv = blueprint.send_static_file('index.html') cc = parse_cache_control_header(rv.headers['Cache-Control']) - self.assert_equal(cc.max_age, 100) + assert cc.max_age == 100 rv.close() finally: app.config['SEND_FILE_MAX_AGE_DEFAULT'] = max_age_default @@ -213,8 +214,7 @@ class TestBlueprint(TestFlask): def test_templates_list(self): from blueprintapp import app templates = sorted(app.jinja_env.list_templates()) - self.assert_equal(templates, ['admin/index.html', - 'frontend/index.html']) + assert templates == ['admin/index.html', 'frontend/index.html'] def test_dotted_names(self): frontend = flask.Blueprint('myapp.frontend', __name__) @@ -237,9 +237,9 @@ class TestBlueprint(TestFlask): app.register_blueprint(backend) c = app.test_client() - self.assert_equal(c.get('/fe').data.strip(), b'/be') - self.assert_equal(c.get('/fe2').data.strip(), b'/fe') - self.assert_equal(c.get('/be').data.strip(), b'/fe') + assert c.get('/fe').data.strip() == b'/be' + assert c.get('/fe2').data.strip() == b'/fe' + assert c.get('/be').data.strip() == b'/fe' def test_dotted_names_from_app(self): app = flask.Flask(__name__) @@ -258,7 +258,7 @@ class TestBlueprint(TestFlask): with app.test_client() as c: rv = c.get('/') - self.assert_equal(rv.data, b'/test/') + assert rv.data == b'/test/' def test_empty_url_defaults(self): bp = flask.Blueprint('bp', __name__) @@ -272,8 +272,8 @@ class TestBlueprint(TestFlask): app.register_blueprint(bp) c = app.test_client() - self.assert_equal(c.get('/').data, b'1') - self.assert_equal(c.get('/page/2').data, b'2') + assert c.get('/').data == b'1' + assert c.get('/page/2').data == b'2' def test_route_decorator_custom_endpoint(self): @@ -303,11 +303,11 @@ class TestBlueprint(TestFlask): return flask.request.endpoint c = app.test_client() - self.assert_equal(c.get('/').data, b'index') - self.assert_equal(c.get('/py/foo').data, b'bp.foo') - self.assert_equal(c.get('/py/bar').data, b'bp.bar') - self.assert_equal(c.get('/py/bar/123').data, b'bp.123') - self.assert_equal(c.get('/py/bar/foo').data, b'bp.bar_foo') + assert c.get('/').data == b'index' + assert c.get('/py/foo').data == b'bp.foo' + assert c.get('/py/bar').data == b'bp.bar' + assert c.get('/py/bar/123').data == b'bp.123' + assert c.get('/py/bar/foo').data == b'bp.bar_foo' def test_route_decorator_custom_endpoint_with_dots(self): bp = flask.Blueprint('bp', __name__) @@ -337,14 +337,14 @@ class TestBlueprint(TestFlask): def foo_foo_foo(): pass - self.assert_raises( + pytest.raises( AssertionError, lambda: bp.add_url_rule( '/bar/123', endpoint='bar.123', view_func=foo_foo_foo ) ) - self.assert_raises( + pytest.raises( AssertionError, bp.route('/bar/123', endpoint='bar.123'), lambda: None @@ -354,7 +354,7 @@ class TestBlueprint(TestFlask): app.register_blueprint(bp, url_prefix='/py') c = app.test_client() - self.assert_equal(c.get('/py/foo').data, b'bp.foo') + assert c.get('/py/foo').data == b'bp.foo' # The rule's didn't actually made it through rv = c.get('/py/bar') assert rv.status_code == 404 @@ -368,9 +368,9 @@ class TestBlueprint(TestFlask): return s[::-1] app = flask.Flask(__name__) app.register_blueprint(bp, url_prefix='/py') - self.assert_in('my_reverse', app.jinja_env.filters.keys()) - self.assert_equal(app.jinja_env.filters['my_reverse'], my_reverse) - self.assert_equal(app.jinja_env.filters['my_reverse']('abcd'), 'dcba') + assert 'my_reverse' in app.jinja_env.filters.keys() + assert app.jinja_env.filters['my_reverse'] == my_reverse + assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba' def test_add_template_filter(self): bp = flask.Blueprint('bp', __name__) @@ -379,9 +379,9 @@ class TestBlueprint(TestFlask): bp.add_app_template_filter(my_reverse) app = flask.Flask(__name__) app.register_blueprint(bp, url_prefix='/py') - self.assert_in('my_reverse', app.jinja_env.filters.keys()) - self.assert_equal(app.jinja_env.filters['my_reverse'], my_reverse) - self.assert_equal(app.jinja_env.filters['my_reverse']('abcd'), 'dcba') + assert 'my_reverse' in app.jinja_env.filters.keys() + assert app.jinja_env.filters['my_reverse'] == my_reverse + assert app.jinja_env.filters['my_reverse']('abcd') == 'dcba' def test_template_filter_with_name(self): bp = flask.Blueprint('bp', __name__) @@ -390,9 +390,9 @@ class TestBlueprint(TestFlask): return s[::-1] app = flask.Flask(__name__) app.register_blueprint(bp, url_prefix='/py') - self.assert_in('strrev', app.jinja_env.filters.keys()) - self.assert_equal(app.jinja_env.filters['strrev'], my_reverse) - self.assert_equal(app.jinja_env.filters['strrev']('abcd'), 'dcba') + assert 'strrev' in app.jinja_env.filters.keys() + assert app.jinja_env.filters['strrev'] == my_reverse + assert app.jinja_env.filters['strrev']('abcd') == 'dcba' def test_add_template_filter_with_name(self): bp = flask.Blueprint('bp', __name__) @@ -401,9 +401,9 @@ class TestBlueprint(TestFlask): bp.add_app_template_filter(my_reverse, 'strrev') app = flask.Flask(__name__) app.register_blueprint(bp, url_prefix='/py') - self.assert_in('strrev', app.jinja_env.filters.keys()) - self.assert_equal(app.jinja_env.filters['strrev'], my_reverse) - self.assert_equal(app.jinja_env.filters['strrev']('abcd'), 'dcba') + assert 'strrev' in app.jinja_env.filters.keys() + assert app.jinja_env.filters['strrev'] == my_reverse + assert app.jinja_env.filters['strrev']('abcd') == 'dcba' def test_template_filter_with_template(self): bp = flask.Blueprint('bp', __name__) @@ -416,7 +416,7 @@ class TestBlueprint(TestFlask): def index(): return flask.render_template('template_filter.html', value='abcd') rv = app.test_client().get('/') - self.assert_equal(rv.data, b'dcba') + assert rv.data == b'dcba' def test_template_filter_after_route_with_template(self): app = flask.Flask(__name__) @@ -429,7 +429,7 @@ class TestBlueprint(TestFlask): return s[::-1] app.register_blueprint(bp, url_prefix='/py') rv = app.test_client().get('/') - self.assert_equal(rv.data, b'dcba') + assert rv.data == b'dcba' def test_add_template_filter_with_template(self): bp = flask.Blueprint('bp', __name__) @@ -442,7 +442,7 @@ class TestBlueprint(TestFlask): def index(): return flask.render_template('template_filter.html', value='abcd') rv = app.test_client().get('/') - self.assert_equal(rv.data, b'dcba') + assert rv.data == b'dcba' def test_template_filter_with_name_and_template(self): bp = flask.Blueprint('bp', __name__) @@ -455,7 +455,7 @@ class TestBlueprint(TestFlask): def index(): return flask.render_template('template_filter.html', value='abcd') rv = app.test_client().get('/') - self.assert_equal(rv.data, b'dcba') + assert rv.data == b'dcba' def test_add_template_filter_with_name_and_template(self): bp = flask.Blueprint('bp', __name__) @@ -468,7 +468,7 @@ class TestBlueprint(TestFlask): def index(): return flask.render_template('template_filter.html', value='abcd') rv = app.test_client().get('/') - self.assert_equal(rv.data, b'dcba') + assert rv.data == b'dcba' def test_template_test(self): bp = flask.Blueprint('bp', __name__) @@ -477,9 +477,9 @@ class TestBlueprint(TestFlask): return isinstance(value, bool) app = flask.Flask(__name__) app.register_blueprint(bp, url_prefix='/py') - self.assert_in('is_boolean', app.jinja_env.tests.keys()) - self.assert_equal(app.jinja_env.tests['is_boolean'], is_boolean) - self.assert_true(app.jinja_env.tests['is_boolean'](False)) + assert 'is_boolean' in app.jinja_env.tests.keys() + assert app.jinja_env.tests['is_boolean'] == is_boolean + assert app.jinja_env.tests['is_boolean'](False) def test_add_template_test(self): bp = flask.Blueprint('bp', __name__) @@ -488,9 +488,9 @@ class TestBlueprint(TestFlask): bp.add_app_template_test(is_boolean) app = flask.Flask(__name__) app.register_blueprint(bp, url_prefix='/py') - self.assert_in('is_boolean', app.jinja_env.tests.keys()) - self.assert_equal(app.jinja_env.tests['is_boolean'], is_boolean) - self.assert_true(app.jinja_env.tests['is_boolean'](False)) + assert 'is_boolean' in app.jinja_env.tests.keys() + assert app.jinja_env.tests['is_boolean'] == is_boolean + assert app.jinja_env.tests['is_boolean'](False) def test_template_test_with_name(self): bp = flask.Blueprint('bp', __name__) @@ -499,9 +499,9 @@ class TestBlueprint(TestFlask): return isinstance(value, bool) app = flask.Flask(__name__) app.register_blueprint(bp, url_prefix='/py') - self.assert_in('boolean', app.jinja_env.tests.keys()) - self.assert_equal(app.jinja_env.tests['boolean'], is_boolean) - self.assert_true(app.jinja_env.tests['boolean'](False)) + assert 'boolean' in app.jinja_env.tests.keys() + assert app.jinja_env.tests['boolean'] == is_boolean + assert app.jinja_env.tests['boolean'](False) def test_add_template_test_with_name(self): bp = flask.Blueprint('bp', __name__) @@ -510,9 +510,9 @@ class TestBlueprint(TestFlask): bp.add_app_template_test(is_boolean, 'boolean') app = flask.Flask(__name__) app.register_blueprint(bp, url_prefix='/py') - self.assert_in('boolean', app.jinja_env.tests.keys()) - self.assert_equal(app.jinja_env.tests['boolean'], is_boolean) - self.assert_true(app.jinja_env.tests['boolean'](False)) + assert 'boolean' in app.jinja_env.tests.keys() + assert app.jinja_env.tests['boolean'] == is_boolean + assert app.jinja_env.tests['boolean'](False) def test_template_test_with_template(self): bp = flask.Blueprint('bp', __name__) @@ -525,7 +525,7 @@ class TestBlueprint(TestFlask): def index(): return flask.render_template('template_test.html', value=False) rv = app.test_client().get('/') - self.assert_in(b'Success!', rv.data) + assert b'Success!' in rv.data def test_template_test_after_route_with_template(self): app = flask.Flask(__name__) @@ -538,7 +538,7 @@ class TestBlueprint(TestFlask): return isinstance(value, bool) app.register_blueprint(bp, url_prefix='/py') rv = app.test_client().get('/') - self.assert_in(b'Success!', rv.data) + assert b'Success!' in rv.data def test_add_template_test_with_template(self): bp = flask.Blueprint('bp', __name__) @@ -551,7 +551,7 @@ class TestBlueprint(TestFlask): def index(): return flask.render_template('template_test.html', value=False) rv = app.test_client().get('/') - self.assert_in(b'Success!', rv.data) + assert b'Success!' in rv.data def test_template_test_with_name_and_template(self): bp = flask.Blueprint('bp', __name__) @@ -564,7 +564,7 @@ class TestBlueprint(TestFlask): def index(): return flask.render_template('template_test.html', value=False) rv = app.test_client().get('/') - self.assert_in(b'Success!', rv.data) + assert b'Success!' in rv.data def test_add_template_test_with_name_and_template(self): bp = flask.Blueprint('bp', __name__) @@ -577,7 +577,7 @@ class TestBlueprint(TestFlask): def index(): return flask.render_template('template_test.html', value=False) rv = app.test_client().get('/') - self.assert_in(b'Success!', rv.data) + assert b'Success!' in rv.data def suite(): suite = unittest.TestSuite() diff --git a/tests/test_config.py b/tests/test_config.py index a0e5fb53..2faaa0c8 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -30,9 +30,9 @@ SECRET_KEY = 'devkey' class TestConfig(TestFlask): def common_object_test(self, app): - self.assert_equal(app.secret_key, 'devkey') - self.assert_equal(app.config['TEST_KEY'], 'foo') - self.assert_not_in('TestConfig', app.config) + assert app.secret_key == 'devkey' + assert app.config['TEST_KEY'] == 'foo' + assert 'TestConfig' not in app.config def test_config_from_file(self): app = flask.Flask(__name__) @@ -73,7 +73,7 @@ class TestConfig(TestFlask): self.common_object_test(app) app = flask.Flask(__name__) - with self.assert_raises(TypeError): + with pytest.raises(TypeError): app.config.from_mapping( {}, {} ) @@ -95,13 +95,13 @@ class TestConfig(TestFlask): try: app.config.from_envvar('FOO_SETTINGS') except RuntimeError as e: - self.assert_true("'FOO_SETTINGS' is not set" in str(e)) + assert "'FOO_SETTINGS' is not set" in str(e) else: - self.assert_true(0, 'expected exception') - self.assert_false(app.config.from_envvar('FOO_SETTINGS', silent=True)) + assert 0, 'expected exception' + assert not app.config.from_envvar('FOO_SETTINGS', silent=True) os.environ = {'FOO_SETTINGS': __file__.rsplit('.', 1)[0] + '.py'} - self.assert_true(app.config.from_envvar('FOO_SETTINGS')) + assert app.config.from_envvar('FOO_SETTINGS') self.common_object_test(app) finally: os.environ = env @@ -115,12 +115,12 @@ class TestConfig(TestFlask): app.config.from_envvar('FOO_SETTINGS') except IOError as e: msg = str(e) - self.assert_true(msg.startswith('[Errno 2] Unable to load configuration ' - 'file (No such file or directory):')) - self.assert_true(msg.endswith("missing.cfg'")) + assert msg.startswith('[Errno 2] Unable to load configuration ' + 'file (No such file or directory):') + assert msg.endswith("missing.cfg'") else: - self.fail('expected IOError') - self.assert_false(app.config.from_envvar('FOO_SETTINGS', silent=True)) + assert False, 'expected IOError' + assert not app.config.from_envvar('FOO_SETTINGS', silent=True) finally: os.environ = env @@ -130,12 +130,12 @@ class TestConfig(TestFlask): app.config.from_pyfile('missing.cfg') except IOError as e: msg = str(e) - self.assert_true(msg.startswith('[Errno 2] Unable to load configuration ' - 'file (No such file or directory):')) - self.assert_true(msg.endswith("missing.cfg'")) + assert msg.startswith('[Errno 2] Unable to load configuration ' + 'file (No such file or directory):') + assert msg.endswith("missing.cfg'") else: - self.assert_true(0, 'expected config') - self.assert_false(app.config.from_pyfile('missing.cfg', silent=True)) + assert 0, 'expected config' + assert not app.config.from_pyfile('missing.cfg', silent=True) def test_config_missing_json(self): app = flask.Flask(__name__) @@ -143,12 +143,12 @@ class TestConfig(TestFlask): app.config.from_json('missing.json') except IOError as e: msg = str(e) - self.assert_true(msg.startswith('[Errno 2] Unable to load configuration ' - 'file (No such file or directory):')) - self.assert_true(msg.endswith("missing.json'")) + assert msg.startswith('[Errno 2] Unable to load configuration ' + 'file (No such file or directory):') + assert msg.endswith("missing.json'") else: - self.assert_true(0, 'expected config') - self.assert_false(app.config.from_json('missing.json', silent=True)) + assert 0, 'expected config' + assert not app.config.from_json('missing.json', silent=True) def test_custom_config_class(self): class Config(flask.Config): @@ -156,14 +156,14 @@ class TestConfig(TestFlask): class Flask(flask.Flask): config_class = Config app = Flask(__name__) - self.assert_isinstance(app.config, Config) + assert isinstance(app.config, Config) app.config.from_object(__name__) self.common_object_test(app) def test_session_lifetime(self): app = flask.Flask(__name__) app.config['PERMANENT_SESSION_LIFETIME'] = 42 - self.assert_equal(app.permanent_session_lifetime.seconds, 42) + assert app.permanent_session_lifetime.seconds == 42 def test_get_namespace(self): app = flask.Flask(__name__) @@ -172,13 +172,13 @@ class TestConfig(TestFlask): app.config['BAR_STUFF_1'] = 'bar stuff 1' app.config['BAR_STUFF_2'] = 'bar stuff 2' foo_options = app.config.get_namespace('FOO_') - self.assert_equal(2, len(foo_options)) - self.assert_equal('foo option 1', foo_options['option_1']) - self.assert_equal('foo option 2', foo_options['option_2']) + assert 2 == len(foo_options) + assert 'foo option 1' == foo_options['option_1'] + assert 'foo option 2' == foo_options['option_2'] bar_options = app.config.get_namespace('BAR_', lowercase=False) - self.assert_equal(2, len(bar_options)) - self.assert_equal('bar stuff 1', bar_options['STUFF_1']) - self.assert_equal('bar stuff 2', bar_options['STUFF_2']) + assert 2 == len(bar_options) + assert 'bar stuff 1' == bar_options['STUFF_1'] + assert 'bar stuff 2' == bar_options['STUFF_2'] class TestInstance(TestFlask): @@ -188,7 +188,7 @@ class TestInstance(TestFlask): assert 'must be absolute' in str(excinfo.value) app = flask.Flask(__name__, instance_path=str(apps_tmpdir)) - self.assert_equal(app.instance_path, str(apps_tmpdir)) + assert app.instance_path == str(apps_tmpdir) def test_main_module_paths(self, apps_tmpdir, purge_module): app = apps_tmpdir.join('main_app.py') @@ -285,7 +285,7 @@ class TestInstance(TestFlask): return self sys.meta_path.append(Loader()) try: - with self.assert_raises(AttributeError): + with pytest.raises(AttributeError): flask.Flask(__name__) finally: sys.meta_path.pop() diff --git a/tests/test_ext.py b/tests/test_ext.py index e48563bf..1bec1b62 100644 --- a/tests/test_ext.py +++ b/tests/test_ext.py @@ -11,6 +11,8 @@ import sys import unittest +import pytest + try: from imp import reload as reload_module except ImportError: @@ -41,70 +43,70 @@ class TestExtImportHook(TestFlask): if cls.__module__ == 'flask.exthook' and \ cls.__name__ == 'ExtensionImporter': import_hooks += 1 - self.assert_equal(import_hooks, 1) + assert import_hooks == 1 def teardown(self): from flask import ext for key in ext.__dict__: - self.assert_not_in('.', key) + assert '.' not in key def test_flaskext_new_simple_import_normal(self): from flask.ext.newext_simple import ext_id - self.assert_equal(ext_id, 'newext_simple') + assert ext_id == 'newext_simple' def test_flaskext_new_simple_import_module(self): from flask.ext import newext_simple - self.assert_equal(newext_simple.ext_id, 'newext_simple') - self.assert_equal(newext_simple.__name__, 'flask_newext_simple') + assert newext_simple.ext_id == 'newext_simple' + assert newext_simple.__name__ == 'flask_newext_simple' def test_flaskext_new_package_import_normal(self): from flask.ext.newext_package import ext_id - self.assert_equal(ext_id, 'newext_package') + assert ext_id == 'newext_package' def test_flaskext_new_package_import_module(self): from flask.ext import newext_package - self.assert_equal(newext_package.ext_id, 'newext_package') - self.assert_equal(newext_package.__name__, 'flask_newext_package') + assert newext_package.ext_id == 'newext_package' + assert newext_package.__name__ == 'flask_newext_package' def test_flaskext_new_package_import_submodule_function(self): from flask.ext.newext_package.submodule import test_function - self.assert_equal(test_function(), 42) + assert test_function() == 42 def test_flaskext_new_package_import_submodule(self): from flask.ext.newext_package import submodule - self.assert_equal(submodule.__name__, 'flask_newext_package.submodule') - self.assert_equal(submodule.test_function(), 42) + assert submodule.__name__ == 'flask_newext_package.submodule' + assert submodule.test_function() == 42 def test_flaskext_old_simple_import_normal(self): from flask.ext.oldext_simple import ext_id - self.assert_equal(ext_id, 'oldext_simple') + assert ext_id == 'oldext_simple' def test_flaskext_old_simple_import_module(self): from flask.ext import oldext_simple - self.assert_equal(oldext_simple.ext_id, 'oldext_simple') - self.assert_equal(oldext_simple.__name__, 'flaskext.oldext_simple') + assert oldext_simple.ext_id == 'oldext_simple' + assert oldext_simple.__name__ == 'flaskext.oldext_simple' def test_flaskext_old_package_import_normal(self): from flask.ext.oldext_package import ext_id - self.assert_equal(ext_id, 'oldext_package') + assert ext_id == 'oldext_package' def test_flaskext_old_package_import_module(self): from flask.ext import oldext_package - self.assert_equal(oldext_package.ext_id, 'oldext_package') - self.assert_equal(oldext_package.__name__, 'flaskext.oldext_package') + assert oldext_package.ext_id == 'oldext_package' + assert oldext_package.__name__ == 'flaskext.oldext_package' def test_flaskext_old_package_import_submodule(self): from flask.ext.oldext_package import submodule - self.assert_equal(submodule.__name__, 'flaskext.oldext_package.submodule') - self.assert_equal(submodule.test_function(), 42) + assert submodule.__name__ == 'flaskext.oldext_package.submodule' + assert submodule.test_function() == 42 def test_flaskext_old_package_import_submodule_function(self): from flask.ext.oldext_package.submodule import test_function - self.assert_equal(test_function(), 42) + assert test_function() == 42 def test_flaskext_broken_package_no_module_caching(self): for x in range(2): - with self.assert_raises(ImportError): + with pytest.raises(ImportError): import flask.ext.broken def test_no_error_swallowing(self): @@ -112,13 +114,13 @@ class TestExtImportHook(TestFlask): import flask.ext.broken except ImportError: exc_type, exc_value, tb = sys.exc_info() - self.assert_true(exc_type is ImportError) + assert exc_type is ImportError if PY2: message = 'No module named missing_module' else: message = 'No module named \'missing_module\'' - self.assert_equal(str(exc_value), message) - self.assert_true(tb.tb_frame.f_globals is globals()) + assert str(exc_value) == message + assert tb.tb_frame.f_globals is globals() # reraise() adds a second frame so we need to skip that one too. # On PY3 we even have another one :( @@ -127,7 +129,8 @@ class TestExtImportHook(TestFlask): next = next.tb_next import os.path - self.assert_in(os.path.join('flask_broken', '__init__.py'), next.tb_frame.f_code.co_filename) + assert os.path.join('flask_broken', '__init__.py') in \ + next.tb_frame.f_code.co_filename def suite(): diff --git a/tests/test_helpers.py b/tests/test_helpers.py index e5055fa6..d1091729 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -9,6 +9,8 @@ :license: BSD, see LICENSE for more details. """ +import pytest + import os import flask import unittest @@ -36,7 +38,7 @@ class TestJSON(TestFlask): return flask.jsonify(foo=text_type(flask.request.get_json())) c = app.test_client() rv = c.post('/json', data='malformed', content_type='application/json') - self.assert_equal(rv.status_code, 400) + assert rv.status_code == 400 def test_json_custom_mimetypes(self): app = flask.Flask(__name__) @@ -45,7 +47,7 @@ class TestJSON(TestFlask): return flask.request.get_json() c = app.test_client() rv = c.post('/json', data='"foo"', content_type='application/x+json') - self.assert_equal(rv.data, b'foo') + assert rv.data == b'foo' def test_json_body_encoding(self): app = flask.Flask(__name__) @@ -57,7 +59,7 @@ class TestJSON(TestFlask): c = app.test_client() resp = c.get('/', data=u'"Hällo Wörld"'.encode('iso-8859-15'), content_type='application/json; charset=iso-8859-15') - self.assert_equal(resp.data, u'Hällo Wörld'.encode('utf-8')) + assert resp.data == u'Hällo Wörld'.encode('utf-8') def test_jsonify(self): d = dict(a=23, b=42, c=[1, 2, 3]) @@ -71,8 +73,8 @@ class TestJSON(TestFlask): c = app.test_client() for url in '/kw', '/dict': rv = c.get(url) - self.assert_equal(rv.mimetype, 'application/json') - self.assert_equal(flask.json.loads(rv.data), d) + assert rv.mimetype == 'application/json' + assert flask.json.loads(rv.data) == d def test_json_as_unicode(self): app = flask.Flask(__name__) @@ -80,12 +82,12 @@ class TestJSON(TestFlask): app.config['JSON_AS_ASCII'] = True with app.app_context(): rv = flask.json.dumps(u'\N{SNOWMAN}') - self.assert_equal(rv, '"\\u2603"') + assert rv == '"\\u2603"' app.config['JSON_AS_ASCII'] = False with app.app_context(): rv = flask.json.dumps(u'\N{SNOWMAN}') - self.assert_equal(rv, u'"\u2603"') + assert rv == u'"\u2603"' def test_json_attr(self): app = flask.Flask(__name__) @@ -96,29 +98,28 @@ class TestJSON(TestFlask): c = app.test_client() rv = c.post('/add', data=flask.json.dumps({'a': 1, 'b': 2}), content_type='application/json') - self.assert_equal(rv.data, b'3') + assert rv.data == b'3' def test_template_escaping(self): app = flask.Flask(__name__) render = flask.render_template_string with app.test_request_context(): rv = flask.json.htmlsafe_dumps('') - self.assert_equal(rv, u'"\\u003c/script\\u003e"') - self.assert_equal(type(rv), text_type) + assert rv == u'"\\u003c/script\\u003e"' + assert type(rv) == text_type rv = render('{{ ""|tojson }}') - self.assert_equal(rv, '"\\u003c/script\\u003e"') + assert rv == '"\\u003c/script\\u003e"' rv = render('{{ "<\0/script>"|tojson }}') - self.assert_equal(rv, '"\\u003c\\u0000/script\\u003e"') + assert rv == '"\\u003c\\u0000/script\\u003e"' rv = render('{{ "