Continuous Coverage with Clojure

Getting Started

Project compilation: Clojure gets converted into Java bytecode, and Leiningen tasks automate that process.Dependency management. Just like Ruby's bundler and gemfiles, Leiningen automates the process of resolving and downloading the Java jar files that your code depends on.Running tasks. Similar to Ruby's Rake, you can run custom automation tasks written in Clojure.Deployment. Helps with creating Java jars which can be executed or incorporated in other projects. Similar to Ruby gems & project.clj is similar to a gem file.
(defproject com.something.servicename "0.1.0-SNAPSHOT"
:description "description of the service and what it is doing"
:url "http://companyname.com"
:min-lein-version "2.0.0"
:exclusions [org.clojure/clojure]
:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/tools.reader "1.2.2"]
[org.clojure/test.check "0.7.0"]
[ring/ring-core "1.5.0"]
[ring/ring-jetty-adapter "1.5.0"]
[clj-http "3.3.0"]
[cheshire "5.6.3"]
[http-kit "2.2.0"]]

Profiles

(defproject com.something.servicename "0.1.0-SNAPSHOT"
:description "description of the service and what it is doing"
:dependencies [[dependency/name "1.0"]]
:profiles
{:dev {:dependencies
[http-kit.fake "0.2.1"]
[http-kit.fake "0.2.1"]
[ring/ring-mock "0.3.1"]]
:plugins [[some-plugin "0.3.10"]]
:resource-paths ["path/to/test-data"]
:test {:test-paths ["test"]
:dependencies [[org.clojure/test.check "0.7.0"]
[pjstadig/humane-test-output"0.8.1"]]

Test Definition

(ns com.something.servicename.name-of-test
(:require [clojure.test.check.clojure-test :refer :all]
[cheshire.core :as json]
[clojure.java.io :as io]
[com.package.test.service.test-util :as util]
[clj-http.client :as client])
(:use [clojure.test]
[com.package.service.core.namespace]))
:require  setting up access to other Clojure namespaces from your code, you can optionally refer functions to the current ns:as It is common to make a namespace available under an alias
;; defining a simple test(deftest test-something-simple
(testing "A simple assertion"
(is (= 1 1))))
;; defining a helper function which your deftest function will call(defn helper-function [m ks expected]
(= expected (get-in m ks)))
(deftest test-that-calls-a-function
(testing "Calling a helper function in your test"
(is (ns/helper-function response-map [:key] "value"))))

Assertions

(deftest some-test
"Description of test"
(is (= 4 (+ 2 2))))
self.assertEqual(200, response.status_code, 'I should get a 200 response code')
(is (= 200 (:status user-information)) "I should get a 200 response code")
(deftest test-turn-response-to-map
(testing "Making a request and turning the response to a map"
(let [request (client/get (str "/url")
{:headers {:header "value"}
:content-type :json})
response (-> request
:body
(json/parse-string true))])))
(deftest test-nested-key-value-pair
(testing "Validating the response body returned from your request"
(is (= "expected" (str (get-in response [:object :key]))))))

Test Selectors

:test-selectors {:default #(not-any? % [:integration :http :db])
:db :db
:integration :integration
:acceptance :acceptance
:all
(constantly true)}
(ns com.something.servicename.name-of-test
(:require [clojure.test.check.clojure-test :refer :all]
[cheshire.core :as json]
[clojure.java.io :as io]
[com.project.test.service.util :as util]
[clj-http.client :as client])
(:use [clojure.test]
[com.project.service.ns.core]))
(deftest ^:acceptance test-name
(testing "A test that does something"
(is (= 1 1))))

Running the tests

$ lein test :acceptancelein test com.project.test.service.#####Ran 1 tests containing 1 assertions.0 failures, 0 errors.
$ lein test :only namespace/test-method-name

Analyzing Failures

FAIL in (test-namespace-name) (namespace_test.clj:34)A user was unable to do something and you received an error messageexpected: (= expected-status status)actual: (not (= 200 500))
Profiles: Setting up your to development tools for your project. You can make a testing profile for your suite of tests.Test Selectors: Allows you to tag your tests. Specify a selection of your tests to run at a time.Project setup: Similar to Ruby's Rake, you can run custom automation tasks written in Clojure.Test Definition: Helps with creating Java jars which can be executed or incorporated in other projects. Similar to Ruby gems & project.clj is similar to a gem file.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store