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
:oncefixtures will run regardless of whether there are tests to run in the ns or not, the:eachfixtures 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
:oncefixture, we only do it in the first (we want to do it only once!):eachfixture, for example doing as following:The output of
lein testwill be as following (notice how theenable-fixturehas run but not the expensiveexpensive-fixture):When running
lein test :integration, theexpensive-fixturewill run exactly once: