How to write tests for Cyclone in Tornado style?

I searched for search queries and accessed the IRC to no avail. Cyclone should look like the Tornado protocol for Twisted. But there are no tests in the Cyclone repository, and no one has written how to convert the tornado.testing.AsyncHTTPTestCase tags to implement code written against Cyclone.

  • How to start the server to test the web interface?
  • Where is self.fetch() ?
  • Where does the documentation in Cyclone describe how to convert an existing Tornado application?
+4
source share
2 answers

Unfortunately, at the moment there is nothing like tornado.testing.AsyncHTTPTestCase in the cyclone. It would be best to use Twisted Trial to write unit tests. One (slightly clonal) approach would explicitly call self.listener = reactor.listenTCP(<someport>, YourCycloneApplication()) in the setUp method inside your test case and call self.listener.stopListening() in the tearDown method.

Then in your testing methods, you can use cyclone.httpclient.fetch to fetch the pages. This is far from ideal. But at the moment this is the only way.

+2
source

Here is what we are currently using to test our cylinder processor, as we did with the tornado:

 from twisted.trial.unittest import TestCase from twisted.internet import defer, reactor from cyclone import httpclient # copied from tornado _next_port = 10000 def get_unused_port(): """Returns a (hopefully) unused port number.""" global _next_port port = _next_port _next_port = _next_port + 1 return port class TxTestCase(TestCase): def get_http_port(self): """Returns the port used by the HTTPServer. A new port is chosen for each test. """ if self.__port is None: self.__port = get_unused_port() return self.__port def setUp(self, *args, **kwargs): self.__port = None self._app = self.get_app() self._listener = None if self._app: self._listener = reactor.listenTCP(self.get_http_port(), self._app) return TestCase.setUp(self, *args, **kwargs) def get_app(self): return None def tearDown(self): if self._listener: self._listener.stopListening() @defer.inlineCallbacks def fetch(self, url, *args, **kwargs): response = yield httpclient.fetch('http://localhost:%s%s'%(self.get_http_port(), url), *args, **kwargs) defer.returnValue(response) 

That way you return the fetch method;)

And no longer need to use a trial version.

Here is a usage example:

 from twisted.internet import defer class Test(TxTestCase): def get_app(self): return MyApplication() @defer.inlineCallbacks def some_test_method(self): res = yield self.fetch('/path/to/resource') self.assertEquals(200, res.code) 

Hope this helps.

+2
source

Source: https://habr.com/ru/post/1438754/


All Articles