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

+7 votes
in Test by

When looking at (log) output from tests written with clojure.test, I would like to be able to identify the output associated with each test. A mechanism to expose the current test var within an :each fixture would enable this.

One mechanism might be to bind a test-var var with the current test var before calling the each-fixture-fn in clojure.test/test-all-vars.

Proposed: The proposed patch (clj840-20161122.diff) allows 'each' fixtures to access the var associated with the currently executing test by using * }. As a result of this change, 'each' fixtures are able to access the metadata associated with the current test var, including the name.

The patch achieves the above by changing the order in which functions are wrapped when a test and its associated 'each' fixtures are run. Before this patch, 'each' fixtures were combined into a single higher-order function, which was then given a thunk containing an invocation of the {{test-var}} function to execute as its body. After this patch, the {{test-var}} function is now responsible for joining and executing 'each' fixtures but, importantly, it does so within the scope of the binding expression that adds the current test var to * }. {{test-var}} now invokes the joined fixtures function, rather than the joined fixtures function being given a thunk that invokes {{test-var}}.

Patch: clj840-20161122.diff

Hello, I wanted to weigh in, saying that I would find the addition of this functionality useful in the Clojure test libraries I work on.

For my use-case, Duncan's 4-line patch that adds `*test-var*`and binds it around the `each-fixture-fn` form would suffice.

I would use this feature in a re-implementation of the `state-flow` test lib using base `clojure.test` blocks (`deftest` + fixtures) to allow for better integration with the existing Clojure tooling ecosystem. Without access to the current test var in the fixture I cannot find a way to perform certain test setup in the fixture (https://gist.github.com/philomates/32f8e1f7a2a4e746ac0186964aaaa2d3#file-flow-clj-L74 sketches out the use-case in code)

17 Answers

0 votes

Comment made by: joelittlejohn

Thanks for taking the time to think about that todo list, Alex. Much appreciated. I'll investigate these things.

0 votes
Reference: https://clojure.atlassian.net/browse/CLJ-840 (reported by hugoduncan)