Skip to content

Commit

Permalink
Merge pull request #1305 from Stranger6667/app-fixture
Browse files Browse the repository at this point in the history
Reuse app fixture in tests
  • Loading branch information
seemethere committed Sep 27, 2018
2 parents 04b8dd9 + f0e1624 commit 076cf51
Show file tree
Hide file tree
Showing 28 changed files with 253 additions and 510 deletions.
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pytest

from sanic import Sanic


@pytest.fixture
def app(request):
return Sanic(request.node.name)
4 changes: 1 addition & 3 deletions tests/test_bad_request.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
55 changes: 18 additions & 37 deletions tests/test_blueprints.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
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

Expand All @@ -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()
Expand All @@ -46,8 +44,7 @@ def handler(request):
assert response.status == 200


def test_bp():
app = Sanic('test_text')
def test_bp(app):
bp = Blueprint('test_text')

@bp.route('/')
Expand All @@ -60,8 +57,7 @@ def handler(request):

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)
Expand All @@ -87,8 +83,7 @@ def handler(request):
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')
Expand All @@ -107,8 +102,7 @@ def handler(request):
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')
Expand All @@ -127,8 +121,7 @@ def handler(request):
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)
Expand All @@ -147,8 +140,7 @@ def handler(request):
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('/')
Expand All @@ -161,8 +153,7 @@ def handler(request):
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')

Expand All @@ -182,8 +173,7 @@ def handler2(request):
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('/')
Expand All @@ -209,8 +199,7 @@ def handler(request):
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")
Expand Down Expand Up @@ -253,8 +242,7 @@ def handler2(request):
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')
Expand All @@ -272,8 +260,7 @@ async def handler(request):
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')
Expand Down Expand Up @@ -305,8 +292,7 @@ def handler_exception(request, exception):
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 = []
Expand Down Expand Up @@ -341,12 +327,11 @@ def handler_6(sanic, loop):

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)
Expand All @@ -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',
Expand All @@ -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()

Expand Down Expand Up @@ -478,9 +461,7 @@ async def handler(request, ws):
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')

Expand Down
21 changes: 7 additions & 14 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
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'
Expand Down Expand Up @@ -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
Expand All @@ -53,14 +51,12 @@ def test_load_from_file():
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)
Expand All @@ -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
Expand All @@ -87,7 +81,6 @@ class 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
18 changes: 6 additions & 12 deletions tests/test_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
# GET
# ------------------------------------------------------------ #

def test_cookies():
app = Sanic('test_text')
def test_cookies(app):

@app.route('/')
def handler(request):
Expand All @@ -30,8 +29,7 @@ def handler(request):
(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):
Expand All @@ -49,8 +47,7 @@ def handler(request):
(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):
Expand All @@ -65,8 +62,7 @@ def handler(request):

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):
Expand All @@ -78,8 +74,7 @@ async def handler(request):

assert response.text == 'Cookies are: working!'

def test_cookie_options():
app = Sanic('test_text')
def test_cookie_options(app):

@app.route('/')
def handler(request):
Expand All @@ -96,8 +91,7 @@ def handler(request):
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):
Expand Down
9 changes: 3 additions & 6 deletions tests/test_create_task.py
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -30,8 +28,7 @@ async def set(request):
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('/')
Expand All @@ -44,4 +41,4 @@ async def coro(app):
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'
11 changes: 4 additions & 7 deletions tests/test_custom_protocol.py
Original file line number Diff line number Diff line change
@@ -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):

Expand All @@ -16,12 +13,12 @@ def write_response(self, response):
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
}
Expand Down

0 comments on commit 076cf51

Please sign in to comment.