Compojure api-test split into multiple files with same fixture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns my.fixtures | |
(:require | |
[my.db :as db])) | |
(defn with-db [f] | |
(db/start) | |
(f) | |
(db/stop)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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