Clojure - Automated Testing



In this chapter, let’s discuss automated testing options provided by Clojure.

Testing for Client Applications

In order to use testing for Clojure framework, you have to use the dependencies located at https://github.com/slagyr/speclj#manual-installation

This URL provides the speclj framework, which is used as a Test data driven or Behaviour driven test framework for Clojure. You have to ensure that you use the Clojure 1.7.0 framework when using any of the ‘speclj’ libraries. By default, the test files will be different from the Clojure code files and need to be placed in a ‘spec’ directory.

Following is a sample code for a test file.

(ns change.core-spec
   (:require [speclj.core :refer :all]))
(describe "Truth"
   (it "is true"
   (should true))
   (it "is not false"
   (should-not false)))
(run-specs)

Following things need to be noted about the above code −

  • We first have to ensure to use the ‘require’ statement to include all the core libraries in the ‘speclj’ framework.

  • Next is the ‘describe’ function. This is used to provide a description for the test case being created.

  • Next function is the ‘it’ function, which is the actual test case. In the first test case, the “is true” string is the name given to the test case.

  • Should and should-not are known as assertions. All assertions begin with should. Should and should-not are just two of the many assertions available. They both take expressions that they will check for truthy-ness and falsy-ness respectively.

If you run the test case, you will get the following output. The output shows the time taken in milliseconds for the test case to run.

←[32m.←[0m←[32m.←[0m
Finished in 0.00014 seconds

Testing for Web-based Applications

Selenium is one of the key frameworks used for testing modern day web-based applications. Clojure libraries are also available which can be used for testing web-based applications.

Let’s look at how we can use the Selenium libraries for testing Clojure web-based applications.

Step 1 − The first step is to ensure we are using the Ring and Compojure framework to create a web-based application, which needs to be tested. Let’s use one of the examples from our earlier chapters. The following code is a simple web application, which displays “Hello World” in the browser.

(ns my-webapp.handler
   (:require [compojure.core :refer :all]
      [compojure.route :as route]
      [ring.middleware.defaults :refer [wrap-defaults site-defaults]]))
(defroutes app-routes
   (GET "/" [] "Hello World")
   (route/not-found "Not Found"))
(def app
   (wrap-defaults app-routes site-defaults))

Step 2 − Next make sure to download the selenium jar file https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server/2.47.0 and include it in your classpath.

Step 3 − Also ensure to download the ‘clj’ web driver, which will be used for running the web test from the following location.

https://clojars.org/clj-webdriver/versions/0.7.1

Step 4 − In your project directory, create another directory called features and create a file called ‘config.clj’.

Step 5 − Next add the following code to the ‘config.clj’ file created in the earlier step.

ns clj-webdriver-tutorial.features.config)
(def test-port 3000)
(def test-host "localhost")
(def test-base-url (str "http://" test-host ":" test-port "/"))

The above code basically tells the web test framework to test the application, which gets loaded at the URL http://localhost:3000

Step 6 − Finally, let’s write our code to carry out our test.

(ns clj-webdriver-tutorial.features.homepage
   (:require [clojure.test :refer :all]
      [ring.adapter.jetty :refer [run-jetty]]
      [clj-webdriver.taxi :refer :all]
      [clj-webdriver-tutorial.features.config :refer :all]
      [clj-webdriver-tutorial.handler :refer [app-routes]]))
(ns clj-webdriver-tutorial.features.homepage
   (:require [clojure.test :refer :all]
      [ring.adapter.jetty :refer [run-jetty]]
      [clj-webdriver.taxi :refer :all]
      [clj-webdriver-tutorial.features.config :refer :all]
      [clj-webdriver-tutorial.handler :refer [app-routes]]))
(defn start-server []
   (loop [server (run-jetty app-routes {:port test-port, :join? false})]
      (if (.isStarted server)
         server
         (recur server))))
(defn stop-server [server]
   (.stop server))
(defn start-browser []
   (set-driver! {:browser :firefox}))
(defn stop-browser []
   (quit))
(deftest homepage-greeting
   (let [server (start-server)]
      (start-browser)
      (to test-base-url)
      (is (= (text "body") "Hello World"))
      (stop-browser)
      (stop-server server)))

The above code is going to take the following actions −

  • Start the server for the application.
  • Open the root path in the browser.
  • Check if the "Hello World" message is present on the page.
  • Close the browser.
  • Shut down the server.
Advertisements