diff --git a/tests/conftest.py b/tests/conftest.py
new file mode 100644
index 00000000..5844e3a1
--- /dev/null
+++ b/tests/conftest.py
@@ -0,0 +1,8 @@
+import pytest
+
+from sanic import Sanic
+
+
+@pytest.fixture
+def app(request):
+ return Sanic(request.node.name)
diff --git a/tests/test_bad_request.py b/tests/test_bad_request.py
index bf595085..eed4e83a 100644
--- a/tests/test_bad_request.py
+++ b/tests/test_bad_request.py
@@ -1,9 +1,7 @@
import asyncio
-from sanic import Sanic
-def test_bad_request_response():
- app = Sanic('test_bad_request_response')
+def test_bad_request_response(app):
lines = []
@app.listener('after_server_start')
async def _request(sanic, loop):
diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py
index 37756085..4b821e91 100644
--- a/tests/test_blueprints.py
+++ b/tests/test_blueprints.py
@@ -3,9 +3,8 @@ import inspect
import os
import pytest
-from sanic import Sanic
from sanic.blueprints import Blueprint
-from sanic.response import json, text
+from sanic.response import text
from sanic.exceptions import NotFound, ServerError, InvalidUsage
from sanic.constants import HTTP_METHODS
@@ -23,8 +22,7 @@ def get_file_content(static_file_directory, file_name):
return file.read()
@pytest.mark.parametrize('method', HTTP_METHODS)
-def test_versioned_routes_get(method):
- app = Sanic('test_shorhand_routes_get')
+def test_versioned_routes_get(app, method):
bp = Blueprint('test_text')
method = method.lower()
@@ -46,8 +44,7 @@ def test_versioned_routes_get(method):
assert response.status == 200
-def test_bp():
- app = Sanic('test_text')
+def test_bp(app):
bp = Blueprint('test_text')
@bp.route('/')
@@ -60,8 +57,7 @@ def test_bp():
assert response.text == 'Hello'
-def test_bp_strict_slash():
- app = Sanic('test_route_strict_slash')
+def test_bp_strict_slash(app):
bp = Blueprint('test_text')
@bp.get('/get', strict_slashes=True)
@@ -87,8 +83,7 @@ def test_bp_strict_slash():
request, response = app.test_client.post('/post')
assert response.status == 404
-def test_bp_strict_slash_default_value():
- app = Sanic('test_route_strict_slash')
+def test_bp_strict_slash_default_value(app):
bp = Blueprint('test_text', strict_slashes=True)
@bp.get('/get')
@@ -107,8 +102,7 @@ def test_bp_strict_slash_default_value():
request, response = app.test_client.post('/post')
assert response.status == 404
-def test_bp_strict_slash_without_passing_default_value():
- app = Sanic('test_route_strict_slash')
+def test_bp_strict_slash_without_passing_default_value(app):
bp = Blueprint('test_text')
@bp.get('/get')
@@ -127,8 +121,7 @@ def test_bp_strict_slash_without_passing_default_value():
request, response = app.test_client.post('/post')
assert response.text == 'OK'
-def test_bp_strict_slash_default_value_can_be_overwritten():
- app = Sanic('test_route_strict_slash')
+def test_bp_strict_slash_default_value_can_be_overwritten(app):
bp = Blueprint('test_text', strict_slashes=True)
@bp.get('/get', strict_slashes=False)
@@ -147,8 +140,7 @@ def test_bp_strict_slash_default_value_can_be_overwritten():
request, response = app.test_client.post('/post')
assert response.text == 'OK'
-def test_bp_with_url_prefix():
- app = Sanic('test_text')
+def test_bp_with_url_prefix(app):
bp = Blueprint('test_text', url_prefix='/test1')
@bp.route('/')
@@ -161,8 +153,7 @@ def test_bp_with_url_prefix():
assert response.text == 'Hello'
-def test_several_bp_with_url_prefix():
- app = Sanic('test_text')
+def test_several_bp_with_url_prefix(app):
bp = Blueprint('test_text', url_prefix='/test1')
bp2 = Blueprint('test_text2', url_prefix='/test2')
@@ -182,8 +173,7 @@ def test_several_bp_with_url_prefix():
request, response = app.test_client.get('/test2/')
assert response.text == 'Hello2'
-def test_bp_with_host():
- app = Sanic('test_bp_host')
+def test_bp_with_host(app):
bp = Blueprint('test_bp_host', url_prefix='/test1', host="example.com")
@bp.route('/')
@@ -209,8 +199,7 @@ def test_bp_with_host():
assert response.text == 'Hello subdomain!'
-def test_several_bp_with_host():
- app = Sanic('test_text')
+def test_several_bp_with_host(app):
bp = Blueprint('test_text',
url_prefix='/test',
host="example.com")
@@ -253,8 +242,7 @@ def test_several_bp_with_host():
headers=headers)
assert response.text == 'Hello3'
-def test_bp_middleware():
- app = Sanic('test_middleware')
+def test_bp_middleware(app):
blueprint = Blueprint('test_middleware')
@blueprint.middleware('response')
@@ -272,8 +260,7 @@ def test_bp_middleware():
assert response.status == 200
assert response.text == 'OK'
-def test_bp_exception_handler():
- app = Sanic('test_middleware')
+def test_bp_exception_handler(app):
blueprint = Blueprint('test_middleware')
@blueprint.route('/1')
@@ -305,8 +292,7 @@ def test_bp_exception_handler():
request, response = app.test_client.get('/3')
assert response.status == 200
-def test_bp_listeners():
- app = Sanic('test_middleware')
+def test_bp_listeners(app):
blueprint = Blueprint('test_middleware')
order = []
@@ -341,12 +327,11 @@ def test_bp_listeners():
assert order == [1,2,3,4,5,6]
-def test_bp_static():
+def test_bp_static(app):
current_file = inspect.getfile(inspect.currentframe())
with open(current_file, 'rb') as file:
current_file_contents = file.read()
- app = Sanic('test_static')
blueprint = Blueprint('test_static')
blueprint.static('/testing.file', current_file)
@@ -358,13 +343,12 @@ def test_bp_static():
assert response.body == current_file_contents
@pytest.mark.parametrize('file_name', ['test.html'])
-def test_bp_static_content_type(file_name):
+def test_bp_static_content_type(app, file_name):
# This is done here, since no other test loads a file here
current_file = inspect.getfile(inspect.currentframe())
current_directory = os.path.dirname(os.path.abspath(current_file))
static_directory = os.path.join(current_directory, 'static')
- app = Sanic('test_static')
blueprint = Blueprint('test_static')
blueprint.static(
'/testing.file',
@@ -379,8 +363,7 @@ def test_bp_static_content_type(file_name):
assert response.body == get_file_content(static_directory, file_name)
assert response.headers['Content-Type'] == 'text/html; charset=utf-8'
-def test_bp_shorthand():
- app = Sanic('test_shorhand_routes')
+def test_bp_shorthand(app):
blueprint = Blueprint('test_shorhand_routes')
ev = asyncio.Event()
@@ -478,9 +461,7 @@ def test_bp_shorthand():
assert response.status == 101
assert ev.is_set()
-def test_bp_group():
- app = Sanic('test_nested_bp_groups')
-
+def test_bp_group(app):
deep_0 = Blueprint('deep_0', url_prefix='/deep')
deep_1 = Blueprint('deep_1', url_prefix = '/deep1')
diff --git a/tests/test_config.py b/tests/test_config.py
index e393d02b..3db35f4d 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -5,8 +5,7 @@ from tempfile import NamedTemporaryFile
from sanic import Sanic
-def test_load_from_object():
- app = Sanic('test_load_from_object')
+def test_load_from_object(app):
class Config:
not_for_config = 'should not be used'
CONFIG_VALUE = 'should be used'
@@ -34,8 +33,7 @@ def test_load_env_prefix():
assert app.config.TEST_ANSWER == 42
del environ["MYAPP_TEST_ANSWER"]
-def test_load_from_file():
- app = Sanic('test_load_from_file')
+def test_load_from_file(app):
config = b"""
VALUE = 'some value'
condition = 1 == 1
@@ -53,14 +51,12 @@ if condition:
assert 'condition' not in app.config
-def test_load_from_missing_file():
- app = Sanic('test_load_from_missing_file')
+def test_load_from_missing_file(app):
with pytest.raises(IOError):
app.config.from_pyfile('non-existent file')
-def test_load_from_envvar():
- app = Sanic('test_load_from_envvar')
+def test_load_from_envvar(app):
config = b"VALUE = 'some value'"
with NamedTemporaryFile() as config_file:
config_file.write(config)
@@ -71,14 +67,12 @@ def test_load_from_envvar():
assert app.config.VALUE == 'some value'
-def test_load_from_missing_envvar():
- app = Sanic('test_load_from_missing_envvar')
+def test_load_from_missing_envvar(app):
with pytest.raises(RuntimeError):
app.config.from_envvar('non-existent variable')
-def test_overwrite_exisiting_config():
- app = Sanic('test_overwrite_exisiting_config')
+def test_overwrite_exisiting_config(app):
app.config.DEFAULT = 1
class Config:
DEFAULT = 2
@@ -87,7 +81,6 @@ def test_overwrite_exisiting_config():
assert app.config.DEFAULT == 2
-def test_missing_config():
- app = Sanic('test_missing_config')
+def test_missing_config(app):
with pytest.raises(AttributeError):
app.config.NON_EXISTENT
diff --git a/tests/test_cookies.py b/tests/test_cookies.py
index 61f50735..87d7dcad 100644
--- a/tests/test_cookies.py
+++ b/tests/test_cookies.py
@@ -9,8 +9,7 @@ import pytest
# GET
# ------------------------------------------------------------ #
-def test_cookies():
- app = Sanic('test_text')
+def test_cookies(app):
@app.route('/')
def handler(request):
@@ -30,8 +29,7 @@ def test_cookies():
(False, False),
(True, True),
])
-def test_false_cookies_encoded(httponly, expected):
- app = Sanic('test_text')
+def test_false_cookies_encoded(app, httponly, expected):
@app.route('/')
def handler(request):
@@ -49,8 +47,7 @@ def test_false_cookies_encoded(httponly, expected):
(False, False),
(True, True),
])
-def test_false_cookies(httponly, expected):
- app = Sanic('test_text')
+def test_false_cookies(app, httponly, expected):
@app.route('/')
def handler(request):
@@ -65,8 +62,7 @@ def test_false_cookies(httponly, expected):
assert ('HttpOnly' in response_cookies['right_back'].output()) == expected
-def test_http2_cookies():
- app = Sanic('test_http2_cookies')
+def test_http2_cookies(app):
@app.route('/')
async def handler(request):
@@ -78,8 +74,7 @@ def test_http2_cookies():
assert response.text == 'Cookies are: working!'
-def test_cookie_options():
- app = Sanic('test_text')
+def test_cookie_options(app):
@app.route('/')
def handler(request):
@@ -96,8 +91,7 @@ def test_cookie_options():
assert response_cookies['test'].value == 'at you'
assert response_cookies['test']['httponly'] == True
-def test_cookie_deletion():
- app = Sanic('test_text')
+def test_cookie_deletion(app):
@app.route('/')
def handler(request):
diff --git a/tests/test_create_task.py b/tests/test_create_task.py
index 1517ca8c..3a94884d 100644
--- a/tests/test_create_task.py
+++ b/tests/test_create_task.py
@@ -1,18 +1,16 @@
-from sanic import Sanic
from sanic.response import text
from threading import Event
import asyncio
from queue import Queue
-def test_create_task():
+def test_create_task(app):
e = Event()
async def coro():
await asyncio.sleep(0.05)
e.set()
- app = Sanic('test_create_task')
app.add_task(coro)
@app.route('/early')
@@ -30,8 +28,7 @@ def test_create_task():
request, response = app.test_client.get('/late')
assert response.body == b'True'
-def test_create_task_with_app_arg():
- app = Sanic('test_add_task')
+def test_create_task_with_app_arg(app):
q = Queue()
@app.route('/')
@@ -44,4 +41,4 @@ def test_create_task_with_app_arg():
app.add_task(coro)
request, response = app.test_client.get('/')
- assert q.get() == 'test_add_task'
+ assert q.get() == 'test_create_task_with_app_arg'
diff --git a/tests/test_custom_protocol.py b/tests/test_custom_protocol.py
index 74564012..236eb831 100644
--- a/tests/test_custom_protocol.py
+++ b/tests/test_custom_protocol.py
@@ -1,9 +1,6 @@
-from sanic import Sanic
from sanic.server import HttpProtocol
from sanic.response import text
-app = Sanic('test_custom_porotocol')
-
class CustomHttpProtocol(HttpProtocol):
@@ -16,12 +13,12 @@ class CustomHttpProtocol(HttpProtocol):
self.transport.close()
-@app.route('/1')
-async def handler_1(request):
- return 'OK'
+def test_use_custom_protocol(app):
+ @app.route('/1')
+ async def handler_1(request):
+ return 'OK'
-def test_use_custom_protocol():
server_kwargs = {
'protocol': CustomHttpProtocol
}
diff --git a/tests/test_dynamic_routes.py b/tests/test_dynamic_routes.py
index 950584a8..a37861e3 100644
--- a/tests/test_dynamic_routes.py
+++ b/tests/test_dynamic_routes.py
@@ -10,8 +10,7 @@ import pytest
("put", "text", "OK2 test"),
("delete", "status", 405),
])
-def test_overload_dynamic_routes(method, attr, expected):
- app = Sanic('test_dynamic_route')
+def test_overload_dynamic_routes(app, method, attr, expected):
@app.route('/overload/', methods=['GET'])
async def handler1(request, param):
@@ -25,8 +24,7 @@ def test_overload_dynamic_routes(method, attr, expected):
assert getattr(response, attr) == expected
-def test_overload_dynamic_routes_exist():
- app = Sanic('test_dynamic_route')
+def test_overload_dynamic_routes_exist(app):
@app.route('/overload/', methods=['GET'])
async def handler1(request, param):
diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py
index b4e2c6ea..cf41982d 100644
--- a/tests/test_exceptions.py
+++ b/tests/test_exceptions.py
@@ -81,8 +81,7 @@ def exception_app():
return app
-def test_catch_exception_list():
- app = Sanic('exception_list')
+def test_catch_exception_list(app):
@app.exception([SanicExceptionTestException, NotFound])
def exception_list(request, exception):
diff --git a/tests/test_logging.py b/tests/test_logging.py
index 1a040a5c..3af3f122 100644
--- a/tests/test_logging.py
+++ b/tests/test_logging.py
@@ -23,7 +23,7 @@ def reset_logging():
reload(logging)
-def test_log():
+def test_log(app):
log_stream = StringIO()
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
@@ -33,7 +33,6 @@ def test_log():
stream=log_stream
)
log = logging.getLogger()
- app = Sanic('test_logging')
rand_string = str(uuid.uuid4())
@app.route('/')
@@ -80,9 +79,8 @@ def test_logging_pass_customer_logconfig():
@pytest.mark.parametrize('debug', (True, False, ))
-def test_log_connection_lost(debug, monkeypatch):
+def test_log_connection_lost(app, debug, monkeypatch):
""" Should not log Connection lost exception on non debug """
- app = Sanic('connection_lost')
stream = StringIO()
root = logging.getLogger('root')
root.addHandler(logging.StreamHandler(stream))
diff --git a/tests/test_middleware.py b/tests/test_middleware.py
index 4d4d6901..d2098cd1 100644
--- a/tests/test_middleware.py
+++ b/tests/test_middleware.py
@@ -1,7 +1,5 @@
-from json import loads as json_loads, dumps as json_dumps
-from sanic import Sanic
from sanic.request import Request
-from sanic.response import json, text, HTTPResponse
+from sanic.response import text, HTTPResponse
from sanic.exceptions import NotFound
@@ -9,9 +7,7 @@ from sanic.exceptions import NotFound
# GET
# ------------------------------------------------------------ #
-def test_middleware_request():
- app = Sanic('test_middleware_request')
-
+def test_middleware_request(app):
results = []
@app.middleware
@@ -28,9 +24,7 @@ def test_middleware_request():
assert type(results[0]) is Request
-def test_middleware_response():
- app = Sanic('test_middleware_response')
-
+def test_middleware_response(app):
results = []
@app.middleware('request')
@@ -54,8 +48,7 @@ def test_middleware_response():
assert isinstance(results[2], HTTPResponse)
-def test_middleware_response_exception():
- app = Sanic('test_middleware_response_exception')
+def test_middleware_response_exception(app):
result = {'status_code': None}
@app.middleware('response')
@@ -75,8 +68,7 @@ def test_middleware_response_exception():
assert response.text == 'OK'
assert result['status_code'] == 404
-def test_middleware_override_request():
- app = Sanic('test_middleware_override_request')
+def test_middleware_override_request(app):
@app.middleware
async def halt_request(request):
@@ -92,8 +84,7 @@ def test_middleware_override_request():
assert response.text == 'OK'
-def test_middleware_override_response():
- app = Sanic('test_middleware_override_response')
+def test_middleware_override_response(app):
@app.middleware('response')
async def process_response(request, response):
@@ -109,10 +100,7 @@ def test_middleware_override_response():
assert response.text == 'OK'
-
-def test_middleware_order():
- app = Sanic('test_middleware_order')
-
+def test_middleware_order(app):
order = []
@app.middleware('request')
diff --git a/tests/test_multiprocessing.py b/tests/test_multiprocessing.py
index 7d94a972..c78e19fd 100644
--- a/tests/test_multiprocessing.py
+++ b/tests/test_multiprocessing.py
@@ -2,15 +2,13 @@ import multiprocessing
import random
import signal
-from sanic import Sanic
from sanic.testing import HOST, PORT
-def test_multiprocessing():
+def test_multiprocessing(app):
"""Tests that the number of children we produce is correct"""
# Selects a number at random so we can spot check
num_workers = random.choice(range(2, multiprocessing.cpu_count() * 2 + 1))
- app = Sanic('test_multiprocessing')
process_list = set()
def stop_on_alarm(*args):
diff --git a/tests/test_named_routes.py b/tests/test_named_routes.py
index ca377e8d..65d5955a 100644
--- a/tests/test_named_routes.py
+++ b/tests/test_named_routes.py
@@ -4,7 +4,6 @@
import asyncio
import pytest
-from sanic import Sanic
from sanic.blueprints import Blueprint
from sanic.response import text
from sanic.exceptions import URLBuildError
@@ -16,8 +15,7 @@ from sanic.constants import HTTP_METHODS
# ------------------------------------------------------------ #
@pytest.mark.parametrize('method', HTTP_METHODS)
-def test_versioned_named_routes_get(method):
- app = Sanic('test_shorhand_routes_get')
+def test_versioned_named_routes_get(app, method):
bp = Blueprint('test_bp', url_prefix='/bp')
method = method.lower()
@@ -57,8 +55,7 @@ def test_versioned_named_routes_get(method):
app.url_for('handler')
-def test_shorthand_default_routes_get():
- app = Sanic('test_shorhand_routes_get')
+def test_shorthand_default_routes_get(app):
@app.get('/get')
def handler(request):
@@ -68,8 +65,7 @@ def test_shorthand_default_routes_get():
assert app.url_for('handler') == '/get'
-def test_shorthand_named_routes_get():
- app = Sanic('test_shorhand_routes_get')
+def test_shorthand_named_routes_get(app):
bp = Blueprint('test_bp', url_prefix='/bp')
@app.get('/get', name='route_get')
@@ -93,8 +89,7 @@ def test_shorthand_named_routes_get():
app.url_for('test_bp.handler2')
-def test_shorthand_named_routes_post():
- app = Sanic('test_shorhand_routes_post')
+def test_shorthand_named_routes_post(app):
@app.post('/post', name='route_name')
def handler(request):
@@ -106,8 +101,7 @@ def test_shorthand_named_routes_post():
app.url_for('handler')
-def test_shorthand_named_routes_put():
- app = Sanic('test_shorhand_routes_put')
+def test_shorthand_named_routes_put(app):
@app.put('/put', name='route_put')
def handler(request):
@@ -121,8 +115,7 @@ def test_shorthand_named_routes_put():
app.url_for('handler')
-def test_shorthand_named_routes_delete():
- app = Sanic('test_shorhand_routes_delete')
+def test_shorthand_named_routes_delete(app):
@app.delete('/delete', name='route_delete')
def handler(request):
@@ -136,8 +129,7 @@ def test_shorthand_named_routes_delete():
app.url_for('handler')
-def test_shorthand_named_routes_patch():
- app = Sanic('test_shorhand_routes_patch')
+def test_shorthand_named_routes_patch(app):
@app.patch('/patch', name='route_patch')
def handler(request):
@@ -151,8 +143,7 @@ def test_shorthand_named_routes_patch():
app.url_for('handler')
-def test_shorthand_named_routes_head():
- app = Sanic('test_shorhand_routes_head')
+def test_shorthand_named_routes_head(app):
@app.head('/head', name='route_head')
def handler(request):
@@ -166,8 +157,7 @@ def test_shorthand_named_routes_head():
app.url_for('handler')
-def test_shorthand_named_routes_options():
- app = Sanic('test_shorhand_routes_options')
+def test_shorthand_named_routes_options(app):
@app.options('/options', name='route_options')
def handler(request):
@@ -181,8 +171,7 @@ def test_shorthand_named_routes_options():
app.url_for('handler')
-def test_named_static_routes():
- app = Sanic('test_dynamic_route')
+def test_named_static_routes(app):
@app.route('/test', name='route_test')
async def handler1(request):
@@ -205,9 +194,7 @@ def test_named_static_routes():
app.url_for('handler2')
-def test_named_dynamic_route():
- app = Sanic('test_dynamic_route')
-
+def test_named_dynamic_route(app):
results = []
@app.route('/folder/', name='route_dynamic')
@@ -221,8 +208,7 @@ def test_named_dynamic_route():
app.url_for('handler')
-def test_dynamic_named_route_regex():
- app = Sanic('test_dynamic_route_regex')
+def test_dynamic_named_route_regex(app):
@app.route('/folder/', name='route_re')
async def handler(request, folder_id):
@@ -235,8 +221,7 @@ def test_dynamic_named_route_regex():
app.url_for('handler')
-def test_dynamic_named_route_path():
- app = Sanic('test_dynamic_route_path')
+def test_dynamic_named_route_path(app):
@app.route('//info', name='route_dynamic_path')
async def handler(request, path):
@@ -249,8 +234,7 @@ def test_dynamic_named_route_path():
app.url_for('handler')
-def test_dynamic_named_route_unhashable():
- app = Sanic('test_dynamic_route_unhashable')
+def test_dynamic_named_route_unhashable(app):
@app.route('/folder//end/',
name='route_unhashable')
@@ -265,8 +249,7 @@ def test_dynamic_named_route_unhashable():
app.url_for('handler')
-def test_websocket_named_route():
- app = Sanic('test_websocket_route')
+def test_websocket_named_route(app):
ev = asyncio.Event()
@app.websocket('/ws', name='route_ws')
@@ -280,8 +263,7 @@ def test_websocket_named_route():
app.url_for('handler')
-def test_websocket_named_route_with_subprotocols():
- app = Sanic('test_websocket_route')
+def test_websocket_named_route_with_subprotocols(app):
results = []
@app.websocket('/ws', subprotocols=['foo', 'bar'], name='route_ws')
@@ -294,8 +276,7 @@ def test_websocket_named_route_with_subprotocols():
app.url_for('handler')
-def test_static_add_named_route():
- app = Sanic('test_static_add_route')
+def test_static_add_named_route(app):
async def handler1(request):
return text('OK1')
@@ -319,9 +300,7 @@ def test_static_add_named_route():
app.url_for('handler2')
-def test_dynamic_add_named_route():
- app = Sanic('test_dynamic_add_route')
-
+def test_dynamic_add_named_route(app):
results = []
async def handler(request, name):
@@ -335,8 +314,7 @@ def test_dynamic_add_named_route():
app.url_for('handler')
-def test_dynamic_add_named_route_unhashable():
- app = Sanic('test_dynamic_add_route_unhashable')
+def test_dynamic_add_named_route_unhashable(app):
async def handler(request, unhashable):
return text('OK')
@@ -351,8 +329,7 @@ def test_dynamic_add_named_route_unhashable():
app.url_for('handler')
-def test_overload_routes():
- app = Sanic('test_dynamic_route')
+def test_overload_routes(app):
@app.route('/overload', methods=['GET'], name='route_first')
async def handler1(request):
diff --git a/tests/test_payload_too_large.py b/tests/test_payload_too_large.py
index ecac605c..49ad5ab7 100644
--- a/tests/test_payload_too_large.py
+++ b/tests/test_payload_too_large.py
@@ -1,49 +1,45 @@
-from sanic import Sanic
from sanic.exceptions import PayloadTooLarge
from sanic.response import text
-def test_payload_too_large_from_error_handler():
- data_received_app = Sanic('data_received')
- data_received_app.config.REQUEST_MAX_SIZE = 1
+def test_payload_too_large_from_error_handler(app):
+ app.config.REQUEST_MAX_SIZE = 1
- @data_received_app.route('/1')
+ @app.route('/1')
async def handler1(request):
return text('OK')
- @data_received_app.exception(PayloadTooLarge)
+ @app.exception(PayloadTooLarge)
def handler_exception(request, exception):
return text('Payload Too Large from error_handler.', 413)
- response = data_received_app.test_client.get('/1', gather_request=False)
+ response = app.test_client.get('/1', gather_request=False)
assert response.status == 413
assert response.text == 'Payload Too Large from error_handler.'
-def test_payload_too_large_at_data_received_default():
- data_received_default_app = Sanic('data_received_default')
- data_received_default_app.config.REQUEST_MAX_SIZE = 1
+def test_payload_too_large_at_data_received_default(app):
+ app.config.REQUEST_MAX_SIZE = 1
- @data_received_default_app.route('/1')
+ @app.route('/1')
async def handler2(request):
return text('OK')
- response = data_received_default_app.test_client.get(
+ response = app.test_client.get(
'/1', gather_request=False)
assert response.status == 413
assert response.text == 'Error: Payload Too Large'
-def test_payload_too_large_at_on_header_default():
- on_header_default_app = Sanic('on_header')
- on_header_default_app.config.REQUEST_MAX_SIZE = 500
+def test_payload_too_large_at_on_header_default(app):
+ app.config.REQUEST_MAX_SIZE = 500
- @on_header_default_app.post('/1')
+ @app.post('/1')
async def handler3(request):
return text('OK')
data = 'a' * 1000
- response = on_header_default_app.test_client.post(
+ response = app.test_client.post(
'/1', gather_request=False, data=data)
assert response.status == 413
assert response.text == 'Error: Payload Too Large'
diff --git a/tests/test_redirect.py b/tests/test_redirect.py
index 5fdec2a6..f5efac60 100644
--- a/tests/test_redirect.py
+++ b/tests/test_redirect.py
@@ -1,12 +1,10 @@
import pytest
-from sanic import Sanic
from sanic.response import text, redirect
@pytest.fixture
-def redirect_app():
- app = Sanic('test_redirection')
+def redirect_app(app):
@app.route('/redirect_init')
async def redirect_init(request):
diff --git a/tests/test_request_data.py b/tests/test_request_data.py
index f795ff1f..69935fc0 100644
--- a/tests/test_request_data.py
+++ b/tests/test_request_data.py
@@ -1,6 +1,5 @@
import random
-from sanic import Sanic
from sanic.response import json
try:
@@ -9,8 +8,7 @@ except ImportError:
from json import loads
-def test_storage():
- app = Sanic('test_text')
+def test_storage(app):
@app.middleware('request')
def store(request):
@@ -29,8 +27,7 @@ def test_storage():
assert response_json.get('sidekick') is None
-def test_app_injection():
- app = Sanic('test_app_injection')
+def test_app_injection(app):
expected = random.choice(range(0, 100))
@app.listener('after_server_start')
diff --git a/tests/test_request_stream.py b/tests/test_request_stream.py
index b14aa519..97cd5a4a 100644
--- a/tests/test_request_stream.py
+++ b/tests/test_request_stream.py
@@ -1,5 +1,4 @@
import asyncio
-from sanic import Sanic
from sanic.blueprints import Blueprint
from sanic.views import CompositionView
from sanic.views import HTTPMethodView
@@ -9,11 +8,9 @@ from sanic.response import stream, text
data = "abc" * 100000
-def test_request_stream_method_view():
+def test_request_stream_method_view(app):
'''for self.is_request_stream = True'''
- app = Sanic('test_request_stream_method_view')
-
class SimpleView(HTTPMethodView):
def get(self, request):
@@ -44,11 +41,9 @@ def test_request_stream_method_view():
assert response.text == data
-def test_request_stream_app():
+def test_request_stream_app(app):
'''for self.is_request_stream = True and decorators'''
- app = Sanic('test_request_stream_app')
-
@app.get('/get')
async def get(request):
assert request.stream is None
@@ -163,11 +158,9 @@ def test_request_stream_app():
assert response.text == data
-def test_request_stream_handle_exception():
+def test_request_stream_handle_exception(app):
'''for handling exceptions properly'''
- app = Sanic('test_request_stream_exception')
-
@app.post('/post/', stream=True)
async def post(request, id):
assert isinstance(request.stream, asyncio.Queue)
@@ -191,10 +184,8 @@ def test_request_stream_handle_exception():
assert response.text == 'Error: Method GET not allowed for URL /post/random_id'
-def test_request_stream_blueprint():
+def test_request_stream_blueprint(app):
'''for self.is_request_stream = True'''
-
- app = Sanic('test_request_stream_blueprint')
bp = Blueprint('test_blueprint_request_stream_blueprint')
@app.get('/get')
@@ -313,11 +304,9 @@ def test_request_stream_blueprint():
assert response.text == data
-def test_request_stream_composition_view():
+def test_request_stream_composition_view(app):
'''for self.is_request_stream = True'''
- app = Sanic('test_request_stream__composition_view')
-
def get_handler(request):
assert request.stream is None
return text('OK')
@@ -348,11 +337,9 @@ def test_request_stream_composition_view():
assert response.text == data
-def test_request_stream():
+def test_request_stream(app):
'''test for complex application'''
-
bp = Blueprint('test_blueprint_request_stream')
- app = Sanic('test_request_stream')
class SimpleView(HTTPMethodView):
diff --git a/tests/test_requests.py b/tests/test_requests.py
index 2a91fb9b..9617216e 100644
--- a/tests/test_requests.py
+++ b/tests/test_requests.py
@@ -5,7 +5,6 @@ import ssl
import pytest
-from sanic import Sanic
from sanic.exceptions import ServerError
from sanic.response import json, text
from sanic.request import DEFAULT_HTTP_CONTENT_TYPE
@@ -16,8 +15,7 @@ from sanic.testing import HOST, PORT
# GET
# ------------------------------------------------------------ #
-def test_sync():
- app = Sanic('test_text')
+def test_sync(app):
@app.route('/')
def handler(request):
@@ -27,8 +25,8 @@ def test_sync():
assert response.text == 'Hello'
-def test_remote_address():
- app = Sanic('test_text')
+
+def test_remote_address(app):
@app.route('/')
def handler(request):
@@ -38,8 +36,8 @@ def test_remote_address():
assert response.text == '127.0.0.1'
-def test_text():
- app = Sanic('test_text')
+
+def test_text(app):
@app.route('/')
async def handler(request):
@@ -50,8 +48,7 @@ def test_text():
assert response.text == 'Hello'
-def test_headers():
- app = Sanic('test_text')
+def test_headers(app):
@app.route('/')
async def handler(request):
@@ -63,8 +60,7 @@ def test_headers():
assert response.headers.get('spam') == 'great'
-def test_non_str_headers():
- app = Sanic('test_text')
+def test_non_str_headers(app):
@app.route('/')
async def handler(request):
@@ -75,8 +71,8 @@ def test_non_str_headers():
assert response.headers.get('answer') == '42'
-def test_invalid_response():
- app = Sanic('test_invalid_response')
+
+def test_invalid_response(app):
@app.exception(ServerError)
def handler_exception(request, exception):
@@ -91,8 +87,7 @@ def test_invalid_response():
assert response.text == "Internal Server Error."
-def test_json():
- app = Sanic('test_json')
+def test_json(app):
@app.route('/')
async def handler(request):
@@ -105,8 +100,7 @@ def test_json():
assert results.get('test') == True
-def test_empty_json():
- app = Sanic('test_json')
+def test_empty_json(app):
@app.route('/')
async def handler(request):
@@ -118,8 +112,7 @@ def test_empty_json():
assert response.text == 'null'
-def test_invalid_json():
- app = Sanic('test_json')
+def test_invalid_json(app):
@app.route('/')
async def handler(request):
@@ -131,8 +124,7 @@ def test_invalid_json():
assert response.status == 400
-def test_query_string():
- app = Sanic('test_query_string')
+def test_query_string(app):
@app.route('/')
async def handler(request):
@@ -145,8 +137,7 @@ def test_query_string():
assert request.args.get('test2') == 'false'
-def test_uri_template():
- app = Sanic('test_uri_template')
+def test_uri_template(app):
@app.route('/foo//bar/')
async def handler(request):
@@ -156,8 +147,7 @@ def test_uri_template():
assert request.uri_template == '/foo//bar/'
-def test_token():
- app = Sanic('test_post_token')
+def test_token(app):
@app.route('/')
async def handler(request):
@@ -204,8 +194,7 @@ def test_token():
assert request.token is None
-def test_content_type():
- app = Sanic('test_content_type')
+def test_content_type(app):
@app.route('/')
async def handler(request):
@@ -223,8 +212,7 @@ def test_content_type():
assert response.text == 'application/json'
-def test_remote_addr():
- app = Sanic('test_content_type')
+def test_remote_addr(app):
@app.route('/')
async def handler(request):
@@ -249,8 +237,7 @@ def test_remote_addr():
assert response.text == '127.0.0.1'
-def test_match_info():
- app = Sanic('test_match_info')
+def test_match_info(app):
@app.route('/api/v1/user//')
async def handler(request, user_id):
@@ -266,8 +253,7 @@ def test_match_info():
# POST
# ------------------------------------------------------------ #
-def test_post_json():
- app = Sanic('test_post_json')
+def test_post_json(app):
@app.route('/', methods=['POST'])
async def handler(request):
@@ -283,8 +269,7 @@ def test_post_json():
assert response.text == 'OK'
-def test_post_form_urlencoded():
- app = Sanic('test_post_form_urlencoded')
+def test_post_form_urlencoded(app):
@app.route('/', methods=['POST'])
async def handler(request):
@@ -311,8 +296,7 @@ def test_post_form_urlencoded():
'OK\r\n' \
'------sanic--\r\n',
])
-def test_post_form_multipart_form_data(payload):
- app = Sanic('test_post_form_multipart_form_data')
+def test_post_form_multipart_form_data(app, payload):
@app.route('/', methods=['POST'])
async def handler(request):
@@ -331,8 +315,7 @@ def test_post_form_multipart_form_data(payload):
('/bar/baz', '', 'http://{}:{}/bar/baz'),
('/moo/boo', 'arg1=val1', 'http://{}:{}/moo/boo?arg1=val1')
])
-def test_url_attributes_no_ssl(path, query, expected_url):
- app = Sanic('test_url_attrs_no_ssl')
+def test_url_attributes_no_ssl(app, path, query, expected_url):
async def handler(request):
return text('OK')
@@ -356,9 +339,7 @@ def test_url_attributes_no_ssl(path, query, expected_url):
('/bar/baz', '', 'https://{}:{}/bar/baz'),
('/moo/boo', 'arg1=val1', 'https://{}:{}/moo/boo?arg1=val1')
])
-def test_url_attributes_with_ssl(path, query, expected_url):
- app = Sanic('test_url_attrs_with_ssl')
-
+def test_url_attributes_with_ssl(app, path, query, expected_url):
current_dir = os.path.dirname(os.path.realpath(__file__))
context = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain(
diff --git a/tests/test_response.py b/tests/test_response.py
index 6dcd2ea6..78f9f103 100644
--- a/tests/test_response.py
+++ b/tests/test_response.py
@@ -8,7 +8,6 @@ from urllib.parse import unquote
import pytest
from random import choice
-from sanic import Sanic
from sanic.response import HTTPResponse, stream, StreamingHTTPResponse, file, file_stream, json
from sanic.server import HttpProtocol
from sanic.testing import HOST, PORT
@@ -17,9 +16,8 @@ from unittest.mock import MagicMock
JSON_DATA = {'ok': True}
-def test_response_body_not_a_string():
+def test_response_body_not_a_string(app):
"""Test when a response body sent from the application is not a string"""
- app = Sanic('response_body_not_a_string')
random_num = choice(range(1000))
@app.route('/hello')
@@ -36,9 +34,7 @@ async def sample_streaming_fn(response):
await response.write('bar')
-
-def test_method_not_allowed():
- app = Sanic('method_not_allowed')
+def test_method_not_allowed(app):
@app.get('/')
async def test(request):
@@ -66,8 +62,8 @@ def test_method_not_allowed():
assert response.headers['Content-Length'] == '0'
-def test_response_header():
- app = Sanic('test_response_header')
+def test_response_header(app):
+
@app.get('/')
async def test(request):
return json({
@@ -86,8 +82,7 @@ def test_response_header():
@pytest.fixture
-def json_app():
- app = Sanic('json')
+def json_app(app):
@app.route("/")
async def test(request):
@@ -145,8 +140,7 @@ def test_no_content(json_app):
@pytest.fixture
-def streaming_app():
- app = Sanic('streaming')
+def streaming_app(app):
@app.route("/")
async def test(request):
@@ -240,8 +234,7 @@ def get_file_content(static_file_directory, file_name):
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt', 'python.png'])
@pytest.mark.parametrize('status', [200, 401])
-def test_file_response(file_name, static_file_directory, status):
- app = Sanic('test_file_helper')
+def test_file_response(app, file_name, static_file_directory, status):
@app.route('/files/', methods=['GET'])
def file_route(request, filename):
@@ -258,8 +251,7 @@ def test_file_response(file_name, static_file_directory, status):
@pytest.mark.parametrize('source,dest', [
('test.file', 'my_file.txt'), ('decode me.txt', 'readme.md'), ('python.png', 'logo.png')])
-def test_file_response_custom_filename(source, dest, static_file_directory):
- app = Sanic('test_file_helper')
+def test_file_response_custom_filename(app, source, dest, static_file_directory):
@app.route('/files/', methods=['GET'])
def file_route(request, filename):
@@ -274,8 +266,7 @@ def test_file_response_custom_filename(source, dest, static_file_directory):
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
-def test_file_head_response(file_name, static_file_directory):
- app = Sanic('test_file_helper')
+def test_file_head_response(app, file_name, static_file_directory):
@app.route('/files/', methods=['GET', 'HEAD'])
async def file_route(request, filename):
@@ -303,8 +294,7 @@ def test_file_head_response(file_name, static_file_directory):
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt', 'python.png'])
-def test_file_stream_response(file_name, static_file_directory):
- app = Sanic('test_file_helper')
+def test_file_stream_response(app, file_name, static_file_directory):
@app.route('/files/', methods=['GET'])
def file_route(request, filename):
@@ -321,8 +311,7 @@ def test_file_stream_response(file_name, static_file_directory):
@pytest.mark.parametrize('source,dest', [
('test.file', 'my_file.txt'), ('decode me.txt', 'readme.md'), ('python.png', 'logo.png')])
-def test_file_stream_response_custom_filename(source, dest, static_file_directory):
- app = Sanic('test_file_helper')
+def test_file_stream_response_custom_filename(app, source, dest, static_file_directory):
@app.route('/files/', methods=['GET'])
def file_route(request, filename):
@@ -337,8 +326,7 @@ def test_file_stream_response_custom_filename(source, dest, static_file_director
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
-def test_file_stream_head_response(file_name, static_file_directory):
- app = Sanic('test_file_helper')
+def test_file_stream_head_response(app, file_name, static_file_directory):
@app.route('/files/', methods=['GET', 'HEAD'])
async def file_route(request, filename):
diff --git a/tests/test_routes.py b/tests/test_routes.py
index 146db97c..d70bf975 100644
--- a/tests/test_routes.py
+++ b/tests/test_routes.py
@@ -12,9 +12,7 @@ from sanic.constants import HTTP_METHODS
# ------------------------------------------------------------ #
@pytest.mark.parametrize('method', HTTP_METHODS)
-def test_versioned_routes_get(method):
- app = Sanic('test_shorhand_routes_get')
-
+def test_versioned_routes_get(app, method):
method = method.lower()
func = getattr(app, method)
@@ -32,8 +30,7 @@ def test_versioned_routes_get(method):
assert response.status == 200
-def test_shorthand_routes_get():
- app = Sanic('test_shorhand_routes_get')
+def test_shorthand_routes_get(app):
@app.get('/get')
def handler(request):
@@ -46,8 +43,7 @@ def test_shorthand_routes_get():
assert response.status == 405
-def test_shorthand_routes_multiple():
- app = Sanic('test_shorthand_routes_multiple')
+def test_shorthand_routes_multiple(app):
@app.get('/get')
def get_handler(request):
@@ -65,8 +61,7 @@ def test_shorthand_routes_multiple():
assert response.status == 200
-def test_route_strict_slash():
- app = Sanic('test_route_strict_slash')
+def test_route_strict_slash(app):
@app.get('/get', strict_slashes=True)
def handler(request):
@@ -93,9 +88,8 @@ def test_route_strict_slash():
assert response.status == 404
-def test_route_invalid_parameter_syntax():
+def test_route_invalid_parameter_syntax(app):
with pytest.raises(ValueError):
- app = Sanic('test_route_invalid_param_syntax')
@app.get('/get/<:string>', strict_slashes=True)
def handler(request):
@@ -115,8 +109,7 @@ def test_route_strict_slash_default_value():
assert response.status == 404
-def test_route_strict_slash_without_passing_default_value():
- app = Sanic('test_route_strict_slash')
+def test_route_strict_slash_without_passing_default_value(app):
@app.get('/get')
def handler(request):
@@ -137,8 +130,7 @@ def test_route_strict_slash_default_value_can_be_overwritten():
assert response.text == 'OK'
-def test_route_slashes_overload():
- app = Sanic('test_route_slashes_overload')
+def test_route_slashes_overload(app):
@app.get('/hello/')
def handler(request):
@@ -161,8 +153,7 @@ def test_route_slashes_overload():
assert response.text == 'OK'
-def test_route_optional_slash():
- app = Sanic('test_route_optional_slash')
+def test_route_optional_slash(app):
@app.get('/get')
def handler(request):
@@ -174,9 +165,8 @@ def test_route_optional_slash():
request, response = app.test_client.get('/get/')
assert response.text == 'OK'
-def test_route_strict_slashes_set_to_false_and_host_is_a_list():
+def test_route_strict_slashes_set_to_false_and_host_is_a_list(app):
#Part of regression test for issue #1120
- app = Sanic('test_route_strict_slashes_set_to_false_and_host_is_a_list')
site1 = 'localhost:{}'.format(app.test_client.port)
@@ -209,8 +199,7 @@ def test_route_strict_slashes_set_to_false_and_host_is_a_list():
request, response = app.test_client.delete('http://' + site1 +'/delete')
assert response.text == 'OK'
-def test_shorthand_routes_post():
- app = Sanic('test_shorhand_routes_post')
+def test_shorthand_routes_post(app):
@app.post('/post')
def handler(request):
@@ -223,8 +212,7 @@ def test_shorthand_routes_post():
assert response.status == 405
-def test_shorthand_routes_put():
- app = Sanic('test_shorhand_routes_put')
+def test_shorthand_routes_put(app):
@app.put('/put')
def handler(request):
@@ -240,8 +228,7 @@ def test_shorthand_routes_put():
assert response.status == 405
-def test_shorthand_routes_delete():
- app = Sanic('test_shorhand_routes_delete')
+def test_shorthand_routes_delete(app):
@app.delete('/delete')
def handler(request):
@@ -257,8 +244,7 @@ def test_shorthand_routes_delete():
assert response.status == 405
-def test_shorthand_routes_patch():
- app = Sanic('test_shorhand_routes_patch')
+def test_shorthand_routes_patch(app):
@app.patch('/patch')
def handler(request):
@@ -274,8 +260,7 @@ def test_shorthand_routes_patch():
assert response.status == 405
-def test_shorthand_routes_head():
- app = Sanic('test_shorhand_routes_head')
+def test_shorthand_routes_head(app):
@app.head('/head')
def handler(request):
@@ -291,8 +276,7 @@ def test_shorthand_routes_head():
assert response.status == 405
-def test_shorthand_routes_options():
- app = Sanic('test_shorhand_routes_options')
+def test_shorthand_routes_options(app):
@app.options('/options')
def handler(request):
@@ -308,8 +292,7 @@ def test_shorthand_routes_options():
assert response.status == 405
-def test_static_routes():
- app = Sanic('test_dynamic_route')
+def test_static_routes(app):
@app.route('/test')
async def handler1(request):
@@ -326,9 +309,7 @@ def test_static_routes():
assert response.text == 'OK2'
-def test_dynamic_route():
- app = Sanic('test_dynamic_route')
-
+def test_dynamic_route(app):
results = []
@app.route('/folder/')
@@ -342,9 +323,7 @@ def test_dynamic_route():
assert results[0] == 'test123'
-def test_dynamic_route_string():
- app = Sanic('test_dynamic_route_string')
-
+def test_dynamic_route_string(app):
results = []
@app.route('/folder/')
@@ -363,9 +342,7 @@ def test_dynamic_route_string():
assert results[1] == 'favicon.ico'
-def test_dynamic_route_int():
- app = Sanic('test_dynamic_route_int')
-
+def test_dynamic_route_int(app):
results = []
@app.route('/folder/')
@@ -381,9 +358,7 @@ def test_dynamic_route_int():
assert response.status == 404
-def test_dynamic_route_number():
- app = Sanic('test_dynamic_route_number')
-
+def test_dynamic_route_number(app):
results = []
@app.route('/weight/')
@@ -402,8 +377,7 @@ def test_dynamic_route_number():
assert response.status == 404
-def test_dynamic_route_regex():
- app = Sanic('test_dynamic_route_regex')
+def test_dynamic_route_regex(app):
@app.route('/folder/')
async def handler(request, folder_id):
@@ -422,9 +396,8 @@ def test_dynamic_route_regex():
assert response.status == 200
-def test_dynamic_route_uuid():
+def test_dynamic_route_uuid(app):
import uuid
- app = Sanic('test_dynamic_route_uuid')
results = []
@@ -444,8 +417,7 @@ def test_dynamic_route_uuid():
assert response.status == 404
-def test_dynamic_route_path():
- app = Sanic('test_dynamic_route_path')
+def test_dynamic_route_path(app):
@app.route('//info')
async def handler(request, path):
@@ -468,8 +440,7 @@ def test_dynamic_route_path():
assert response.status == 200
-def test_dynamic_route_unhashable():
- app = Sanic('test_dynamic_route_unhashable')
+def test_dynamic_route_unhashable(app):
@app.route('/folder//end/')
async def handler(request, unhashable):
@@ -488,8 +459,7 @@ def test_dynamic_route_unhashable():
assert response.status == 404
-def test_websocket_route():
- app = Sanic('test_websocket_route')
+def test_websocket_route(app):
ev = asyncio.Event()
@app.websocket('/ws')
@@ -506,8 +476,7 @@ def test_websocket_route():
assert ev.is_set()
-def test_websocket_route_with_subprotocols():
- app = Sanic('test_websocket_route')
+def test_websocket_route_with_subprotocols(app):
results = []
@app.websocket('/ws', subprotocols=['foo', 'bar'])
@@ -548,8 +517,7 @@ def test_websocket_route_with_subprotocols():
assert results == ['bar', 'bar', None, None]
-def test_route_duplicate():
- app = Sanic('test_route_duplicate')
+def test_route_duplicate(app):
with pytest.raises(RouteExists):
@app.route('/test')
@@ -570,8 +538,7 @@ def test_route_duplicate():
pass
-def test_method_not_allowed():
- app = Sanic('test_method_not_allowed')
+def test_method_not_allowed(app):
@app.route('/test', methods=['GET'])
async def handler(request):
@@ -584,8 +551,7 @@ def test_method_not_allowed():
assert response.status == 405
-def test_static_add_route():
- app = Sanic('test_static_add_route')
+def test_static_add_route(app):
async def handler1(request):
return text('OK1')
@@ -603,8 +569,7 @@ def test_static_add_route():
assert response.text == 'OK2'
-def test_dynamic_add_route():
- app = Sanic('test_dynamic_add_route')
+def test_dynamic_add_route(app):
results = []
@@ -619,8 +584,7 @@ def test_dynamic_add_route():
assert results[0] == 'test123'
-def test_dynamic_add_route_string():
- app = Sanic('test_dynamic_add_route_string')
+def test_dynamic_add_route_string(app):
results = []
@@ -640,9 +604,7 @@ def test_dynamic_add_route_string():
assert results[1] == 'favicon.ico'
-def test_dynamic_add_route_int():
- app = Sanic('test_dynamic_add_route_int')
-
+def test_dynamic_add_route_int(app):
results = []
async def handler(request, folder_id):
@@ -659,9 +621,7 @@ def test_dynamic_add_route_int():
assert response.status == 404
-def test_dynamic_add_route_number():
- app = Sanic('test_dynamic_add_route_number')
-
+def test_dynamic_add_route_number(app):
results = []
async def handler(request, weight):
@@ -681,8 +641,7 @@ def test_dynamic_add_route_number():
assert response.status == 404
-def test_dynamic_add_route_regex():
- app = Sanic('test_dynamic_route_int')
+def test_dynamic_add_route_regex(app):
async def handler(request, folder_id):
return text('OK')
@@ -702,8 +661,7 @@ def test_dynamic_add_route_regex():
assert response.status == 200
-def test_dynamic_add_route_unhashable():
- app = Sanic('test_dynamic_add_route_unhashable')
+def test_dynamic_add_route_unhashable(app):
async def handler(request, unhashable):
return text('OK')
@@ -723,8 +681,7 @@ def test_dynamic_add_route_unhashable():
assert response.status == 404
-def test_add_route_duplicate():
- app = Sanic('test_add_route_duplicate')
+def test_add_route_duplicate(app):
with pytest.raises(RouteExists):
async def handler1(request):
@@ -747,8 +704,7 @@ def test_add_route_duplicate():
app.add_route(handler2, '/test//')
-def test_add_route_method_not_allowed():
- app = Sanic('test_add_route_method_not_allowed')
+def test_add_route_method_not_allowed(app):
async def handler(request):
return text('OK')
@@ -762,8 +718,7 @@ def test_add_route_method_not_allowed():
assert response.status == 405
-def test_remove_static_route():
- app = Sanic('test_remove_static_route')
+def test_remove_static_route(app):
async def handler1(request):
return text('OK1')
@@ -790,8 +745,7 @@ def test_remove_static_route():
assert response.status == 404
-def test_remove_dynamic_route():
- app = Sanic('test_remove_dynamic_route')
+def test_remove_dynamic_route(app):
async def handler(request, name):
return text('OK')
@@ -806,15 +760,13 @@ def test_remove_dynamic_route():
assert response.status == 404
-def test_remove_inexistent_route():
- app = Sanic('test_remove_inexistent_route')
+def test_remove_inexistent_route(app):
with pytest.raises(RouteDoesNotExist):
app.remove_route('/test')
-def test_removing_slash():
- app = Sanic(__name__)
+def test_removing_slash(app):
@app.get('/rest/')
def get(_):
@@ -827,8 +779,7 @@ def test_removing_slash():
assert len(app.router.routes_all.keys()) == 2
-def test_remove_unhashable_route():
- app = Sanic('test_remove_unhashable_route')
+def test_remove_unhashable_route(app):
async def handler(request, unhashable):
return text('OK')
@@ -856,8 +807,7 @@ def test_remove_unhashable_route():
assert response.status == 404
-def test_remove_route_without_clean_cache():
- app = Sanic('test_remove_static_route')
+def test_remove_route_without_clean_cache(app):
async def handler(request):
return text('OK')
@@ -884,8 +834,7 @@ def test_remove_route_without_clean_cache():
assert response.status == 200
-def test_overload_routes():
- app = Sanic('test_dynamic_route')
+def test_overload_routes(app):
@app.route('/overload', methods=['GET'])
async def handler1(request):
@@ -913,8 +862,7 @@ def test_overload_routes():
return text('Duplicated')
-def test_unmergeable_overload_routes():
- app = Sanic('test_dynamic_route')
+def test_unmergeable_overload_routes(app):
@app.route('/overload_whole', methods=None)
async def handler1(request):
@@ -947,8 +895,7 @@ def test_unmergeable_overload_routes():
assert response.status == 405
-def test_unicode_routes():
- app = Sanic('test_unicode_routes')
+def test_unicode_routes(app):
@app.get('/你好')
def handler1(request):
@@ -965,8 +912,7 @@ def test_unicode_routes():
assert response.text == 'OK2 你好'
-def test_uri_with_different_method_and_different_params():
- app = Sanic('test_uri')
+def test_uri_with_different_method_and_different_params(app):
@app.route('/ads/', methods=['GET'])
async def ad_get(request, ad_id):
diff --git a/tests/test_server_events.py b/tests/test_server_events.py
index c2ccc7dc..68e097eb 100644
--- a/tests/test_server_events.py
+++ b/tests/test_server_events.py
@@ -1,11 +1,7 @@
-from io import StringIO
-from random import choice
-from string import ascii_letters
import signal
import pytest
-from sanic import Sanic
from sanic.testing import HOST, PORT
AVAILABLE_LISTENERS = [
@@ -37,54 +33,46 @@ def start_stop_app(random_name_app, **run_kwargs):
@pytest.mark.parametrize('listener_name', AVAILABLE_LISTENERS)
-def test_single_listener(listener_name):
+def test_single_listener(app, listener_name):
"""Test that listeners on their own work"""
- random_name_app = Sanic(''.join(
- [choice(ascii_letters) for _ in range(choice(range(5, 10)))]))
- output = list()
+ output = []
# Register listener
- random_name_app.listener(listener_name)(
+ app.listener(listener_name)(
create_listener(listener_name, output))
- start_stop_app(random_name_app)
- assert random_name_app.name + listener_name == output.pop()
+ start_stop_app(app)
+ assert app.name + listener_name == output.pop()
@pytest.mark.parametrize('listener_name', AVAILABLE_LISTENERS)
-def test_register_listener(listener_name):
+def test_register_listener(app, listener_name):
"""
Test that listeners on their own work with
app.register_listener method
"""
- random_name_app = Sanic(''.join(
- [choice(ascii_letters) for _ in range(choice(range(5, 10)))]))
- output = list()
+ output = []
# Register listener
listener = create_listener(listener_name, output)
- random_name_app.register_listener(listener,
+ app.register_listener(listener,
event=listener_name)
- start_stop_app(random_name_app)
- assert random_name_app.name + listener_name == output.pop()
+ start_stop_app(app)
+ assert app.name + listener_name == output.pop()
-def test_all_listeners():
- random_name_app = Sanic(''.join(
- [choice(ascii_letters) for _ in range(choice(range(5, 10)))]))
- output = list()
+def test_all_listeners(app):
+ output = []
for listener_name in AVAILABLE_LISTENERS:
listener = create_listener(listener_name, output)
- random_name_app.listener(listener_name)(listener)
- start_stop_app(random_name_app)
+ app.listener(listener_name)(listener)
+ start_stop_app(app)
for listener_name in AVAILABLE_LISTENERS:
- assert random_name_app.name + listener_name == output.pop()
+ assert app.name + listener_name == output.pop()
-async def test_trigger_before_events_create_server():
+async def test_trigger_before_events_create_server(app):
class MySanicDb:
pass
- app = Sanic("test_sanic_app")
-
@app.listener('before_server_start')
async def init_db(app, loop):
app.db = MySanicDb()
diff --git a/tests/test_signal_handlers.py b/tests/test_signal_handlers.py
index bee4f8e7..7c1327c2 100644
--- a/tests/test_signal_handlers.py
+++ b/tests/test_signal_handlers.py
@@ -1,4 +1,3 @@
-from sanic import Sanic
from sanic.response import HTTPResponse
from sanic.testing import HOST, PORT
from unittest.mock import MagicMock
@@ -18,9 +17,8 @@ def set_loop(app, loop):
def after(app, loop):
calledq.put(loop.add_signal_handler.called)
-def test_register_system_signals():
+def test_register_system_signals(app):
"""Test if sanic register system signals"""
- app = Sanic('test_register_system_signals')
@app.route('/hello')
async def hello_route(request):
@@ -34,9 +32,8 @@ def test_register_system_signals():
assert calledq.get() == True
-def test_dont_register_system_signals():
+def test_dont_register_system_signals(app):
"""Test if sanic don't register system signals"""
- app = Sanic('test_register_system_signals')
@app.route('/hello')
async def hello_route(request):
diff --git a/tests/test_static.py b/tests/test_static.py
index 3335e248..e436aa74 100644
--- a/tests/test_static.py
+++ b/tests/test_static.py
@@ -3,8 +3,6 @@ import os
import pytest
-from sanic import Sanic
-
@pytest.fixture(scope='module')
def static_file_directory():
@@ -26,8 +24,7 @@ def get_file_content(static_file_directory, file_name):
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt', 'python.png'])
-def test_static_file(static_file_directory, file_name):
- app = Sanic('test_static')
+def test_static_file(app, static_file_directory, file_name):
app.static(
'/testing.file', get_file_path(static_file_directory, file_name))
@@ -37,8 +34,7 @@ def test_static_file(static_file_directory, file_name):
@pytest.mark.parametrize('file_name', ['test.html'])
-def test_static_file_content_type(static_file_directory, file_name):
- app = Sanic('test_static')
+def test_static_file_content_type(app, static_file_directory, file_name):
app.static(
'/testing.file',
get_file_path(static_file_directory, file_name),
@@ -53,9 +49,7 @@ def test_static_file_content_type(static_file_directory, file_name):
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
@pytest.mark.parametrize('base_uri', ['/static', '', '/dir'])
-def test_static_directory(file_name, base_uri, static_file_directory):
-
- app = Sanic('test_static')
+def test_static_directory(app, file_name, base_uri, static_file_directory):
app.static(base_uri, static_file_directory)
request, response = app.test_client.get(
@@ -65,8 +59,7 @@ def test_static_directory(file_name, base_uri, static_file_directory):
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
-def test_static_head_request(file_name, static_file_directory):
- app = Sanic('test_static')
+def test_static_head_request(app, file_name, static_file_directory):
app.static(
'/testing.file', get_file_path(static_file_directory, file_name),
use_content_range=True)
@@ -81,8 +74,7 @@ def test_static_head_request(file_name, static_file_directory):
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
-def test_static_content_range_correct(file_name, static_file_directory):
- app = Sanic('test_static')
+def test_static_content_range_correct(app, file_name, static_file_directory):
app.static(
'/testing.file', get_file_path(static_file_directory, file_name),
use_content_range=True)
@@ -102,8 +94,7 @@ def test_static_content_range_correct(file_name, static_file_directory):
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
-def test_static_content_range_front(file_name, static_file_directory):
- app = Sanic('test_static')
+def test_static_content_range_front(app, file_name, static_file_directory):
app.static(
'/testing.file', get_file_path(static_file_directory, file_name),
use_content_range=True)
@@ -123,8 +114,7 @@ def test_static_content_range_front(file_name, static_file_directory):
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
-def test_static_content_range_back(file_name, static_file_directory):
- app = Sanic('test_static')
+def test_static_content_range_back(app, file_name, static_file_directory):
app.static(
'/testing.file', get_file_path(static_file_directory, file_name),
use_content_range=True)
@@ -144,8 +134,7 @@ def test_static_content_range_back(file_name, static_file_directory):
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
-def test_static_content_range_empty(file_name, static_file_directory):
- app = Sanic('test_static')
+def test_static_content_range_empty(app, file_name, static_file_directory):
app.static(
'/testing.file', get_file_path(static_file_directory, file_name),
use_content_range=True)
@@ -161,8 +150,7 @@ def test_static_content_range_empty(file_name, static_file_directory):
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
-def test_static_content_range_error(file_name, static_file_directory):
- app = Sanic('test_static')
+def test_static_content_range_error(app, file_name, static_file_directory):
app.static(
'/testing.file', get_file_path(static_file_directory, file_name),
use_content_range=True)
@@ -179,8 +167,7 @@ def test_static_content_range_error(file_name, static_file_directory):
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt', 'python.png'])
-def test_static_file_specified_host(static_file_directory, file_name):
- app = Sanic('test_static')
+def test_static_file_specified_host(app, static_file_directory, file_name):
app.static(
'/testing.file',
get_file_path(static_file_directory, file_name),
diff --git a/tests/test_url_building.py b/tests/test_url_building.py
index 98bbc20a..1768f78f 100644
--- a/tests/test_url_building.py
+++ b/tests/test_url_building.py
@@ -1,7 +1,6 @@
import pytest as pytest
from urllib.parse import urlsplit, parse_qsl
-from sanic import Sanic
from sanic.response import text
from sanic.views import HTTPMethodView
from sanic.blueprints import Blueprint
@@ -30,8 +29,7 @@ def _generate_handlers_from_names(app, l):
@pytest.fixture
-def simple_app():
- app = Sanic('simple_app')
+def simple_app(app):
handler_names = list(string.ascii_letters)
_generate_handlers_from_names(app, handler_names)
@@ -54,8 +52,7 @@ def test_simple_url_for_getting(simple_app):
(URL_FOR_ARGS2, URL_FOR_VALUE2),
(URL_FOR_ARGS3, URL_FOR_VALUE3),
(URL_FOR_ARGS4, URL_FOR_VALUE4)])
-def test_simple_url_for_getting_with_more_params(args, url):
- app = Sanic('more_url_build')
+def test_simple_url_for_getting_with_more_params(app, args, url):
@app.route('/myurl')
def passes(request):
@@ -67,8 +64,7 @@ def test_simple_url_for_getting_with_more_params(args, url):
assert response.text == 'this should pass'
-def test_fails_if_endpoint_not_found():
- app = Sanic('fail_url_build')
+def test_fails_if_endpoint_not_found(app):
@app.route('/fail')
def fail(request):
@@ -80,14 +76,12 @@ def test_fails_if_endpoint_not_found():
assert str(e.value) == 'Endpoint with name `passes` was not found'
-def test_fails_url_build_if_param_not_passed():
+def test_fails_url_build_if_param_not_passed(app):
url = '/'
for letter in string.ascii_letters:
url += '<{}>/'.format(letter)
- app = Sanic('fail_url_build')
-
@app.route(url)
def fail(request):
return text('this should fail')
@@ -103,8 +97,7 @@ def test_fails_url_build_if_param_not_passed():
assert 'Required parameter `Z` was not passed to url_for' in str(e.value)
-def test_fails_url_build_if_params_not_passed():
- app = Sanic('fail_url_build')
+def test_fails_url_build_if_params_not_passed(app):
@app.route('/fail')
def fail(request):
@@ -126,8 +119,7 @@ PASSING_KWARGS = {
EXPECTED_BUILT_URL = '/4/woof/ba/normal/1.001'
-def test_fails_with_int_message():
- app = Sanic('fail_url_build')
+def test_fails_with_int_message(app):
@app.route(COMPLEX_PARAM_URL)
def fail(request):
@@ -145,8 +137,7 @@ def test_fails_with_int_message():
assert str(e.value) == expected_error
-def test_fails_with_two_letter_string_message():
- app = Sanic('fail_url_build')
+def test_fails_with_two_letter_string_message(app):
@app.route(COMPLEX_PARAM_URL)
def fail(request):
@@ -165,8 +156,7 @@ def test_fails_with_two_letter_string_message():
assert str(e.value) == expected_error
-def test_fails_with_number_message():
- app = Sanic('fail_url_build')
+def test_fails_with_number_message(app):
@app.route(COMPLEX_PARAM_URL)
def fail(request):
@@ -185,8 +175,7 @@ def test_fails_with_number_message():
assert str(e.value) == expected_error
-def test_adds_other_supplied_values_as_query_string():
- app = Sanic('passes')
+def test_adds_other_supplied_values_as_query_string(app):
@app.route(COMPLEX_PARAM_URL)
def passes(request):
@@ -205,8 +194,7 @@ def test_adds_other_supplied_values_as_query_string():
@pytest.fixture
-def blueprint_app():
- app = Sanic('blueprints')
+def blueprint_app(app):
first_print = Blueprint('first', url_prefix='/first')
second_print = Blueprint('second', url_prefix='/second')
@@ -252,8 +240,7 @@ def test_blueprints_work_with_params(blueprint_app):
@pytest.fixture
-def methodview_app():
- app = Sanic('methodview')
+def methodview_app(app):
class ViewOne(HTTPMethodView):
def get(self, request):
diff --git a/tests/test_url_for_static.py b/tests/test_url_for_static.py
index d1d8fc9b..2e802700 100644
--- a/tests/test_url_for_static.py
+++ b/tests/test_url_for_static.py
@@ -3,7 +3,6 @@ import os
import pytest
-from sanic import Sanic
from sanic.blueprints import Blueprint
@@ -27,8 +26,7 @@ def get_file_content(static_file_directory, file_name):
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt', 'python.png'])
-def test_static_file(static_file_directory, file_name):
- app = Sanic('test_static')
+def test_static_file(app, static_file_directory, file_name):
app.static(
'/testing.file', get_file_path(static_file_directory, file_name))
app.static(
@@ -102,9 +100,7 @@ def test_static_file(static_file_directory, file_name):
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
@pytest.mark.parametrize('base_uri', ['/static', '', '/dir'])
-def test_static_directory(file_name, base_uri, static_file_directory):
-
- app = Sanic('test_static')
+def test_static_directory(app, file_name, base_uri, static_file_directory):
app.static(base_uri, static_file_directory)
base_uri2 = base_uri + '/2'
app.static(base_uri2, static_file_directory, name='uploads')
@@ -156,10 +152,8 @@ def test_static_directory(file_name, base_uri, static_file_directory):
assert response.body == get_file_content(static_file_directory, file_name)
-
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
-def test_static_head_request(file_name, static_file_directory):
- app = Sanic('test_static')
+def test_static_head_request(app, file_name, static_file_directory):
app.static(
'/testing.file', get_file_path(static_file_directory, file_name),
use_content_range=True)
@@ -198,8 +192,7 @@ def test_static_head_request(file_name, static_file_directory):
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
-def test_static_content_range_correct(file_name, static_file_directory):
- app = Sanic('test_static')
+def test_static_content_range_correct(app, file_name, static_file_directory):
app.static(
'/testing.file', get_file_path(static_file_directory, file_name),
use_content_range=True)
@@ -250,8 +243,7 @@ def test_static_content_range_correct(file_name, static_file_directory):
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
-def test_static_content_range_front(file_name, static_file_directory):
- app = Sanic('test_static')
+def test_static_content_range_front(app, file_name, static_file_directory):
app.static(
'/testing.file', get_file_path(static_file_directory, file_name),
use_content_range=True)
@@ -302,8 +294,7 @@ def test_static_content_range_front(file_name, static_file_directory):
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
-def test_static_content_range_back(file_name, static_file_directory):
- app = Sanic('test_static')
+def test_static_content_range_back(app, file_name, static_file_directory):
app.static(
'/testing.file', get_file_path(static_file_directory, file_name),
use_content_range=True)
@@ -354,8 +345,7 @@ def test_static_content_range_back(file_name, static_file_directory):
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
-def test_static_content_range_empty(file_name, static_file_directory):
- app = Sanic('test_static')
+def test_static_content_range_empty(app, file_name, static_file_directory):
app.static(
'/testing.file', get_file_path(static_file_directory, file_name),
use_content_range=True)
@@ -401,8 +391,7 @@ def test_static_content_range_empty(file_name, static_file_directory):
@pytest.mark.parametrize('file_name', ['test.file', 'decode me.txt'])
-def test_static_content_range_error(file_name, static_file_directory):
- app = Sanic('test_static')
+def test_static_content_range_error(app, file_name, static_file_directory):
app.static(
'/testing.file', get_file_path(static_file_directory, file_name),
use_content_range=True)
diff --git a/tests/test_utf8.py b/tests/test_utf8.py
index e1602958..4a8b9e73 100644
--- a/tests/test_utf8.py
+++ b/tests/test_utf8.py
@@ -1,14 +1,12 @@
-from json import loads as json_loads, dumps as json_dumps
-from sanic import Sanic
-from sanic.response import json, text
+from json import dumps as json_dumps
+from sanic.response import text
# ------------------------------------------------------------ #
# UTF-8
# ------------------------------------------------------------ #
-def test_utf8_query_string():
- app = Sanic('test_utf8_query_string')
+def test_utf8_query_string(app):
@app.route('/')
async def handler(request):
@@ -18,8 +16,7 @@ def test_utf8_query_string():
assert request.args.get('utf8') == '✓'
-def test_utf8_response():
- app = Sanic('test_utf8_response')
+def test_utf8_response(app):
@app.route('/')
async def handler(request):
@@ -29,8 +26,7 @@ def test_utf8_response():
assert response.text == '✓'
-def skip_test_utf8_route():
- app = Sanic('skip_test_utf8_route')
+def skip_test_utf8_route(app):
@app.route('/')
async def handler(request):
@@ -41,8 +37,7 @@ def skip_test_utf8_route():
assert response.text == 'OK'
-def test_utf8_post_json():
- app = Sanic('test_utf8_post_json')
+def test_utf8_post_json(app):
@app.route('/')
async def handler(request):
diff --git a/tests/test_vhosts.py b/tests/test_vhosts.py
index 2b88bff3..0be537e6 100644
--- a/tests/test_vhosts.py
+++ b/tests/test_vhosts.py
@@ -1,9 +1,7 @@
-from sanic import Sanic
-from sanic.response import json, text
+from sanic.response import text
-def test_vhosts():
- app = Sanic('test_vhosts')
+def test_vhosts(app):
@app.route('/', host="example.com")
async def handler(request):
@@ -22,8 +20,7 @@ def test_vhosts():
assert response.text == "You're at subdomain.example.com!"
-def test_vhosts_with_list():
- app = Sanic('test_vhosts')
+def test_vhosts_with_list(app):
@app.route('/', host=["hello.com", "world.com"])
async def handler(request):
@@ -37,8 +34,8 @@ def test_vhosts_with_list():
request, response = app.test_client.get('/', headers=headers)
assert response.text == "Hello, world!"
-def test_vhosts_with_defaults():
- app = Sanic('test_vhosts')
+
+def test_vhosts_with_defaults(app):
@app.route('/', host="hello.com")
async def handler(request):
diff --git a/tests/test_views.py b/tests/test_views.py
index 71d32a7f..5d339f55 100644
--- a/tests/test_views.py
+++ b/tests/test_views.py
@@ -1,6 +1,5 @@
import pytest as pytest
-from sanic import Sanic
from sanic.exceptions import InvalidUsage
from sanic.response import text, HTTPResponse
from sanic.views import HTTPMethodView, CompositionView
@@ -10,8 +9,7 @@ from sanic.constants import HTTP_METHODS
@pytest.mark.parametrize('method', HTTP_METHODS)
-def test_methods(method):
- app = Sanic('test_methods')
+def test_methods(app, method):
class DummyView(HTTPMethodView):
@@ -44,8 +42,7 @@ def test_methods(method):
assert response.headers['method'] == method
-def test_unexisting_methods():
- app = Sanic('test_unexisting_methods')
+def test_unexisting_methods(app):
class DummyView(HTTPMethodView):
@@ -59,8 +56,7 @@ def test_unexisting_methods():
assert response.text == 'Error: Method POST not allowed for URL /'
-def test_argument_methods():
- app = Sanic('test_argument_methods')
+def test_argument_methods(app):
class DummyView(HTTPMethodView):
@@ -74,8 +70,7 @@ def test_argument_methods():
assert response.text == 'I am get method with test123'
-def test_with_bp():
- app = Sanic('test_with_bp')
+def test_with_bp(app):
bp = Blueprint('test_text')
class DummyView(HTTPMethodView):
@@ -93,8 +88,7 @@ def test_with_bp():
assert response.text == 'I am get method'
-def test_with_bp_with_url_prefix():
- app = Sanic('test_with_bp_with_url_prefix')
+def test_with_bp_with_url_prefix(app):
bp = Blueprint('test_text', url_prefix='/test1')
class DummyView(HTTPMethodView):
@@ -110,8 +104,7 @@ def test_with_bp_with_url_prefix():
assert response.text == 'I am get method'
-def test_with_middleware():
- app = Sanic('test_with_middleware')
+def test_with_middleware(app):
class DummyView(HTTPMethodView):
@@ -132,9 +125,7 @@ def test_with_middleware():
assert type(results[0]) is Request
-def test_with_middleware_response():
- app = Sanic('test_with_middleware_response')
-
+def test_with_middleware_response(app):
results = []
@app.middleware('request')
@@ -161,8 +152,7 @@ def test_with_middleware_response():
assert isinstance(results[2], HTTPResponse)
-def test_with_custom_class_methods():
- app = Sanic('test_with_custom_class_methods')
+def test_with_custom_class_methods(app):
class DummyView(HTTPMethodView):
global_var = 0
@@ -179,9 +169,7 @@ def test_with_custom_class_methods():
assert response.text == 'I am get method and global var is 10'
-def test_with_decorator():
- app = Sanic('test_with_decorator')
-
+def test_with_decorator(app):
results = []
def stupid_decorator(view):
@@ -227,9 +215,7 @@ def test_composition_view_rejects_duplicate_methods():
@pytest.mark.parametrize('method', HTTP_METHODS)
-def test_composition_view_runs_methods_as_expected(method):
- app = Sanic('test_composition_view')
-
+def test_composition_view_runs_methods_as_expected(app, method):
view = CompositionView()
def first(request):
@@ -251,9 +237,7 @@ def test_composition_view_runs_methods_as_expected(method):
@pytest.mark.parametrize('method', HTTP_METHODS)
-def test_composition_view_rejects_invalid_methods(method):
- app = Sanic('test_composition_view')
-
+def test_composition_view_rejects_invalid_methods(app, method):
view = CompositionView()
view.add(['GET', 'POST', 'PUT'], lambda x: text('first method'))