Selenium is a really nice framework for testing web application front-ends by automating actions through a web browser, but it also requires a web server to be running so that the browser can interact with the web application. Most other tests usually interact with the code directly, so this requirement can also lead to a slight problem... how should the web server be started when running tests?
The simplest way to run a Selenium test is to manually start up a web server for your application and then run the tests against it, but this can get a bit tedious after a while (especially if you keep forgetting to start the server).
Django provides a LiveServerTestCase
which automates starting up a web
server to serve up your Django application, run your Selenium tests, and then
stop the server again. This is a really nice approach, but I wanted to be able
to do something similar when I am not using Django.
Last week I came across the flask-testing framework which provides similar
functionality for Flask applications. The flask-testing
LiveServerTestCase
is inspired by the Django version, but is much
simpler. Unfortunately it is also a bit specific to Flask applications.
What I really wanted was a something that could be used for any WSGI compliant
web application. So I wrote my own which is loosely based on the
flask-testing version. You simply inherit from the LiveServerTestCase
class instead of from unittest.TestCase
when creating your test class,
override the create_app()
method to return your WSGI application and,
write your tests as normal. When you run your tests it will handle starting and
stopping the web server in the background as required. I have written a very
basic example Bottle application called bottle-selenium to show it in
action.
I originally wrote this to use with Bottle applications, mainly because they are very simple to work with. My eventual goal is to use this for testing the development of Roundup instances, so it should work with any WSGI compliant web application.
Update (22/03/2012): The LiveServerTestCase
is now available in its own package
called wsgi-liveserver.