Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

test_crawl: make mock server print a line when ready, and wait for that line to start tests, instead of waiting for an arbitrary time

Pablo Hoffman626331b8650a
diff --git a/scrapy/tests/mockserver.py b/scrapy/tests/mockserver.py
index e3c8ed1d0..d5ff56605 100644
--- a/scrapy/tests/mockserver.py
+++ b/scrapy/tests/mockserver.py
@@ -77,7 +77,12 @@ class Root(Resource):
return 'Scrapy mock HTTP server\n'
-root = Root()
-factory = Site(root)
-reactor.listenTCP(8998, factory)
-reactor.run()
+if __name__ == "__main__":
+ root = Root()
+ factory = Site(root)
+ port = reactor.listenTCP(8998, factory, interface="127.0.0.1")
+ def print_listening():
+ h = port.getHost()
+ print "Mock server running at http://%s:%d" % (h.host, h.port)
+ reactor.callWhenRunning(print_listening)
+ reactor.run()
diff --git a/scrapy/tests/test_crawl.py b/scrapy/tests/test_crawl.py
index 755fd2801..b4cfa465f 100644
--- a/scrapy/tests/test_crawl.py
+++ b/scrapy/tests/test_crawl.py
@@ -1,7 +1,7 @@
import sys, time
from twisted.internet import defer
from twisted.trial.unittest import TestCase, SkipTest
-from subprocess import Popen
+from subprocess import Popen, PIPE
from scrapy.spider import BaseSpider
from scrapy.http import Request
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
@@ -32,8 +32,8 @@ def docrawl(spider, settings=None):
class CrawlTestCase(TestCase):
def setUp(self):
- self.proc = Popen([sys.executable, '-m', 'scrapy.tests.mockserver'])
- time.sleep(0.5)
+ self.proc = Popen([sys.executable, '-u', '-m', 'scrapy.tests.mockserver'], stdout=PIPE)
+ self.proc.stdout.readline()
def tearDown(self):
self.proc.kill()