Skip to content

Commit 429154e

Browse files
committed
Mark url method of live_server fixture as deprecated
The `live_server.url` method is not used any more, so mark it as deprecated. The better way to get live server URL is via `flask.url_for` function. Explicitly emit DeprecationWarning when the `live_server.url` is called. Update test suite to remove any deprecated `live_server.url()` calls.
1 parent 9725875 commit 429154e

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

pytest_flask/fixtures.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3-
import time
3+
import functools
44
import multiprocessing
55
import pytest
66
import socket
7+
import time
8+
import warnings
79

810
try:
911
from urllib2 import urlopen
@@ -13,6 +15,21 @@
1315
from flask import _request_ctx_stack
1416

1517

18+
def deprecated(reason):
19+
"""Decorator which can be used to mark function or method as deprecated.
20+
It will result a warning being emmitted when the function is called.
21+
"""
22+
def decorator(func):
23+
@functools.wraps(func)
24+
def deprecated_call(*args, **kwargs):
25+
warnings.simplefilter('always', DeprecationWarning)
26+
warnings.warn(reason, DeprecationWarning, stacklevel=2)
27+
warnings.simplefilter('default', DeprecationWarning)
28+
return func(*args, **kwargs)
29+
return deprecated_call
30+
return decorator
31+
32+
1633
@pytest.yield_fixture
1734
def client(app):
1835
"""A Flask test client. An instance of :class:`flask.testing.TestClient`
@@ -97,6 +114,11 @@ def _is_ready(self):
97114
sock.close()
98115
return ret
99116

117+
@deprecated(reason=(
118+
'The "live_server.url" method is deprecated and scheduled '
119+
'to be removed in pytest-flask 1.0.0. Please use '
120+
'the "flask.url_for" function instead.',
121+
))
100122
def url(self, url=''):
101123
"""Returns the complete url based on server options."""
102124
return 'http://{host!s}:{port!s}{url!s}'.format(

tests/test_live_server.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ def test_server_url(self, live_server):
2727
assert live_server.url() == 'http://localhost:%d' % live_server.port
2828
assert live_server.url('/ping') == 'http://localhost:%d/ping' % live_server.port
2929

30+
def test_server_url_is_deprecated(self, live_server):
31+
assert pytest.deprecated_call(live_server.url)
32+
3033
def test_server_listening(self, live_server):
31-
res = urlopen(live_server.url('/ping'))
34+
res = urlopen(url_for('ping', _external=True))
3235
assert res.code == 200
3336
assert b'pong' in res.read()
3437

0 commit comments

Comments
 (0)