Skip to content

Instantly share code, notes, and snippets.

@singen
Last active October 24, 2016 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save singen/07357205ff677f092101a9748af5771c to your computer and use it in GitHub Desktop.
Save singen/07357205ff677f092101a9748af5771c to your computer and use it in GitHub Desktop.
Compojure api-test split into multiple files with same fixture
(in-ns 'my.test) # Note same ns as with test.clj
(use-fixtures :once with-db)
# You can now repeat use-fixtures in any other test
# you create in this namespace. While in the same namespace
# any fixture used with :once will only be run once in the
# entire namespace.
# You can even combine fixtures in different files.
# For example: (use-fixtures :once with-db with-test-data)
(deftest bonk-route
(testing "Bonk route"
(let [response (app (mock/request :head "/bonk"))]
(is (= (:status response) 200)))))
(ns my.fixtures
(:require
[my.db :as db]))
(defn with-db [f]
(db/start)
(f)
(db/stop))
(ns my.handler
(:require
[compojure.api.sweet :refer :all]
[my.other.route :as other]))
# Suppose my.other.route adds a route like "/bonk"
(defapi app
(HEAD "/" [] "")
other/route)
(ns my.test
(:require [clojure.test :refer :all]
[ring.mock.request :as mock]
[my.handler :refer :all]
[my.fixtures :refer :all]))
# You must now require eveything needed by your other tests here
# to use the require macro.
# Otherwise you have to use both require and refer.
# Probably loading individual tests is not possible.
(deftest test-app
(testing "HEAD route"
(let [response (app (mock/request :head "/"))]
(is (= (:status response) 200)))))
(load "another_test") #this is where the tests go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment