Skip to content

Instantly share code, notes, and snippets.

Last active October 24, 2016 15:17
What would you like to do?
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
[my.db :as db]))
(defn with-db [f]
(ns my.handler
[compojure.api.sweet :refer :all]
[my.other.route :as other]))
# Suppose my.other.route adds a route like "/bonk"
(defapi app
(HEAD "/" [] "")
(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