Skip to content

Commit 5fdaec7

Browse files
committed
Merge pull request #36 from vitalk/optional-live-server-start
Add options to prevent live server from starting automatically.
2 parents f1f2c4e + 95b36ba commit 5fdaec7

File tree

6 files changed

+129
-2
lines changed

6 files changed

+129
-2
lines changed

docs/features.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,41 @@ other headless browsers).
139139
assert res.code == 200
140140
141141
142+
``--start-live-server`` - start live server automatically (default)
143+
```````````````````````````````````````````````````````````````````
144+
145+
146+
``--no-start-live-server`` - don't start live server automatically
147+
``````````````````````````````````````````````````````````````````
148+
149+
By default the server is starting automatically whenever you reference
150+
``live_server`` fixture in your tests. But starting live server imposes some
151+
high costs on tests that need it when they may not be ready yet. To prevent
152+
that behaviour pass ``--no-start-live-server`` into your default options (for
153+
example, in your project's ``pytest.ini`` file)::
154+
155+
[pytest]
156+
addopts = --no-start-live-server
157+
158+
.. note::
159+
160+
Your **should manually start** live server after you finish your application
161+
configuration and define all required routes:
162+
163+
.. code:: python
164+
165+
def test_add_endpoint_to_live_server(live_server):
166+
@live_server.app.route('/test-endpoint')
167+
def test_endpoint():
168+
return 'got it', 200
169+
170+
live_server.start()
171+
172+
res = urlopen(url_for('test_endpoint', _external=True))
173+
assert res.code == 200
174+
assert b'got it' in res.read()
175+
176+
142177
``request_ctx`` - request context
143178
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
144179

pytest_flask/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
__version__ = "0.9.0"
4+
__version__ = "0.10.0"

pytest_flask/fixtures.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ def __init__(self, app, port):
5353
self.app = app
5454
self.port = port
5555
self._process = None
56-
self.start()
5756

5857
def start(self):
5958
"""Start application in a separate process."""
@@ -124,6 +123,9 @@ def test_server_is_up_and_running(live_server):
124123
_rewrite_server_name(server_name, str(port)))
125124

126125
server = LiveServer(app, port)
126+
if request.config.getvalue('start_live_server'):
127+
server.start()
128+
127129
request.addfinalizer(server.stop)
128130
return server
129131

pytest_flask/plugin.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ def test_something(app):
113113
monkeypatch.setitem(app.config, key.upper(), value)
114114

115115

116+
def pytest_addoption(parser):
117+
group = parser.getgroup('flask')
118+
group.addoption('--start-live-server',
119+
action="store_true", dest="start_live_server", default=True,
120+
help="start server automatically when live_server "
121+
"fixture is applyed (enabled by default).")
122+
group.addoption('--no-start-live-server',
123+
action="store_false", dest="start_live_server",
124+
help="don't start server automatically when live_server "
125+
"fixture is applyed.")
126+
127+
116128
def pytest_configure(config):
117129
config.addinivalue_line(
118130
'markers',

tests/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
# -*- coding: utf-8 -*-
33
import pytest
44

5+
from textwrap import dedent
56
from flask import Flask, jsonify
67

78

9+
pytest_plugins = 'pytester'
10+
11+
812
@pytest.fixture
913
def app():
1014
app = Flask(__name__)
@@ -19,3 +23,28 @@ def ping():
1923
return jsonify(ping='pong')
2024

2125
return app
26+
27+
28+
@pytest.fixture
29+
def appdir(testdir):
30+
app_root = testdir.tmpdir
31+
test_root = app_root.mkdir('tests')
32+
33+
def create_test_module(code, filename='test_app.py'):
34+
f = test_root.join(filename)
35+
f.write(dedent(code), ensure=True)
36+
return f
37+
38+
testdir.create_test_module = create_test_module
39+
40+
testdir.create_test_module('''
41+
import pytest
42+
43+
from flask import Flask
44+
45+
@pytest.fixture
46+
def app():
47+
app = Flask(__name__)
48+
return app
49+
''', filename='conftest.py')
50+
return testdir

tests/test_live_server.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,52 @@ def test_set_application_server_name(self, live_server):
3333
@pytest.mark.options(server_name='example.com:5000')
3434
def test_rewrite_application_server_name(self, live_server):
3535
assert live_server.app.config['SERVER_NAME'] == 'example.com:%d' % live_server.port
36+
37+
def test_prevent_starting_live_server(self, appdir):
38+
appdir.create_test_module('''
39+
import pytest
40+
41+
def test_a(live_server):
42+
assert live_server._process is None
43+
''')
44+
45+
result = appdir.runpytest('-v', '--no-start-live-server')
46+
result.stdout.fnmatch_lines(['*PASSED*'])
47+
assert result.ret == 0
48+
49+
def test_start_live_server(self, appdir):
50+
appdir.create_test_module('''
51+
import pytest
52+
53+
def test_a(live_server):
54+
assert live_server._process
55+
assert live_server._process.is_alive()
56+
''')
57+
result = appdir.runpytest('-v', '--start-live-server')
58+
result.stdout.fnmatch_lines(['*PASSED*'])
59+
assert result.ret == 0
60+
61+
def test_add_endpoint_to_live_server(self, appdir):
62+
appdir.create_test_module('''
63+
import pytest
64+
try:
65+
from urllib2 import urlopen
66+
except ImportError:
67+
from urllib.request import urlopen
68+
69+
from flask import url_for
70+
71+
def test_a(live_server):
72+
@live_server.app.route('/new-endpoint')
73+
def new_endpoint():
74+
return 'got it', 200
75+
76+
live_server.start()
77+
78+
res = urlopen(url_for('new_endpoint', _external=True))
79+
assert res.code == 200
80+
assert b'got it' in res.read()
81+
''')
82+
result = appdir.runpytest('-v', '--no-start-live-server')
83+
result.stdout.fnmatch_lines(['*PASSED*'])
84+
assert result.ret == 0

0 commit comments

Comments
 (0)