How to setup a fixture for a group of tests in clojure.test

761 Views Asked by At

In my tests I use something like:

;; api.utils
(defn wrap-test-server [f]
  (start-test-server)
  (f)
  (stop-test-server))

;; api.some-endpoint
(use-fixtures
  :once
  utils/wrap-test-server)

However, I have to duplicate the fixture setup code in every test module.

How can I setup a global fixture for all the tests?
Or even better, for a "package" so that tests in api.* are wrapped with the start / stop fixture.

Note that in this case, I don't care about the wrapping "level". The following would both work:

;; Wrap around all the tests in the package:
(start-test-server)
(test-1)
...
(test-n)
(stop-test-server)
;; Wrap every test in the package:
(start-test-server)
(test-1)
(stop-test-server)
...
(start-test-server)
(test-n)
(stop-test-server)
1

There are 1 best solutions below

2
On

You can definitely pull that function into another namespace and re-use it that way and do something like this:

(defn one-time-setup [f]
    (start-test-server)
    (f)
    (stop-test-server))

(use-fixtures :once one-time-setup)

This blog post summarizes it pretty well. From the post:

A :once fixture - method that will be called before any tests are run and it will be passed a function to call that will invoke all the tests.