Search code examples
clojurewebdrivercontinuous-integrationleiningen

How to do integration testing for clojure/ring/selenium/leiningen?


I have a clojure/ring web application which I want to test with clj-webdriver. Is there a simple way to run the ring webserver and then run the tests such that they target the ring instance?

I'm thinking of a usage something like:

$ lein with-ring test

I have two ideas so far:

  1. write a custom higher order leiningen plugin. Seems too complicated. Does a similar plugin already exist?

  2. write a Makefile rule which starts ring, runs lein test, find&kill the ring process - too complicated/hacky

Is there a simpler way?


Solution

  • If you're using clojure.test you can call use-fixtures to start/stop your server from within the tests, e.g.:

    (use-fixtures
      :once
      (fn [f]
        (let [server (ring.adapter.jetty/run-jetty
                        #'your-app
                        {:port 1234 :join? false})]
          (try
            (f)
            (finally
              (.stop server))))))
    

    Just adjust start/stop logic to your preferred webserver (jetty in this example; http-kit would be very similar). If you replace :once with :each you'll even have a fresh server instance for each single test.