99import logging
1010
1111try :
12- from urllib2 import urlopen
12+ from urllib2 import URLError , urlopen
1313except ImportError :
14+ from urllib .error import URLError
1415 from urllib .request import urlopen
1516
1617from flask import _request_ctx_stack
@@ -37,7 +38,7 @@ def login(self, email, password):
3738 return self.client.post(url_for('login'), data=credentials)
3839
3940 def test_login(self):
40- assert self.login('vital @example.com', 'pass').status_code == 200
41+ assert self.login('foo @example.com', 'pass').status_code == 200
4142
4243 """
4344 if request .cls is not None :
@@ -61,7 +62,7 @@ def __init__(self, app, port, clean_stop=False):
6162 def start (self ):
6263 """Start application in a separate process."""
6364 def worker (app , port ):
64- app .run (port = port , use_reloader = False )
65+ app .run (port = port , use_reloader = False , threaded = True )
6566 self ._process = multiprocessing .Process (
6667 target = worker ,
6768 args = (self .app , self .port )
@@ -76,7 +77,7 @@ def worker(app, port):
7677 try :
7778 urlopen (self .url ())
7879 timeout = 0
79- except :
80+ except URLError :
8081 timeout -= 1
8182
8283 def url (self , url = '' ):
@@ -114,10 +115,10 @@ def _rewrite_server_name(server_name, new_port):
114115
115116
116117@pytest .fixture (scope = 'function' )
117- def live_server (request , app , monkeypatch ):
118+ def live_server (request , app , monkeypatch , pytestconfig ):
118119 """Run application in a separate process.
119120
120- When the ``live_server`` fixture is applyed , the ``url_for`` function
121+ When the ``live_server`` fixture is applied , the ``url_for`` function
121122 works as expected::
122123
123124 def test_server_is_up_and_running(live_server):
@@ -128,11 +129,14 @@ def test_server_is_up_and_running(live_server):
128129 assert res.code == 200
129130
130131 """
131- # Bind to an open port
132- s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
133- s .bind (('' , 0 ))
134- port = s .getsockname ()[1 ]
135- s .close ()
132+ port = pytestconfig .getvalue ('live_server_port' )
133+
134+ if port == 0 :
135+ # Bind to an open port
136+ s = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
137+ s .bind (('' , 0 ))
138+ port = s .getsockname ()[1 ]
139+ s .close ()
136140
137141 # Explicitly set application ``SERVER_NAME`` for test suite
138142 # and restore original value on test teardown.
0 commit comments