liveBook · Manning

Use-fixtures

As explained in Clojure: The Essential Reference

use-fixtures is a clojure.test function that registers fixtures for execution around tests in a namespace.

use-fixtures registers fixture functions for a clojure.test namespace, arranging necessary side effects before and after tests. Fixtures support per-test execution with :each or one-time namespace setup with :once. section 22.9

Run setup around every test

(defn setup [tests] (println "### before") (tests) (println "### after")) (use-fixtures :each setup) (deftest a-test (is (= 1 1))) (deftest b-test (is (= 1 1))) (run-tests) ;; ### before ;; ### after ;; ### before ;; ### after ;; Ran 2 tests containing 2 assertions.

A fixture receives a zero-argument test runner, calls it between setup and cleanup, and thereby recreates conditions such as files or database tables. Fixtures do not run when test-ns-hook is defined. section 22.9

How Clojure: The Essential Reference covers it

How deep it goes
Explains registration, per-test or namespace-wide timing, runner invocation, and the test-ns-hook exception.
Examples
Demonstrates a setup function printing before and after each of two tests.
What it assumes
Assumes familiarity with clojure.test tests and functions while demonstrating the fixture runner contract.
What you can do after
Operate/configure clojure.test with repeatable setup and cleanup around individual tests.
How it connects
Connects fixtures with clojure.test, test namespaces, test-ns-hook, setup, and cleanup.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.