Welcome! Please see the About page for a little more info on how this works.

+2 votes
in Test by
recategorized by

The existing clojure.test/use-fixtures function works well enough for the broad use-cases, but frequenly I find myself writing functions that I want to attach to only a set of tests and having to either move those tests to a new namespace or hand-expanding the fixture in the tests. These solutions have a number of issues:

If in a new namespace:

  • What to name the new namespace? How do I make sure other devs know where to look for it?
  • Splitting tests that all focus on the same functionality makes it hard to see the whole picture.
  • To avoid requiring other test files, other common fixtures need to be moved to their own new namespaces.

If hand-expanding:

  • Inclusion of wrapping logic occludes actual test focus.
  • It's cumbersome to wrap with multiple fixtures (lots of indentation).
  • It's cumbersome to refactor/reorder multiple fixtures (unlike moving symbols back and forth in a vector).


I would love to see some mechanism for attaching fixtures to specific tests. This has been accomplished by various community libraries (for example, fixa and kaocha), but these tend to require using their own custom implementation of clojure.test/deftest to work around the lack of functionality (fixa) or implement their own hook system (kaocha).

Potential solutions

  • Following fixa's lead, fixtures could be attached to deftests via metadata.
  • A new function/macro, like testing, could be added that takes a collection of fixtures and applies them to all nested deftests. Would rely on a dynamic var and push/pop the fixtures as nesting happens:

Metadata example:

(deftest ^{:fixtures [fixture-a]} some-test
  (is (= 1 2)))

New macro:

(with-fixtures [fixture-a fixture-b]
  (deftest some-test
    (is (= 1 2))))

1 Answer

0 votes
selected by
Best answer
Thanks for logging that, @Alex! Would you be interested in a patch?
Sure, don't remember if you are a contributor but https://clojure.org/dev/dev#_becoming_a_contributor is the process