Clojure

Add a way to access the current test var in :each fixtures for clojure.test

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
  • Patch:
    Code
  • Approval:
    Triaged

Description

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.

Patch: clj840-20161122.diff

  1. add-test-var.diff
    21/Oct/11 10:34 PM
    3 kB
    Hugo Duncan
  2. clj840-2.diff
    25/Oct/11 6:26 PM
    2 kB
    Stuart Sierra
  3. clj840-20161122.diff
    22/Nov/16 11:30 AM
    2 kB
    Joe Littlejohn

Activity

Hide
Stuart Sierra added a comment -

Or just pass the Var directly into the fixture. Vars are invokable.

Show
Stuart Sierra added a comment - Or just pass the Var directly into the fixture. Vars are invokable.
Hide
Hugo Duncan added a comment - - edited

I don't think that works, since the the function passed to the fixture is not the test var, but a function calling test-var on the test var.

Show
Hugo Duncan added a comment - - edited I don't think that works, since the the function passed to the fixture is not the test var, but a function calling test-var on the test var.
Hide
Hugo Duncan added a comment -

Patch to add test-var

Show
Hugo Duncan added a comment - Patch to add test-var
Hide
Stuart Sierra added a comment - - edited

*testing-vars* already has this information, but it's not visible to the fixture functions because it gets bound inside test-var.

Perhaps the :each fixture functions should be called in test-var rather than in test-all-vars. (The namespace of a Var is available in its metadata.) But then we have to call join-fixtures inside test-var every time.

Show
Stuart Sierra added a comment - - edited *testing-vars* already has this information, but it's not visible to the fixture functions because it gets bound inside test-var. Perhaps the :each fixture functions should be called in test-var rather than in test-all-vars. (The namespace of a Var is available in its metadata.) But then we have to call join-fixtures inside test-var every time.
Hide
Stuart Sierra added a comment -

Try this patch: clj840-2.diff.

This makes *testing-vars* visible to :each fixture functions, which seems intuitively more correct.

BUT it slightly changes the behavior of test-var, which I'm less happy about.

Show
Stuart Sierra added a comment - Try this patch: clj840-2.diff. This makes *testing-vars* visible to :each fixture functions, which seems intuitively more correct. BUT it slightly changes the behavior of test-var, which I'm less happy about.
Hide
Hugo Duncan added a comment -

Might it make sense to provide a function on top of testing-vars to return the current test-var?

Show
Hugo Duncan added a comment - Might it make sense to provide a function on top of testing-vars to return the current test-var?
Hide
Stuart Sierra added a comment -

No, that function is first

Show
Stuart Sierra added a comment - No, that function is first
Hide
Hugo Duncan added a comment -

I agree with having the dynamic vars as part of the extension interface, but would have thought that having a function for use when writing tests would have been cleaner. Just my 2c.

Show
Hugo Duncan added a comment - I agree with having the dynamic vars as part of the extension interface, but would have thought that having a function for use when writing tests would have been cleaner. Just my 2c.
Hide
Andy Fingerhut added a comment -

With a commit made on Nov 22, 2013, patch clj840-2.diff no longer applies cleanly to latest master. Updating it appears like it might be straightforward, but best for someone who knows this part of the code well to do so.

Show
Andy Fingerhut added a comment - With a commit made on Nov 22, 2013, patch clj840-2.diff no longer applies cleanly to latest master. Updating it appears like it might be straightforward, but best for someone who knows this part of the code well to do so.
Hide
Joe Littlejohn added a comment -

I'd find it very useful if this one was fixed.

I've added an updated patch that has the same content as clj840-2.diff but applies against the current master (c0326d2), as of November 22nd 2016.

Show
Joe Littlejohn added a comment - I'd find it very useful if this one was fixed. I've added an updated patch that has the same content as clj840-2.diff but applies against the current master (c0326d2), as of November 22nd 2016.
Hide
Joe Littlejohn added a comment -

I realise I've only translated a patch provided by someone else here, but if there's anything further you think this one needs before it's in a fit state to be considered then please do shout and I'll endeavour to add something further. Thanks.

Show
Joe Littlejohn added a comment - I realise I've only translated a patch provided by someone else here, but if there's anything further you think this one needs before it's in a fit state to be considered then please do shout and I'll endeavour to add something further. Thanks.
Hide
Alex Miller added a comment -

If you could update the ticket to better describe the approach of the patch that would be helpful.

Show
Alex Miller added a comment - If you could update the ticket to better describe the approach of the patch that would be helpful.
Hide
Joe Littlejohn added a comment -

The proposed patch (clj840-20161122.diff) allows 'each' fixtures to access the var associated with the currently executing test by using (first *testing-vars*). 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 *testing-vars*. test-var now invokes the joined fixtures function, rather than the joined fixtures function being given a think that invokes test-var.

Hopefully that's clear, ish

Show
Joe Littlejohn added a comment - The proposed patch (clj840-20161122.diff) allows 'each' fixtures to access the var associated with the currently executing test by using (first *testing-vars*). 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 *testing-vars*. test-var now invokes the joined fixtures function, rather than the joined fixtures function being given a think that invokes test-var. Hopefully that's clear, ish

People

Vote (1)
Watch (3)

Dates

  • Created:
    Updated: