Document best practices for integration/functional tests
How do I test my klein apps, without using urllib or similar?
Currently I'm considering adapting these Twisted web tests or these ones.
A bug tracker isn’t really a support medium, please consider to ask on #twisted.web on Freenode.
But maybe we should make this into a documentation bug.
As for your question, I like to do something like this for integration tests:
def with_base_url(method):
"""
Save some typing and ensure we always use our own pool.
"""
def request(self, url, *args, **kwargs):
return method(self.base_url + url, *args, pool=self.pool, **kwargs)
return request
class IntegrationMixin(object):
get = with_base_url(treq.get)
def setUp(self):
self.pool = HTTPConnectionPool(reactor, False)
self.addCleanup(self.pool.closeCachedConnections)
def startHTTPServer(self, app):
port = reactor.listenTCP(
0,
server.Site(app.resource())
)
self.addCleanup(port.stopListening)
portNumber = port.getHost().port
self.base_url = "http://127.0.0.1:%d" % (portNumber,)
return self.base_url
class FunctionalTest(IntegrationMixin, TestCase):
def setUp(self):
IntegrationMixin.setUp(self)
self.startHTTPServer(
YourApp()
)
Then you can just use self.get("/your-path")in your tests.
Which as you see uses treq that is much more user-friendly.
This information should be added to the docs.
This is where the future lies: https://github.com/twisted/treq/blob/e0b3ad8253b455bcf63ec040f9c46e6caf194c67/treq/testing.py
Unfortunately treq.testing does not have narrative documentation! But it should.