Environment: Python 3, tornado 4.4. The normal unittests cannot be used because methods are asynchronous. There is ttp://www.tornadoweb.org/en/stable/testing.html that explains how to do unit testing for asynchronous code. But that works with tornado coroutines ONLY. The classes I want to test are using the async def statements, and they cannot be tested this way. For example, here is a test case that uses ASyncHTTPClient.fetch and its callback parameter:
class MyTestCase2(AsyncTestCase):
def test_http_fetch(self):
client = AsyncHTTPClient(self.io_loop)
client.fetch("http://www.tornadoweb.org/", self.stop)
response = self.wait()
# Test contents of response
self.assertIn("FriendFeed", response.body)
But my methods are declared like this:
class Connection:
async def get_data(url, *args):
# ....
And there is no callback. There cannot be, because async def methods MUST be awaited for! But I cannot await from inside the test method, because it is not an async method. I can make it an async method (I have tried) but then I get this error:
TypeError: Generator and coroutine test methods should be decorated with tornado.testing.gen_test
raised here: https://github.com/tornadoweb/tornado/blob/master/tornado/testing.py#L138
So how can I test async def methods from unit tests?
(Also asked here: http://stackoverflow.com/questions/42040248/howto-do-unittest-for-tornado-async-def )
import unittest
import tornado.ioloop
from weedstorm.weed import WeedFS
class MyTests(unittest.TestCase):
async def main(self):
self.setUp()
for mname in sorted(dir(self)):
if mname.startswith("test_"):
item = getattr(self, mname)
if callable(item):
print("Testing",mname)
await item()
tornado.ioloop.IOLoop.current().stop()
def setUp(self):
# Initialize your tests here
pass
async def test_01(self):
pass # You can await here...
async def test_02(self):
pass # You can await here...
if __name__ == '__main__':
io_loop = tornado.ioloop.IOLoop.current()
io_loop.run_sync(WeedFSTests().main)
io_loop.start()
--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornado+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
tornado.testing.main exists because the python 2.6 unittest module had no good way to run all of a projects tests, so it required you to define a function `all()` that would initialize a unittest.TestSuite for the project.Now, of course, the unittest module is greatly improved, so there is little reason to use tornado.testing.main instead of unittest.main. (The main reason at this point is that tornado.testing.main will call tornado.options.parse_command_line for you if you use tornado.options). This function should probably just be deprecated.And in any case, using tornado.testing.main was never a requirement for testing with tornado and AsyncTestCase - the AsyncTestCase classes are self-contained and don't require any particular runner (so you can run them with nose or pytest too).