Skip to content

Commit a728592

Browse files
committed
Prevent starting live server when --no-start-live-server is set
Thanks to @EliRibble.
1 parent d7a347b commit a728592

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

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

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)