mardi 1 mars 2016

How to write tests for Tornado's AsyncHTTPClient?

I'm trying to follow TDD practice while writing Tornado application. I have an endpoint with get method which get data from several API's, filter, combine it and send a response. For this get handler I created a helper, which is responsible for async data fetching. And I want to write tests for it.

Helper looks like this:

class DummyHelper(object):
    @gen.coroutine
    def fetch(self, url):
        http_client = AsyncHTTPClient()
        response = yield http_client.fetch(url)

        raise gen.Return(response.body)

And tests are:

from tornado.testing import AsyncHTTPTestCase
from tornado.web import HTTPError
import app
from lib.handlers.dummy import DummyHelper
from tornado.testing import gen_test

class TestDummyHandler(AsyncHTTPTestCase):    
    def get_app(self):
        return app.get_app()

    @gen_test
    def test_200_fetch(self):
        helper = DummyHelper()
        response = yield helper.fetch("http://google.com/")
        self.assertEqual(response.code, 200)

    @gen_test
    def test_404_fetch(self):
        helper = DummyHelper()
        with self.assertRaises(HTTPError):
            yield helper.fetch("http://google.com/test")

Everything works fine for test_200_fetch. But it's not working for test_404_fetch. How to make it work? And should I even write those tests?

Aucun commentaire:

Enregistrer un commentaire