Unit Test with Bokeh

Hi, I’m running some data visualization using Bokeh on a local server like this:

from bokeh.application import Application
from bokeh.application.handlers import FunctionHandler
from bokeh.server.server import Server as bokehServer

bokehApp = Application(FunctionHandler(foo))
bokehServer = bokehServer({'/request-path': bokehApp})
bokehServer.start()

Is there a way to do some unit testing on the Application so that when I call /request-path, I can assert I get the response 200.

I know that in Tornado, I would follow something like this:

import hello    

class TestHelloApp(AsyncHTTPTestCase):
        def get_app(self):
            return hello.make_app()

        def test_homepage(self):
            response = self.fetch('/')
            self.assertEqual(response.code, 200)
            self.assertEqual(response.body, 'Hello, world')

Thanks :slight_smile:

The above tests are basically testing Tornado and Bokeh (which is not your responsibility) and nothing in particular about your actual app. I would come at this from a different angle. I think you should:

  • Trust that Tornado will correctly handle network connections if Bokeh is working
  • Trust that Bokeh will work if your foo function modifies the document how you intend

Therefore, especially if this is to be considered a unit test, I would suggest testing only the part that you have control over: the foo function. In particular you can just call it with a new Document passed in, and verify that the Document state after the call is what you expect. That is both simpler to do and also (can be) substantially more thorough.

Hi Bryan,

Thank you for your answer.

Can you redirect me to some references about what you mentioned here please?

In particular you can just call it with a new Document passed in, and verify that the Document state after the call is what you expect. That is both simpler to do and also (can be) substantially more thorough.

Thanks again for your time.

I don’t have any references to provide. That’s just how I would do things if I needed to test Bokeh applications (which I don’t usually, I have to worry about testing Bokeh itself as part of maintaining it). But this is what mean:

In [5]: def foo(doc):  # just a toy example representing your foo function
   ...:     p = figure()
   ...:     doc.add_root(p)
   ...:

In [6]: d = Document()

In [7]: foo(d)

In [8]: d.roots
Out[8]: [Figure(id='1001', ...)]

After you call your foo on a new Document the Document state has changed, and you can inspect and interrogate that state in all the usual ways you might do in unit tests, to make sure you have constructed the document that you mean to.

1 Like