2

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)
Laurent
  • 1,141
  • 1
  • 12
  • 17

1 Answers1

2

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.

leeor
  • 17,041
  • 6
  • 34
  • 60
  • True, that's what I do already. I updated the question to make it clearer, thanks for the answer. – Laurent Feb 14 '16 at 17:38
  • Ok, I see. I'm not sure there's a way to avoid that one-liner in each namespace. You might be able to do something programmatic with namespaces but that seems like a bad trade-off for complexity. – leeor Feb 14 '16 at 17:40