I have seen many clojure projects that disable integration tests by default by adding this setting to project.clj
:
:test-selectors {:default (complement :integration)
:integration :integration}
But, if a namespace contains only integration tests, the fixtures in it still run when I run lein test
!
For example if I run lein new app test
and make the contents of core_test.clj
this:
(defn fixture [f]
(println "Expensive setup fixture is running")
(f))
(use-fixtures :once fixture)
(deftest ^:integration a-test
(println "integration test running"))
Then when I run lein test
I see the fixture running even though no tests are run.
What is the correct way to handle this in clojure?
One way to accomplish not running the expensive computation is to take advantage of the fact that even though the
:once
fixtures will run regardless of whether there are tests to run in the ns or not, the:each
fixtures will only run on each actually-running test.Instead of doing the actual computation (or acquiring resources like a db connection, or doing whatever side-effects) in the
:once
fixture, we only do it in the first (we want to do it only once!):each
fixture, for example doing as following:The output of
lein test
will be as following (notice how theenable-fixture
has run but not the expensiveexpensive-fixture
):When running
lein test :integration
, theexpensive-fixture
will run exactly once: